feat: SQLAlchemy async database setup
This commit is contained in:
@@ -0,0 +1,20 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
from typing import TYPE_CHECKING
|
||||||
|
from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker, AsyncSession
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from quart import Quart
|
||||||
|
|
||||||
|
|
||||||
|
def init_db(app: "Quart") -> None:
|
||||||
|
"""Create async engine and attach db_sessionmaker to app.
|
||||||
|
|
||||||
|
Called during create_app() after config is loaded.
|
||||||
|
Does not create tables — Alembic handles migrations.
|
||||||
|
"""
|
||||||
|
db_url: str = app.config["DATABASE_URL"]
|
||||||
|
engine = create_async_engine(db_url, echo=False)
|
||||||
|
app.db_sessionmaker: async_sessionmaker[AsyncSession] = async_sessionmaker(
|
||||||
|
engine, expire_on_commit=False
|
||||||
|
)
|
||||||
|
app._db_engine = engine
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
from .base import Base
|
||||||
|
from .users import User
|
||||||
|
|
||||||
|
__all__ = ["Base", "User"]
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
from sqlalchemy.orm import DeclarativeBase
|
||||||
|
|
||||||
|
|
||||||
|
class Base(DeclarativeBase):
|
||||||
|
pass
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
# fablednetmon/models/users.py — stub, filled in Task 4
|
||||||
|
from .base import Base
|
||||||
Reference in New Issue
Block a user