File tree Expand file tree Collapse file tree 2 files changed +40
-0
lines changed
Expand file tree Collapse file tree 2 files changed +40
-0
lines changed Original file line number Diff line number Diff line change 1+ import asyncio
2+ import typing as t
3+
4+ import asyncpg
5+ from asyncpg .pool import Pool
6+
7+
8+ class Engine ():
9+ """
10+ Currently when using run ... it sets up a connection each time.
11+
12+ When instantiated ... create the connection pool ...
13+
14+ Needs to be a singleton that's shared by all the tables.
15+ """
16+
17+ pool : t .Optional [Pool ] = None
18+
19+ def __init__ (self , config : t .Dict [str , t .Any ]) -> None :
20+ self .config = config
21+
22+ async def get_pool (self ):
23+ if not self .pool :
24+ self .pool = await asyncpg .create_pool (
25+ ** self .config
26+ )
27+ return self .pool
28+
29+ async def run (self , query : str ):
30+ pool = await self .get_pool ()
31+
32+ async with pool .acquire () as connection :
33+ response = connection .fetch (query )
34+
35+ return response
36+
37+ def run_sync (self , query : str ):
38+ return asyncio .run (
39+ self .run (query )
40+ )
You can’t perform that action at this time.
0 commit comments