-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
29 lines (20 loc) · 708 Bytes
/
database.py
File metadata and controls
29 lines (20 loc) · 708 Bytes
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
from fastapi import Depends
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from typing import Annotated
SQLALCHEMY_DATABASE_URL = "sqlite:///./my_todos.db"
engine = create_engine(
SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False}
)
# check same thread will check if the thread is same or not
# if not then it will create a new thread
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base = declarative_base()
def get_db():
try:
db = SessionLocal()
yield db
finally:
db.close()
db_dependency = Annotated[SessionLocal, Depends(get_db)]