fastapi-extra 0.3.6__tar.gz → 0.4.0__tar.gz
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.
- {fastapi_extra-0.3.6 → fastapi_extra-0.4.0}/PKG-INFO +1 -1
- {fastapi_extra-0.3.6 → fastapi_extra-0.4.0}/fastapi_extra/__init__.py +1 -1
- {fastapi_extra-0.3.6 → fastapi_extra-0.4.0}/fastapi_extra/database/service.py +17 -2
- {fastapi_extra-0.3.6 → fastapi_extra-0.4.0}/fastapi_extra/response.py +1 -3
- {fastapi_extra-0.3.6 → fastapi_extra-0.4.0}/fastapi_extra.egg-info/PKG-INFO +1 -1
- {fastapi_extra-0.3.6 → fastapi_extra-0.4.0}/LICENSE +0 -0
- {fastapi_extra-0.3.6 → fastapi_extra-0.4.0}/README.rst +0 -0
- {fastapi_extra-0.3.6 → fastapi_extra-0.4.0}/fastapi_extra/_patch.py +0 -0
- {fastapi_extra-0.3.6 → fastapi_extra-0.4.0}/fastapi_extra/cache/__init__.py +0 -0
- {fastapi_extra-0.3.6 → fastapi_extra-0.4.0}/fastapi_extra/cache/redis.py +0 -0
- {fastapi_extra-0.3.6 → fastapi_extra-0.4.0}/fastapi_extra/cursor.pyi +0 -0
- {fastapi_extra-0.3.6 → fastapi_extra-0.4.0}/fastapi_extra/database/__init__.py +0 -0
- {fastapi_extra-0.3.6 → fastapi_extra-0.4.0}/fastapi_extra/database/model.py +0 -0
- {fastapi_extra-0.3.6 → fastapi_extra-0.4.0}/fastapi_extra/database/session.py +0 -0
- {fastapi_extra-0.3.6 → fastapi_extra-0.4.0}/fastapi_extra/database/sqlmap.py +0 -0
- {fastapi_extra-0.3.6 → fastapi_extra-0.4.0}/fastapi_extra/dependency.py +0 -0
- {fastapi_extra-0.3.6 → fastapi_extra-0.4.0}/fastapi_extra/form.py +0 -0
- {fastapi_extra-0.3.6 → fastapi_extra-0.4.0}/fastapi_extra/native/cursor.pyx +0 -0
- {fastapi_extra-0.3.6 → fastapi_extra-0.4.0}/fastapi_extra/native/routing.pyx +0 -0
- {fastapi_extra-0.3.6 → fastapi_extra-0.4.0}/fastapi_extra/native/urlparse.pyx +0 -0
- {fastapi_extra-0.3.6 → fastapi_extra-0.4.0}/fastapi_extra/py.typed +0 -0
- {fastapi_extra-0.3.6 → fastapi_extra-0.4.0}/fastapi_extra/routing.pyi +0 -0
- {fastapi_extra-0.3.6 → fastapi_extra-0.4.0}/fastapi_extra/settings.py +0 -0
- {fastapi_extra-0.3.6 → fastapi_extra-0.4.0}/fastapi_extra/types.py +0 -0
- {fastapi_extra-0.3.6 → fastapi_extra-0.4.0}/fastapi_extra/urlparse.pyi +0 -0
- {fastapi_extra-0.3.6 → fastapi_extra-0.4.0}/fastapi_extra/utils.py +0 -0
- {fastapi_extra-0.3.6 → fastapi_extra-0.4.0}/fastapi_extra.egg-info/SOURCES.txt +0 -0
- {fastapi_extra-0.3.6 → fastapi_extra-0.4.0}/fastapi_extra.egg-info/dependency_links.txt +0 -0
- {fastapi_extra-0.3.6 → fastapi_extra-0.4.0}/fastapi_extra.egg-info/requires.txt +0 -0
- {fastapi_extra-0.3.6 → fastapi_extra-0.4.0}/fastapi_extra.egg-info/top_level.txt +0 -0
- {fastapi_extra-0.3.6 → fastapi_extra-0.4.0}/pyproject.toml +0 -0
- {fastapi_extra-0.3.6 → fastapi_extra-0.4.0}/setup.cfg +0 -0
|
@@ -2,13 +2,18 @@ __author__ = "ziyan.yin"
|
|
|
2
2
|
__date__ = "2025-01-12"
|
|
3
3
|
|
|
4
4
|
from contextvars import ContextVar
|
|
5
|
-
from typing import Any, Generic, TypeVar
|
|
5
|
+
from typing import Any, Generic, Sequence, TypeVar
|
|
6
|
+
|
|
7
|
+
from sqlalchemy import ColumnExpressionArgument
|
|
8
|
+
from sqlmodel import select
|
|
6
9
|
|
|
7
10
|
from fastapi_extra.database.model import SQLModel
|
|
8
11
|
from fastapi_extra.database.session import AsyncSession, DefaultSession
|
|
9
12
|
from fastapi_extra.dependency import AbstractService
|
|
10
13
|
|
|
11
14
|
Model = TypeVar("Model", bound=SQLModel)
|
|
15
|
+
ID = int | str
|
|
16
|
+
PK = ID | tuple[ID] | dict[str, ID]
|
|
12
17
|
|
|
13
18
|
|
|
14
19
|
class ModelService(AbstractService, Generic[Model], abstract=True):
|
|
@@ -40,14 +45,24 @@ class ModelService(AbstractService, Generic[Model], abstract=True):
|
|
|
40
45
|
assert _session is not None, "Session is not initialized"
|
|
41
46
|
return _session
|
|
42
47
|
|
|
43
|
-
async def get(self, ident:
|
|
48
|
+
async def get(self, ident: PK, **kwargs: Any) -> Model | None:
|
|
44
49
|
return await self.session.get(self.__model__, ident, **kwargs)
|
|
50
|
+
|
|
51
|
+
async def get_list(self, *clause: ColumnExpressionArgument[bool] | bool) -> Sequence[Model]:
|
|
52
|
+
return (await self.session.exec(select(self.__model__).where(*clause))).all()
|
|
45
53
|
|
|
46
54
|
async def create_model(self, **kwargs: Any) -> Model:
|
|
47
55
|
model = self.__model__.model_validate(kwargs)
|
|
48
56
|
self.session.add(model)
|
|
49
57
|
await self.session.flush()
|
|
50
58
|
return model
|
|
59
|
+
|
|
60
|
+
async def update_model(self, model: Model, **kwargs: Any) -> Model:
|
|
61
|
+
for key, value in kwargs.items():
|
|
62
|
+
if key in model.__fields__:
|
|
63
|
+
setattr(model, key, value)
|
|
64
|
+
await self.session.flush()
|
|
65
|
+
return model
|
|
51
66
|
|
|
52
67
|
async def delete(self, model: Model) -> None:
|
|
53
68
|
return await self.session.delete(model)
|
|
@@ -223,7 +223,7 @@ class APIResponse(JSONResponse):
|
|
|
223
223
|
|
|
224
224
|
def init_headers(self, headers: Mapping[str, str] | None = None) -> None:
|
|
225
225
|
self.raw_headers = [
|
|
226
|
-
(b"content-length",
|
|
226
|
+
(b"content-length", f"{len(self.body)}".encode("latin-1")),
|
|
227
227
|
(b"content-type", b"application/json; charset=utf-8"),
|
|
228
228
|
]
|
|
229
229
|
if headers:
|
|
@@ -234,8 +234,6 @@ class APIResponse(JSONResponse):
|
|
|
234
234
|
self.raw_headers.extend(raw_headers)
|
|
235
235
|
|
|
236
236
|
|
|
237
|
-
|
|
238
|
-
|
|
239
237
|
class APIError(Exception):
|
|
240
238
|
__slots__ = ("code", "message")
|
|
241
239
|
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|