-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.py
More file actions
16 lines (13 loc) · 656 Bytes
/
db.py
File metadata and controls
16 lines (13 loc) · 656 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
from sqlmodel import SQLModel
from sqlmodel.ext.asyncio.session import AsyncSession
from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker
from sqlalchemy import create_engine
from app.core.config import DATABASE_URL
# Async engine for FastAPI
async_engine = create_async_engine(DATABASE_URL, echo=True)
async_session = async_sessionmaker(async_engine, class_=AsyncSession, expire_on_commit=False)
# Sync engine for Celery worker
sync_engine = create_engine(DATABASE_URL.replace("+aiosqlite", ""), echo=True)
async def init_db():
async with async_engine.begin() as conn:
await conn.run_sync(SQLModel.metadata.create_all)