-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.py
More file actions
45 lines (33 loc) · 1.28 KB
/
db.py
File metadata and controls
45 lines (33 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from sqlalchemy import create_engine, Column, Integer, String, ForeignKey
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, relationship
from dotenv import load_dotenv
import os
load_dotenv()
# PostgreSQL connection string
DATABASE_URL = os.getenv("DATABASE_URL")
engine = create_engine(DATABASE_URL)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base = declarative_base()
# User table
class User(Base):
__tablename__ = "users"
id = Column(Integer, primary_key=True, index=True)
email = Column(String, unique=True, index=True, nullable=False)
password_hash = Column(String, nullable=False)
tokens = relationship("UserToken", back_populates="user", cascade="all, delete")
# Google token storage
class UserToken(Base):
__tablename__ = "user_tokens"
id = Column(Integer, primary_key=True, index=True)
google_email = Column(String, index=True, nullable=False)
token_path = Column(String, nullable=False)
user_id = Column(Integer, ForeignKey("users.id", ondelete="CASCADE"), nullable=False)
user = relationship("User", back_populates="tokens")
# Dependency to use in FastAPI routes
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()