hajimi-sqlalchemy 0.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- hajimi_sqlalchemy/__init__.py +4 -0
- hajimi_sqlalchemy/base.py +5 -0
- hajimi_sqlalchemy/config.py +29 -0
- hajimi_sqlalchemy/dependencies.py +20 -0
- hajimi_sqlalchemy/plugin.py +44 -0
- hajimi_sqlalchemy-0.0.1.dist-info/METADATA +8 -0
- hajimi_sqlalchemy-0.0.1.dist-info/RECORD +10 -0
- hajimi_sqlalchemy-0.0.1.dist-info/WHEEL +5 -0
- hajimi_sqlalchemy-0.0.1.dist-info/licenses/LICENSE +21 -0
- hajimi_sqlalchemy-0.0.1.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
from dataclasses import dataclass
|
|
2
|
+
|
|
3
|
+
from sqlalchemy import URL
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
@dataclass(frozen=True, kw_only=True)
|
|
7
|
+
class HajimiSQLAlchemyConfig:
|
|
8
|
+
username: str
|
|
9
|
+
password: str
|
|
10
|
+
host: str
|
|
11
|
+
port: int = 5432
|
|
12
|
+
database: str
|
|
13
|
+
|
|
14
|
+
echo: bool = False
|
|
15
|
+
pool_size: int = 3
|
|
16
|
+
max_overflow: int = 2
|
|
17
|
+
pool_timeout: int = 30
|
|
18
|
+
pool_recycle: int = 1800
|
|
19
|
+
|
|
20
|
+
@property
|
|
21
|
+
def url(self) -> URL:
|
|
22
|
+
return URL.create(
|
|
23
|
+
drivername='postgresql+psycopg',
|
|
24
|
+
username=self.username,
|
|
25
|
+
password=self.password,
|
|
26
|
+
host=self.host,
|
|
27
|
+
port=self.port,
|
|
28
|
+
database=self.database,
|
|
29
|
+
)
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"""FastAPI dependencies exposing the SQLAlchemy async session."""
|
|
2
|
+
|
|
3
|
+
from typing import Annotated, AsyncGenerator
|
|
4
|
+
|
|
5
|
+
from fastapi import Depends, Request
|
|
6
|
+
from sqlalchemy.ext.asyncio import AsyncSession
|
|
7
|
+
|
|
8
|
+
from .plugin import HajimiSQLAlchemyPlugin
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
async def get_session(request: Request) -> AsyncGenerator[AsyncSession, None]:
|
|
12
|
+
"""Yield an ``AsyncSession`` scoped to the request, opened from the registered plugin."""
|
|
13
|
+
app_state = request.app.state._state['__app_state__']
|
|
14
|
+
plugin: HajimiSQLAlchemyPlugin = getattr(app_state, HajimiSQLAlchemyPlugin.__name__)
|
|
15
|
+
|
|
16
|
+
async with plugin.session_maker.begin() as session:
|
|
17
|
+
yield session
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
SessionDep = Annotated[AsyncSession, Depends(get_session)]
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
from fastapi import FastAPI
|
|
2
|
+
from sqlalchemy.ext.asyncio import AsyncEngine, AsyncSession, async_sessionmaker, create_async_engine
|
|
3
|
+
|
|
4
|
+
from .config import HajimiSQLAlchemyConfig
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class HajimiSQLAlchemyPlugin:
|
|
8
|
+
def __init__(self, config: HajimiSQLAlchemyConfig) -> None:
|
|
9
|
+
self._config = config
|
|
10
|
+
self._engine: AsyncEngine | None = None
|
|
11
|
+
self._session_maker: async_sessionmaker[AsyncSession] | None = None
|
|
12
|
+
|
|
13
|
+
@property
|
|
14
|
+
def session_maker(self) -> async_sessionmaker[AsyncSession]:
|
|
15
|
+
if not isinstance(self._session_maker, async_sessionmaker):
|
|
16
|
+
raise RuntimeError(
|
|
17
|
+
'HajimiSQLAlchemyPlugin.session_maker is unavailable: '
|
|
18
|
+
'startup() has not been called or shutdown() has already run.'
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
return self._session_maker
|
|
22
|
+
|
|
23
|
+
async def startup(self, app: FastAPI) -> None:
|
|
24
|
+
self._engine = create_async_engine(
|
|
25
|
+
url=self._config.url,
|
|
26
|
+
echo=self._config.echo,
|
|
27
|
+
pool_size=self._config.pool_size,
|
|
28
|
+
max_overflow=self._config.max_overflow,
|
|
29
|
+
pool_timeout=self._config.pool_timeout,
|
|
30
|
+
pool_recycle=self._config.pool_recycle,
|
|
31
|
+
pool_pre_ping=True,
|
|
32
|
+
connect_args={'connect_timeout': 6},
|
|
33
|
+
)
|
|
34
|
+
self._session_maker = async_sessionmaker(engine=self._engine)
|
|
35
|
+
app.state.__app__state[self.__name__] = self
|
|
36
|
+
|
|
37
|
+
async def shutdown(self, app: FastAPI) -> None:
|
|
38
|
+
if self._session_maker is not None:
|
|
39
|
+
self._session_maker = None
|
|
40
|
+
if self._engine is not None:
|
|
41
|
+
await self._engine.dispose()
|
|
42
|
+
self._engine = None
|
|
43
|
+
|
|
44
|
+
app.state.__app__state.pop(self.__name__, None)
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
hajimi_sqlalchemy/__init__.py,sha256=zNelzQf6mpE3P9adR7oWuDMQ0QJI9LFVRtOAXWFpNWE,151
|
|
2
|
+
hajimi_sqlalchemy/base.py,sha256=atu01ommpKNd6MbNlta39RpRr4gsit67eyp9kmWnYZ8,103
|
|
3
|
+
hajimi_sqlalchemy/config.py,sha256=A_8mvzlKk0uzWGWe-P9z_clq11LvG8dKyk_gYfjJJu4,637
|
|
4
|
+
hajimi_sqlalchemy/dependencies.py,sha256=VJBYJ7sQt8h6qGf3xqa0OoQ6UwkUOUh6HzkihSsIIlI,704
|
|
5
|
+
hajimi_sqlalchemy/plugin.py,sha256=EE0Ce4rAkJz-NGY0ReS_jYY47XFi7SFsWm6RZwMpvUI,1694
|
|
6
|
+
hajimi_sqlalchemy-0.0.1.dist-info/licenses/LICENSE,sha256=2N6aWoJwaqUS051iv3_th5CYm77Wv2yjZ7hfk4q1SXI,1063
|
|
7
|
+
hajimi_sqlalchemy-0.0.1.dist-info/METADATA,sha256=ciuDzjYAtUWvNHJ06VXhaQfZNtPjdNxZ76lSJZ4Yt7Q,225
|
|
8
|
+
hajimi_sqlalchemy-0.0.1.dist-info/WHEEL,sha256=K260EYznzXsJYBQGqmI8VTxEdiZYNvDZwW9cBh9-_MA,91
|
|
9
|
+
hajimi_sqlalchemy-0.0.1.dist-info/top_level.txt,sha256=ttsxvMfRWlYAHZMZWBe4tnVu8pQiBAEJVyH7HkaT3L0,18
|
|
10
|
+
hajimi_sqlalchemy-0.0.1.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 huruhi
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
hajimi_sqlalchemy
|