forked from ait-aecid/aminer-rest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
39 lines (30 loc) · 1.46 KB
/
Copy pathdatabase.py
File metadata and controls
39 lines (30 loc) · 1.46 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
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, Session
from sqlalchemy import String, Integer, Boolean, create_engine
from sqlalchemy.orm import sessionmaker
from typing import Generator, Optional
# SQLite example (you can change to PostgreSQL, MySQL, etc.)
DATABASE_URL = "sqlite:///./aminer-rest.db"
engine = create_engine(DATABASE_URL, connect_args={"check_same_thread": False}) # only for SQLite
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
class Base(DeclarativeBase):
pass
class UserDB(Base):
__tablename__ = "users"
id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True)
username: Mapped[str] = mapped_column(String, unique=True, index=True, nullable=False)
hashed_password: Mapped[str] = mapped_column(String, nullable=False)
email: Mapped[Optional[str]] = mapped_column(String, nullable=True)
full_name: Mapped[Optional[str]] = mapped_column(String, nullable=True)
disabled: Mapped[bool] = mapped_column(Boolean, default=False)
is_admin: Mapped[bool] = mapped_column(Boolean, default=False)
totp_secret: Mapped[Optional[str]] = mapped_column(String, nullable=True)
must_reset_password: Mapped[bool] = mapped_column(Boolean, default=True)
# Create tables if they don't exist
def init_db():
Base.metadata.create_all(bind=engine)
def get_db() -> Generator[Session, None, None]:
db = SessionLocal()
try:
yield db
finally:
db.close()