-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodule.py
More file actions
146 lines (131 loc) · 5.28 KB
/
Copy pathmodule.py
File metadata and controls
146 lines (131 loc) · 5.28 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import functools
import typing as t
import sqlalchemy as sa
from ellar.app import current_injector
from ellar.common import IApplicationShutdown, IModuleSetup, Module
from ellar.common.utils.importer import get_main_directory_by_stack
from ellar.core import Config, DynamicModule, ModuleBase, ModuleSetup
from ellar.di import ProviderConfig, request_scope
from sqlalchemy.ext.asyncio import (
AsyncEngine,
AsyncSession,
)
from sqlalchemy.orm import Session
from ellar_sqlalchemy.services import EllarSQLAlchemyService
from .cli import DBCommands
from .schemas import MigrationOption, SQLAlchemyConfig
@Module(commands=[DBCommands])
class EllarSQLAlchemyModule(ModuleBase, IModuleSetup, IApplicationShutdown):
async def on_shutdown(self) -> None:
db_service = current_injector.get(EllarSQLAlchemyService)
db_service.session_factory.remove()
@classmethod
def setup(
cls,
*,
databases: t.Union[str, t.Dict[str, t.Any]],
migration_options: t.Union[t.Dict[str, t.Any], MigrationOption],
session_options: t.Optional[t.Dict[str, t.Any]] = None,
engine_options: t.Optional[t.Dict[str, t.Any]] = None,
models: t.Optional[t.List[str]] = None,
echo: bool = False,
) -> "DynamicModule":
"""
Configures EllarSQLAlchemyModule and setup required providers.
"""
root_path = get_main_directory_by_stack("__main__", stack_level=2)
if isinstance(migration_options, dict):
migration_options.update(
directory=get_main_directory_by_stack(
migration_options.get("directory", "__main__/migrations"),
stack_level=2,
from_dir=root_path,
)
)
if isinstance(migration_options, MigrationOption):
migration_options.directory = get_main_directory_by_stack(
migration_options.directory, stack_level=2, from_dir=root_path
)
migration_options = migration_options.dict()
schema = SQLAlchemyConfig.model_validate(
{
"databases": databases,
"engine_options": engine_options,
"echo": echo,
"models": models,
"migration_options": migration_options,
"root_path": root_path,
},
from_attributes=True,
)
return cls.__setup_module(schema)
@classmethod
def __setup_module(cls, sql_alchemy_config: SQLAlchemyConfig) -> DynamicModule:
db_service = EllarSQLAlchemyService(
databases=sql_alchemy_config.databases,
common_engine_options=sql_alchemy_config.engine_options,
common_session_options=sql_alchemy_config.session_options,
echo=sql_alchemy_config.echo,
models=sql_alchemy_config.models,
root_path=sql_alchemy_config.root_path,
migration_options=sql_alchemy_config.migration_options,
)
providers: t.List[t.Any] = []
if db_service._async_session_type:
providers.append(ProviderConfig(AsyncEngine, use_value=db_service.engine))
providers.append(
ProviderConfig(
AsyncSession,
use_value=lambda: db_service.session_factory(),
scope=request_scope,
)
)
else:
providers.append(ProviderConfig(sa.Engine, use_value=db_service.engine))
providers.append(
ProviderConfig(
Session,
use_value=lambda: db_service.session_factory(),
scope=request_scope,
)
)
providers.append(ProviderConfig(EllarSQLAlchemyService, use_value=db_service))
return DynamicModule(
cls,
providers=providers,
)
@classmethod
def register_setup(cls, **override_config: t.Any) -> ModuleSetup:
"""
Register Module to be configured through `SQLALCHEMY_CONFIG` variable in Application Config
"""
root_path = get_main_directory_by_stack("__main__", stack_level=2)
return ModuleSetup(
cls,
inject=[Config],
factory=functools.partial(
cls.__register_setup_factory,
root_path=root_path,
override_config=override_config,
),
)
@staticmethod
def __register_setup_factory(
module: t.Type["EllarSQLAlchemyModule"],
config: Config,
root_path: str,
override_config: t.Dict[str, t.Any],
) -> DynamicModule:
if config.get("SQLALCHEMY_CONFIG") and isinstance(
config.SQLALCHEMY_CONFIG, dict
):
defined_config = dict(config.SQLALCHEMY_CONFIG, root_path=root_path)
defined_config.update(override_config)
schema = SQLAlchemyConfig.model_validate(
defined_config, from_attributes=True
)
schema.migration_options.directory = get_main_directory_by_stack(
schema.migration_options.directory, stack_level=2, from_dir=root_path
)
return module.__setup_module(schema)
raise RuntimeError("Could not find `SQLALCHEMY_CONFIG` in application config.")