-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
35 lines (28 loc) · 1015 Bytes
/
Copy pathdatabase.py
File metadata and controls
35 lines (28 loc) · 1015 Bytes
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
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
# [prod] : Will move to .env
HOST = 'localhost'
PORT = 5432
USERNAME = 'postgres'
PASSWORD = 'postgres'
DATABASE = 'test_db'
# postgresql://USER:PASSWORD@INTERNAL_HOST:PORT/DATABASE
SQLALCHEMY_DATABASE_URL = f"postgresql+asyncpg://{USERNAME}:{PASSWORD}@{HOST}:{PORT}/{DATABASE}"
engine = create_async_engine(SQLALCHEMY_DATABASE_URL)
SessionLocal = sessionmaker(
autocommit=False,
autoflush=False,
bind=engine,
class_=AsyncSession, # Ensure the session is asynchronous
expire_on_commit=False, # Prevent SQLAlchemy from expiring objects after commit
)
Base = declarative_base()
async def get_db() -> AsyncSession: # type: ignore
async with SessionLocal() as db:
try:
yield db
except Exception as e:
print(f"Error; database.py : {e}")
finally:
await db.close()