Skip to content

Commit 95366fb

Browse files
committed
fleshing out engine
1 parent 296d2fa commit 95366fb

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

aragorm/engine.py

Whitespace-only changes.

aragorm/query/engine.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
)

0 commit comments

Comments
 (0)