DB connection¶
Low level helper methods to setup the database connection and the orm interaction with the database.
engine
¶
Simple adaptation of SQLModel official documentation to connect to a SQL database. At EcoAct we use postgresql and are happy with it
create_db_and_tables
¶
Simple wrapper around SQLModel create_all method.
Make sure to read this documentation, as the order matters between importing Models and this create_db_and_tables
.
What you usally find in our applications (being Dash or FastAPI):
# Go read https://sqlmodel.tiangolo.com/tutorial/create-db-and-table/?h=create_all#sqlmodel-metadata-order-matters to learn more
from ecodev_core import create_db_and_tables
import app.db_model as db_model # BELOW for a reason!
delete_table
¶
Simple helper to drop all passed SQLModel
model associated table entries.
def delete_table(model: Callable) -> None:
"""
Delete all rows of the passed model from db
"""
with Session(engine) as session:
result = session.execute(delete(model))
session.commit()
log.info(f'Deleted {result.rowcount} rows')
get_session
¶
Retrieves the session, used in Depends() attributes of fastapi routes. Completely inspired from the official SQLModel documentation, that you should read in its entirety (best return on a 1/2 day dev investment ever 😊)