Closed statiqantriksh closed 1 year ago
Hi @statiqantriksh, let's assume that we want to use SQLAlchemy for database connection. and we have an ORM model called Location for the location records stored in our database. the following will be a sample for get() method in CRUD class using a database connection for retrieving a location.
from sqlalchemy import create_engine, select
from sqlalchemy.orm import Session
# an Engine, which the Session will use for connection resources
engine = create_engine("postgresql+psycopg2://scott:tiger@localhost/")
class Crud:
@classmethod
async def get(cls, module: enums.ModuleID, role: enums.RoleEnum, id, *args, **kwargs):
# create session and get object
with Session(engine) as session:
return session.get(Location, id)
@HamedAkhavan Thank you for the reply, The above sample aligns with what I'm trying to achieve here.
Best practice to setup an infrastructure layer in this project to be compatible with a relational database