diff options
| author | blackhao <13851610112@163.com> | 2025-12-10 20:12:21 -0600 |
|---|---|---|
| committer | blackhao <13851610112@163.com> | 2025-12-10 20:12:21 -0600 |
| commit | 9646da833bc3d94564c10649b62a378d0190471e (patch) | |
| tree | d0c9c0584b8c4f167c281f5970f713b239a1d7c5 /backend/app/auth/models.py | |
| parent | 9ba956c7aa601f0e6cd0fe2ede907cbc558fa1b8 (diff) | |
user data
Diffstat (limited to 'backend/app/auth/models.py')
| -rw-r--r-- | backend/app/auth/models.py | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/backend/app/auth/models.py b/backend/app/auth/models.py new file mode 100644 index 0000000..76c33fa --- /dev/null +++ b/backend/app/auth/models.py @@ -0,0 +1,41 @@ +import os +from sqlalchemy import Column, Integer, String, DateTime, create_engine +from sqlalchemy.ext.declarative import declarative_base +from sqlalchemy.orm import sessionmaker +from datetime import datetime + +# Database configuration +DATA_ROOT = os.path.abspath(os.getenv("DATA_ROOT", os.path.join(os.getcwd(), "data"))) +DATABASE_PATH = os.path.join(DATA_ROOT, "users.db") +DATABASE_URL = f"sqlite:///{DATABASE_PATH}" + +engine = create_engine(DATABASE_URL, connect_args={"check_same_thread": False}) +SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) +Base = declarative_base() + + +class User(Base): + __tablename__ = "users" + + id = Column(Integer, primary_key=True, index=True) + username = Column(String(50), unique=True, index=True, nullable=False) + email = Column(String(100), unique=True, index=True, nullable=False) + hashed_password = Column(String(255), nullable=False) + created_at = Column(DateTime, default=datetime.utcnow) + is_active = Column(Integer, default=1) + + +def init_db(): + """Initialize database tables""" + os.makedirs(DATA_ROOT, exist_ok=True) + Base.metadata.create_all(bind=engine) + + +def get_db(): + """Dependency to get database session""" + db = SessionLocal() + try: + yield db + finally: + db.close() + |
