Last active
November 21, 2023 12:45
-
-
Save cezarmezzalira/a112c6f576615abee97b876523834138 to your computer and use it in GitHub Desktop.
SQLModel database configuration example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from contextlib import contextmanager | |
from sqlmodel import Session, SQLModel, create_engine | |
PG_USERNAME = "root" | |
PG_PASSWORD = "postgres" | |
PG_HOST = "localhost" | |
PG_PORT = 54322 | |
PG_DATABASE = "race_db" | |
connect_args = {} | |
db_url = f"postgresql://{PG_USERNAME}:{PG_PASSWORD}@{PG_HOST}:{PG_PORT}/{PG_DATABASE}" | |
engine = create_engine(db_url, echo=True, connect_args=connect_args) | |
def create_db_and_tables(): | |
SQLModel.metadata.create_all(engine) | |
def get_engine(): | |
return engine | |
@contextmanager | |
def get_session(): | |
yield Session(engine) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment