Skip to content

Commit 1241daf

Browse files
committed
🚸 fix(sqla): lazy orm init
1 parent d213e34 commit 1241daf

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

nonebot_plugin_orm/__init__.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,7 @@ def _init_orm():
9999
_init_engines()
100100
_init_table()
101101
_session_factory = sa_async.async_sessionmaker(
102-
**{
103-
**dict(bind=_engines[""], binds=_binds),
104-
**plugin_config.sqlalchemy_session_options,
105-
}
102+
_engines[""], binds=_binds, **plugin_config.sqlalchemy_session_options
106103
)
107104
_scoped_sessions = sa_async.async_scoped_session(
108105
_session_factory,
@@ -116,7 +113,9 @@ def get_session(**local_kw: Any) -> sa_async.AsyncSession:
116113
try:
117114
return _session_factory(**local_kw)
118115
except NameError:
119-
raise RuntimeError("nonebot-plugin-orm 未初始化") from None
116+
_init_orm()
117+
118+
return _session_factory(**local_kw)
120119

121120

122121
# NOTE: NoneBot DI will run sync function in thread pool executor,
@@ -131,7 +130,9 @@ def get_scoped_session() -> sa_async.async_scoped_session[sa_async.AsyncSession]
131130
try:
132131
return _scoped_sessions
133132
except NameError:
134-
raise RuntimeError("nonebot-plugin-orm 未初始化") from None
133+
_init_orm()
134+
135+
return _scoped_sessions
135136

136137

137138
async_scoped_session = Annotated[
@@ -180,7 +181,9 @@ def _init_engines():
180181
"可以通过 `pip install nonebot-plugin-orm[default]` 获得开箱即用的数据库配置."
181182
) from None
182183

183-
_engines[""] = _create_engine(f"sqlite+aiosqlite:///{_data_dir / 'db.sqlite3'}")
184+
_engines[""] = _create_engine(
185+
URL.create("sqlite+aiosqlite", database=str(_data_dir / "db.sqlite3"))
186+
)
184187

185188

186189
def _init_table():

0 commit comments

Comments
 (0)