How to yield a db connection in a python sqlalchemy function similar to how it is done in FastAPI?

Question:

In FastAPI I had the following function that I used to open and close a DB session:

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

And within the routes of my API I would do something like that:

@router.get("/")
async def read_all_events(user: dict = Depends(get_current_user), db: Session = Depends(get_db)):
    logger.info("API read_all_events")
    if user is None:
        raise http_user_credentials_not_valid_exception()
    return db.query(models.Events).all()

You can see that I am injectin the session in the api call.

So now i want to do something similar within a python function:

def do_something():
   #get person data from database
   #play with person data
   #save new person data in database
   #get cars data from database

So i am wondering if I should use the same approach than in FastAPI (i do not know how) or if i just should be openning and clossing the connection manually like that:

def do_something():
   try:
        db = SessionLocal()
        yield db
 
        #get person data from database
        #play with person data
        #save new person data in database
        #get cars data from database
   finally:
        db.close()

Thanks

Asked By: Víctor Rosillo

||

Answers:

The usage of yield in this case is so that Depends(get_db) returns the db session instance, so that it can be used in the fastapi route, and as soon as the fastapi route returns response to user, the finally clause (db.close()) will be executed. This is good because every request will be using a separate db session, and db connections will be closed after every route response.

If you want to use the db session normally in a function, just get the db instance using db = SessionLocal(), and proceed to use the db instance in the function.

Example:

def do_something():
    db = SessionLocal()
    event = db.query(models.Events).first()
    db.delete(event)
    db.commit()
    db.close()
Answered By: Zihao Lam
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.