What is the best approach to hooking up database in FastAPI?

Viewed 2246

I found two approaches to working with databases in FastAPI. Are there any fundamental differences? if so which approach is preferable?

Approach 1 This is found in official FastAPI-fullstack example :

from fastapi import Depends
from sqlalchemy.orm import sessionmaker
from sqlalchemy import create_engine

engine = create_engine(SQLALCHEMY_DATABASE_URI, pool_pre_ping=True)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

def get_db() -> Generator:
    try:
        db = SessionLocal()
        yield db
    finally:
        db.close()


def get_current_user(
    db: Session = Depends(get_db), token: str = Depends(reusable_oauth2)
) -> models.User:
    ...

Approach 2 Following approach found in official FastAPI docs, some blogs and also in fastapi_users package documentation:

import databases

DATABASE_URL = "sqlite:///./test.db"
database = databases.Database(DATABASE_URL)

@app.on_event("startup")
async def startup():
    await database.connect()


@app.on_event("shutdown")
async def shutdown():
    await database.disconnect()

Edit (2021-09-06) - Approach 3

Another way recommended in the new SQLModel package docs by tiangolo himself. This seems like just a variant of the first approach above, but for sake of completeness:

https://sqlmodel.tiangolo.com/tutorial/fastapi/session-with-dependency/

sqlite_file_name = "database.db"
sqlite_url = f"sqlite:///{sqlite_file_name}"

connect_args = {"check_same_thread": False}
engine = create_engine(sqlite_url, echo=True, connect_args=connect_args)

def get_session():
    with Session(engine) as session:
        yield session

# Code here omitted 

@app.post("/heroes/", response_model=HeroRead)
def create_hero(*, session: Session = Depends(get_session), hero: HeroCreate):
    db_hero = Hero.from_orm(hero)
    session.add(db_hero)
    session.commit()
    session.refresh(db_hero)
    return db_hero

0 Answers
Related