Skip to content

Commit ebe7228

Browse files
committed
improve CRUD-methods.
1 parent 7ab3bf3 commit ebe7228

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

src/db_first/mixins/crud.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def create(
5858

5959

6060
class ReadMixin:
61-
"""Read object from database.
61+
"""Read objects from database.
6262
6363
Mixin with get paginated result.
6464
@@ -77,10 +77,14 @@ class Meta:
7777
custom_controller = CustomController()
7878
```
7979
Required options:
80-
`input_schema_of_read` - marshmallow schema for serialize.
81-
`output_schema_of_read` - marshmallow schema for serialize.
80+
`session` - session object for connect to database.
81+
`model` - sqlalchemy model.
82+
`input_schema_of_read` - marshmallow schema for validating and deserialization input data.
83+
`output_schema_of_read` - marshmallow schema for serialization output data.
8284
8385
Optional options:
86+
`per_page` - items per page (default = 20).
87+
`max_per_page` - maximum items per page (default = 100).
8488
`filterable` - list of fields allowed for filtration.
8589
`interval_filterable` - list of fields allowed for filtration interval.
8690
`sortable` - list of fields allowed for sorting.
@@ -105,7 +109,7 @@ def _make_metadata(self, session: Session, page, per_page, statement):
105109
pages, total = self._calculate_items_per_page(session, statement, per_page)
106110
return {'pagination': {'page': page, 'per_page': per_page, 'pages': pages, 'total': total}}
107111

108-
def paginate(
112+
def _paginate(
109113
self,
110114
statement: Optional[Select],
111115
page: int = 1,
@@ -180,7 +184,7 @@ def read(
180184
if max_per_page is None:
181185
max_per_page = self._get_option_from_meta('max_per_page', 100)
182186

183-
items = self.paginate(
187+
items = self._paginate(
184188
statement=stmt,
185189
page=page,
186190
per_page=per_page,
@@ -194,7 +198,23 @@ def read(
194198

195199

196200
class UpdateMixin:
197-
"""Update object in database."""
201+
"""Update object in database.
202+
203+
This mixin supports the following options in the Meta class:
204+
```
205+
class CustomController(CreateMixin, BaseCRUD):
206+
class Meta:
207+
session = Session
208+
model = Model
209+
input_schema_of_update = InputSchema
210+
output_schema_of_update = OutputSchema
211+
212+
custom_controller = CustomController()
213+
```
214+
215+
`input_schema_of_update` - marshmallow schema for validating and deserialization input data.
216+
`output_schema_of_update` - marshmallow schema for serialization output data.
217+
"""
198218

199219
def update_object(self, id: Any, **kwargs) -> Result:
200220
"""If this method does not suit you, simply override it in your class."""

0 commit comments

Comments
 (0)