-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.py
More file actions
39 lines (33 loc) · 1.09 KB
/
db.py
File metadata and controls
39 lines (33 loc) · 1.09 KB
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
import os
from psycopg2.pool import SimpleConnectionPool
from fastapi import HTTPException
from dotenv import load_dotenv
from contextlib import contextmanager
load_dotenv()
class Database:
def __init__(self):
self.pool = SimpleConnectionPool(
minconn=os.getenv("POSTGRES_MIN_CONNECTIONS"),
maxconn=os.getenv("POSTGRES_MAX_CONNECTIONS"),
user=os.getenv("POSTGRES_USER"),
password=os.getenv("POSTGRES_PASSWORD"),
host=os.getenv("POSTGRES_HOST"),
port=os.getenv("POSTGRES_PORT"),
database=os.getenv("POSTGRES_DATABASE"),
)
@contextmanager
def get_connection(self):
conn = self.pool.getconn()
if not conn:
raise HTTPException(
status_code=500, detail="Unable to get a database connection"
)
try:
yield conn
finally:
self.pool.putconn(conn)
def release_connection(self, conn):
self.pool.putconn(conn)
def close_all_connections(self):
if self.pool:
self.pool.closeall()