Skip to content

Commit 09b3f2b

Browse files
committed
chore: api db
1 parent 2f98e9c commit 09b3f2b

File tree

1 file changed

+20
-0
lines changed
  • src/quant_research_starter/api

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"""Async database setup using SQLAlchemy for Postgres (asyncpg)."""
2+
from typing import AsyncGenerator
3+
import os
4+
from sqlalchemy.ext.asyncio import (
5+
create_async_engine,
6+
AsyncSession,
7+
async_sessionmaker,
8+
)
9+
from sqlalchemy.orm import declarative_base
10+
11+
DATABASE_URL = os.getenv("DATABASE_URL", "postgresql+asyncpg://postgres:password@localhost:5432/qrs")
12+
13+
engine = create_async_engine(DATABASE_URL, future=True, echo=False)
14+
AsyncSessionLocal = async_sessionmaker(engine, expire_on_commit=False, class_=AsyncSession)
15+
Base = declarative_base()
16+
17+
18+
async def get_session() -> AsyncGenerator[AsyncSession, None]:
19+
async with AsyncSessionLocal() as session:
20+
yield session

0 commit comments

Comments
 (0)