Skip to main content
BackAnt uses SQLAlchemy with PostgreSQL via psycopg2. The database layer is configured in api/startup/Alchemy.py and accessed through the DBSession wrapper in api/helper/DBSession.py.

How it works

On application startup, create_app() in app.py calls init_db():
init_db() imports all models (so SQLAlchemy knows about all tables), optionally drops them if CLEAR_DB=True, then creates any missing tables:

Engine and session

The engine connects to PostgreSQL using credentials from Environment.py:
db_session is a thread-local scoped session. It is removed at the end of each request via the teardown_appcontext hook in app.py.

DBSession wrapper

All database operations go through the DBSession class in api/helper/DBSession.py. It wraps the scoped session with error handling and automatic rollback:
The myDB singleton is pre-instantiated and injected into every repository.

Adding a new model

When ant generate route <name> runs, it:
  1. Creates api/models/<Name>_model.py
  2. Adds an import of the model to init_db() in api/startup/Alchemy.py
The model is then included in create_all() on the next startup. No manual migration step is required for new tables in development.

Environment variables

Connecting outside Docker

Set DB_URL=localhost in .env and ensure PostgreSQL is running locally on port 5432.