Spaces:
Paused
Paused
Update app/db/database.py
Browse files- app/db/database.py +32 -30
app/db/database.py
CHANGED
|
@@ -1,30 +1,32 @@
|
|
| 1 |
-
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession, async_sessionmaker
|
| 2 |
-
from sqlalchemy.orm import
|
| 3 |
-
from
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
async
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession, async_sessionmaker
|
| 2 |
+
from sqlalchemy.orm import declarative_base
|
| 3 |
+
from ..core.config import settings
|
| 4 |
+
|
| 5 |
+
# Create async engine for FastAPI
|
| 6 |
+
async_engine = create_async_engine(
|
| 7 |
+
settings.DATABASE_URL,
|
| 8 |
+
echo=True,
|
| 9 |
+
future=True,
|
| 10 |
+
pool_pre_ping=True
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
# Create async session factory
|
| 14 |
+
AsyncSessionLocal = async_sessionmaker(
|
| 15 |
+
bind=async_engine,
|
| 16 |
+
class_=AsyncSession,
|
| 17 |
+
expire_on_commit=False
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
# Create declarative base for models
|
| 21 |
+
Base = declarative_base()
|
| 22 |
+
|
| 23 |
+
# Database dependency for FastAPI routes
|
| 24 |
+
async def get_db():
|
| 25 |
+
async with AsyncSessionLocal() as session:
|
| 26 |
+
try:
|
| 27 |
+
yield session
|
| 28 |
+
finally:
|
| 29 |
+
await session.close()
|
| 30 |
+
|
| 31 |
+
# Add alias for external imports
|
| 32 |
+
db = AsyncSessionLocal
|