-
Notifications
You must be signed in to change notification settings - Fork 2
/
db.py
41 lines (31 loc) · 987 Bytes
/
db.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
import asyncpg
from settings import POSTGRES_HOST, POSTGRES_DB, POSTGRES_PASSWORD, POSTGRES_USER
pool = None # asyncpg connection pool
async def create_tables():
async with pool.acquire() as conn:
await conn.fetch(
'''CREATE TABLE IF NOT EXISTS coin(
id VARCHAR PRIMARY KEY UNIQUE)'''
)
async def init_db():
global pool
pool = await asyncpg.create_pool(
host=POSTGRES_HOST,
database=POSTGRES_DB,
user=POSTGRES_USER,
password=POSTGRES_PASSWORD
)
await create_tables()
async def add_coins(coin_ids):
async with pool.acquire() as conn:
await conn.executemany(
'''INSERT INTO coin (id)
VALUES ($1)''',
((c_id,) for c_id in coin_ids),
)
async def get_coins():
async with pool.acquire() as conn:
result = await conn.fetch(
'''SELECT id FROM coin'''
)
return set(c['id'] for c in result)