-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.py
37 lines (30 loc) · 1 KB
/
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
import os
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
from sqlalchemy.orm import sessionmaker
from sqlalchemy.pool import NullPool
from dotenv import load_dotenv
from contextlib import asynccontextmanager
import asyncio
from models.base import Base # Make sure your models are imported here
# Load environment variables
load_dotenv()
# Get the database URL from the .env file
DATABASE_URL = os.getenv("DATABASE_URL")
if not DATABASE_URL:
raise ValueError("DATABASE_URL is not set in .env file.")
# Create an async engine
engine = create_async_engine(DATABASE_URL, echo=False, poolclass=NullPool, pool_recycle=3600)
# Create a sessionmaker for async sessions
async_session = sessionmaker(
bind=engine,
expire_on_commit=False,
class_=AsyncSession
)
# Function to get the session
@asynccontextmanager
async def get_session() -> AsyncSession:
"""
Provide a database session in an async context manager.
"""
async with async_session() as session:
yield session