-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathextension.py
More file actions
40 lines (30 loc) · 1.07 KB
/
extension.py
File metadata and controls
40 lines (30 loc) · 1.07 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 typing as t
from quart import Quart
from .. import signals
from ..config import SQLAlchemyConfig
from ..sqla import SQLAlchemy
from .cli import db_cli
class QuartSQLAlchemy(SQLAlchemy):
def __init__(
self,
config: SQLAlchemyConfig,
app: t.Optional[Quart] = None,
):
super().__init__(config)
if app is not None:
self.init_app(app)
def init_app(self, app: Quart) -> None:
if "sqlalchemy" in app.extensions:
raise RuntimeError(
f"A {type(self).__name__} instance has already been registered on this app"
)
signals.before_framework_extension_initialization.send(self, app=app)
app.extensions["sqlalchemy"] = self
@app.shell_context_processor
def export_sqlalchemy_objects():
return dict(
db=self,
**{m.class_.__name__: m.class_ for m in self.Model._sa_registry.mappers},
)
app.cli.add_command(db_cli)
signals.before_framework_extension_initialization.send(self, app=app)