@@ -51,26 +51,45 @@ for example for PostgreSQL:
5151``` python
5252from sqlalchemy.ext.asyncio import (
5353 async_sessionmaker,
54+ AsyncEngine,
5455 AsyncSession,
5556 create_async_engine,
5657)
57- from context_async_sqlalchemy import db_connect
58-
59- db_connect.engine = create_async_engine(
60- f " postgresql+asyncpg:// "
61- f " { pg_user} : { pg_password} "
62- f " @ { host} : { pg_port} "
63- f " / { pg_db} " ,
64- future = True ,
65- pool_pre_ping = True ,
66- )
67- db_connect.session_maker = async_sessionmaker(
68- db_connect.engine, class_ = AsyncSession, expire_on_commit = False
69- )
7058
59+
60+ def create_engine (host : str ) -> AsyncEngine:
61+ """
62+ database connection parameters.
63+ In production code, you will probably take these parameters from
64+ the environment.
65+ """
66+ pg_user = " krylosov-aa"
67+ pg_password = " "
68+ pg_port = 6432
69+ pg_db = " test"
70+ return create_async_engine(
71+ f " postgresql+asyncpg:// "
72+ f " { pg_user} : { pg_password} "
73+ f " @ { host} : { pg_port} "
74+ f " / { pg_db} " ,
75+ future = True ,
76+ pool_pre_ping = True ,
77+ )
78+
79+
80+ def create_session_maker (
81+ engine : AsyncEngine,
82+ ) -> async_sessionmaker[AsyncSession]:
83+ """ session parameters"""
84+ return async_sessionmaker(
85+ engine, class_ = AsyncSession, expire_on_commit = False
86+ )
7187```
7288
73- #### 2. Close the resources at the end of your application's life
89+ #### 2. Manage Database connection lifecycle
90+ Configure the connection to the database at the begin of your application's life.
91+ Close the resources at the end of your application's life
92+
7493
7594Example for FastAPI:
7695
@@ -79,18 +98,31 @@ import asyncio
7998from typing import Any, AsyncGenerator
8099from contextlib import asynccontextmanager
81100from fastapi import FastAPI
82- from context_async_sqlalchemy import db_connect
83101
84- @asynccontextmanager
85- async def lifespan (app : FastAPI) -> AsyncGenerator[None , Any]:
102+ from context_async_sqlalchemy import (
103+ master_connect,
104+ replica_connect,
105+ )
106+
107+ from .database import create_engine, create_session_maker
108+
109+ async def setup_database () -> None :
86110 """
87- It is important to clean up resources at the end of an application's
88- life .
111+ Here you pass the database connection parameters to the library.
112+ More specifically, the engine and session maker .
89113 """
114+ master_connect.engine_creator = create_engine
115+ master_connect.session_maker_creator = create_session_maker
116+ await master_connect.connect(" 127.0.0.1" )
117+
118+ @asynccontextmanager
119+ async def lifespan (app : FastAPI) -> AsyncGenerator[None , Any]:
120+ """ Database connection lifecycle management"""
121+ await setup_database()
90122 yield
91123 await asyncio.gather(
92- db_connect .close(), # Close the engine if it was open
93- ... # other resources in your application
124+ master_connect .close(), # Close the engine if it was open
125+ replica_connect.close(), # Close the engine if it was open
94126 )
95127```
96128
0 commit comments