-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdb.py
More file actions
36 lines (22 loc) · 1013 Bytes
/
db.py
File metadata and controls
36 lines (22 loc) · 1013 Bytes
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
from typing import AsyncGenerator
from fastapi import Depends
from fastapi_users.db import SQLAlchemyBaseUserTableUUID, SQLAlchemyUserDatabase
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
from sqlalchemy.orm import DeclarativeBase
from utils import get_settings
class Base(DeclarativeBase):
pass
class User(SQLAlchemyBaseUserTableUUID, Base):
pass
_engine = create_async_engine(get_settings().AUTH_DATABASE_URL)
_async_session_maker = async_sessionmaker(_engine, expire_on_commit=False)
async def _get_async_session() -> AsyncGenerator[AsyncSession, None]:
async with _async_session_maker() as session:
yield session
async def make_sure_db_and_tables() -> None:
async with _engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)
async def get_user_db(
session: AsyncSession = Depends(_get_async_session),
) -> AsyncGenerator[SQLAlchemyUserDatabase, None]:
yield SQLAlchemyUserDatabase(session, User)