-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathdatabase.py
More file actions
40 lines (32 loc) · 1.03 KB
/
Copy pathdatabase.py
File metadata and controls
40 lines (32 loc) · 1.03 KB
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
38
39
40
import os
from typing import AsyncIterator
from sqlalchemy.ext.asyncio import (
AsyncEngine,
AsyncSession,
async_sessionmaker,
create_async_engine,
)
from sqlalchemy.orm import declarative_base
Base = declarative_base()
_engine = None
def get_engine() -> AsyncEngine:
global _engine
if _engine is None:
_engine = create_async_engine(os.getenv("DATABASE_URL"))
return _engine
async def get_db_session() -> AsyncIterator[AsyncSession]:
"""Provide a session scoped to one request.
The transaction boundary lives here, not in the database clients: the
session commits once the request handler finishes successfully and rolls
back if it raises, so a request that writes multiple records stays atomic.
"""
session_factory = async_sessionmaker(bind=get_engine(), expire_on_commit=False)
session = session_factory()
try:
yield session
await session.commit()
except Exception:
await session.rollback()
raise
finally:
await session.close()