-
Notifications
You must be signed in to change notification settings - Fork 0
/
i_database.py
43 lines (30 loc) · 880 Bytes
/
i_database.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
"""
This module is used for providing db connection
"""
from typing import List
import asyncpg
from project_secrets import postgres_secret
#: types
#: provides some types for referencing
DBConnection = asyncpg.Connection
DBRow = asyncpg.Record
DBRows = List[DBRow]
#: connection pool object
__db_connection_pool: asyncpg.pool.Pool = None
async def get_db_context() -> asyncpg.pool.PoolAcquireContext:
"""
returns one connection from the connection pool
"""
if __db_connection_pool is None:
await __init_db()
return __db_connection_pool.acquire()
async def __init_db():
"""
initiates db connection pool
"""
global __db_connection_pool
__db_connection_pool = await asyncpg.create_pool(
database=postgres_secret['database'],
user=postgres_secret['user'],
password=postgres_secret['password']
)