Skip to content

init

Most database access should be handled through functions available in this module...

Preferred usage:

from backend.services.database import get_session, get_X

async with get_session() as db:
    data: SchemaBaseType = get_X(db)

I.e. The data is loaded in some subclass of SchemaBase (rather than the model itself). Model ID can be stored in the schema for easy access to the model for updates at a later step.

Note: Models are stored in the separate models package so that any database service can import any combination of models without concerns of circular imports.

__all__ = ['db_uri_from_settings', 'get_session', 'get_sessionmaker', 'initialize_db'] module-attribute

db_uri_from_settings(asynchronous=True, driver='postgresql', async_addition='+asyncpg', user=settings.POSTGRES_USER, password=settings.POSTGRES_PASSWORD, host=settings.POSTGRES_HOST, port=settings.POSTGRES_PORT, db=settings.POSTGRES_DB)

get_session() async

Get a new session from the db.

get_sessionmaker()

Get the session maker.

initialize_db(database_uri)

Initialize the db engine and session maker.