-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Using 2.0 sqlalchemy style for future compatibility. The `get_db` function can be used as a dependency to inject a working db session where it will be needed.
- Loading branch information
Showing
8 changed files
with
113 additions
and
2 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from typing import AsyncIterator | ||
|
||
import sqlalchemy as sa | ||
import sqlalchemy.ext.asyncio as saa | ||
from sqlalchemy import orm | ||
|
||
from app import config | ||
|
||
settings = config.get_settings() | ||
|
||
|
||
# echo: if True, the Engine will log all statements as well as a repr() of their | ||
# parameter lists to the default log handler, which defaults to sys.stdout for output. | ||
# | ||
# hide_parameters: Boolean, when set to True, SQL statement parameters will not be | ||
# displayed in INFO logging nor will they be formatted into the string representation | ||
# of StatementError objects. | ||
# | ||
# future=True: Use the 2.0 style Engine and Connection API. | ||
engine = saa.create_async_engine( | ||
settings.db_uri, | ||
echo=settings.db_echo, | ||
hide_parameters=settings.db_hide_params, | ||
future=True, | ||
) | ||
|
||
# https://alembic.sqlalchemy.org/en/latest/naming.html | ||
convention = { | ||
"ix": "ix_%(column_0_label)s", | ||
"uq": "uq_%(table_name)s_%(column_0_name)s", | ||
"ck": "ck_%(table_name)s_%(constraint_name)s", | ||
"fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s", | ||
"pk": "pk_%(table_name)s", | ||
} | ||
mapper_registry = orm.registry(sa.MetaData(naming_convention=convention)) | ||
|
||
|
||
async def get_db() -> AsyncIterator[saa.AsyncSession]: | ||
# https://docs.sqlalchemy.org/en/14/orm/extensions/asyncio.html#preventing-implicit-io-when-using-asyncsession | ||
# expire_on_commit=False will prevent attributes from being expired after commit | ||
async_session = saa.AsyncSession(engine, expire_on_commit=False) | ||
try: | ||
yield async_session | ||
finally: | ||
await async_session.close() |
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
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
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
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