Dependency Injection : Connecting to Database

Git Commit: implement generator to query tables using DI
Dependency injection is a beautiful concept. I won't torture you with big words, let's understand it with a simple example. You should be knowing that we use a test database to run our unit test and a production/development database. Can you imagine what would happen if we use the same database everywhere? Yes, our database will become a mess in a few weeks! It would be filled up with useless emails like - [email protected], [email protected], and so on!

When we will be creating our logic to create a user, we would need to decouple our database settings and not hardcode it. During tests, we would want a different database and during development/production, our DB would be completely different. This can be achieved by using dependency injection. For this we would create a dependency called 'get_db' which would guide our database connection. During unit testing, we would override this 'get_db' dependency and we would get connected to some other test database. Now, I think you should have understood it to some extent. Now let's type in the below code in db > session.py

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from typing import Generator            #new


from core.config import settings


SQLALCHEMY_DATABASE_URL = settings.DATABASE_URL
engine = create_engine(SQLALCHEMY_DATABASE_URL)

#if you don't want to install postgres or any database, use sqlite, a file system based database, 
# uncomment below lines if you would like to use sqlite and comment above 2 lines of SQLALCHEMY_DATABASE_URL AND engine

# SQLALCHEMY_DATABASE_URL = "sqlite:///./sql_app.db"
# engine = create_engine(
#     SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False}
# )

SessionLocal = sessionmaker(autocommit=False,autoflush=False,bind=engine)

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

Done, now during testing, we would override this 'get_db' to connect to a different database. Let's meet after posts in the testing section. There we would get a better appreciation for dependencies.
Now, we will be able to utilize the generator to query the tables as below:

>>> from db.models.user import User
>>> from db.session import get_db
Database URL is  postgresql://nofoobar:supersecret@localhost:5432/blogdb
>>> db = get_db().__next__()
>>> db.query(User).all()
[]

FastAPITutorial

Brige the gap between Tutorial hell and Industry. We want to bring in the culture of Clean Code, Test Driven Development.

We know, we might make it hard for you but definitely worth the efforts.

Contacts

Refunds:

Refund Policy
Social

Follow us on our social media channels to stay updated.

© Copyright 2022-23 Team FastAPITutorial