90 lines
3.0 KiB
Python
90 lines
3.0 KiB
Python
from sqlalchemy import create_engine, Column, String, Boolean, DateTime, ForeignKey
|
|
from sqlalchemy.ext.declarative import declarative_base
|
|
from sqlalchemy.orm import sessionmaker, relationship
|
|
from datetime import datetime
|
|
import os
|
|
|
|
# PostgreSQL connection string
|
|
# Format: postgresql://username:password@localhost:5432/database_name
|
|
DATABASE_URL = os.getenv(
|
|
"DATABASE_URL",
|
|
"postgresql://tasko_user:tasko_password@localhost:5432/tasko_db"
|
|
)
|
|
|
|
engine = create_engine(DATABASE_URL)
|
|
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
|
|
|
Base = declarative_base()
|
|
|
|
class User(Base):
|
|
__tablename__ = "users"
|
|
|
|
id = Column(String, primary_key=True, index=True)
|
|
username = Column(String, unique=True, index=True, nullable=False)
|
|
email = Column(String, unique=True, index=True, nullable=False)
|
|
password_hash = Column(String, nullable=False)
|
|
created_at = Column(DateTime, default=datetime.utcnow)
|
|
|
|
# Relationships
|
|
lists = relationship("TaskList", back_populates="user", cascade="all, delete-orphan")
|
|
tasks = relationship("Task", back_populates="user", cascade="all, delete-orphan")
|
|
tokens = relationship("Token", back_populates="user", cascade="all, delete-orphan")
|
|
|
|
|
|
class Token(Base):
|
|
__tablename__ = "tokens"
|
|
|
|
token = Column(String, primary_key=True, index=True)
|
|
user_id = Column(String, ForeignKey("users.id"), nullable=False)
|
|
created_at = Column(DateTime, default=datetime.utcnow)
|
|
|
|
# Relationships
|
|
user = relationship("User", back_populates="tokens")
|
|
|
|
|
|
class TaskList(Base):
|
|
__tablename__ = "task_lists"
|
|
|
|
id = Column(String, primary_key=True, index=True)
|
|
user_id = Column(String, ForeignKey("users.id"), nullable=False)
|
|
name = Column(String, nullable=False)
|
|
icon = Column(String, default="📝")
|
|
color = Column(String, default="#667eea")
|
|
created_at = Column(DateTime, default=datetime.utcnow)
|
|
|
|
# Relationships
|
|
user = relationship("User", back_populates="lists")
|
|
tasks = relationship("Task", back_populates="task_list", cascade="all, delete-orphan")
|
|
|
|
|
|
class Task(Base):
|
|
__tablename__ = "tasks"
|
|
|
|
id = Column(String, primary_key=True, index=True)
|
|
list_id = Column(String, ForeignKey("task_lists.id"), nullable=False)
|
|
user_id = Column(String, ForeignKey("users.id"), nullable=False)
|
|
title = Column(String, nullable=False)
|
|
description = Column(String, nullable=True)
|
|
completed = Column(Boolean, default=False)
|
|
priority = Column(String, default="medium")
|
|
created_at = Column(DateTime, default=datetime.utcnow)
|
|
updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
|
|
|
|
# Relationships
|
|
user = relationship("User", back_populates="tasks")
|
|
task_list = relationship("TaskList", back_populates="tasks")
|
|
|
|
|
|
def init_db():
|
|
"""Initialize the database"""
|
|
Base.metadata.create_all(bind=engine)
|
|
|
|
|
|
def get_db():
|
|
"""Dependency for getting database session"""
|
|
db = SessionLocal()
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.close()
|