-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
30 lines (22 loc) · 1.03 KB
/
Copy pathdatabase.py
File metadata and controls
30 lines (22 loc) · 1.03 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
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.orm import declarative_base #(Creates a base class for all database tables.)
from config import settings
DATABASE_URL=settings.DATABASE_URL #WHERE THE DATABASE IS
engine = create_engine( #CREATE_ENGINE
DATABASE_URL,
#connect_args = {"check_same_thread": False} #Only the thread that created the connection can use it. But FastAPI handles multiple requests simultaneously. So we disable that restriction
)
SessionLocal = sessionmaker( #CREATE DATABASE SESSION
autocommit = False, #Changes are NOT automatically saved.
autoflush = False, #Prevents SQLAlchemy from automatically sending changes to the database. We decide when to save. Good for control and performance.
bind = engine #Connects sessions to the database engine.
)
Base = declarative_base() #Creates a parent class for all models.
#Dependencies functions:
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()