-
Notifications
You must be signed in to change notification settings - Fork 2
/
database.py
26 lines (17 loc) · 747 Bytes
/
database.py
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
from typing import Annotated
from fastapi import Depends
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, Session
from db_models import base_model
from db_models import *
# SQLALCHEMY_DATABASE_URL = "sqlite:///./database.sqlite"
SQLALCHEMY_DATABASE_URL = "postgresql+psycopg://postgres:password@localhost:5432/postgres"
engine = create_engine(SQLALCHEMY_DATABASE_URL)
session_factory = sessionmaker(engine, expire_on_commit=False)
# A route accesses DB by "Depends()"ing on this:
def get_db():
with session_factory() as session:
yield session
DB_dependency = Annotated[Session, Depends(get_db)]
def init_db():
base_model.BaseModel_DB.metadata.create_all(bind=engine)