TECHS-Technological-Solutions / ocpi

Python implementation of the Open Charge Point Interface (OCPI)
MIT License
49 stars 15 forks source link

sample suggestions for setting up the infrastructure(database) layer ? #33

Closed statiqantriksh closed 1 year ago

statiqantriksh commented 1 year ago

Best practice to setup an infrastructure layer in this project to be compatible with a relational database

HamedAkhavan commented 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)
statiqantriksh commented 1 year ago

@HamedAkhavan Thank you for the reply, The above sample aligns with what I'm trying to achieve here.