Base DB CRUDs
BaseCRUD
Base class that implements CRUD for the database using TortoiseORM.
Attributes:
Name | Type | Description |
---|---|---|
model |
Model
|
Tortoise Model on which CRUD operations are applied. |
schema |
Schema
|
PydanticModel generated from the Tortoise Model. |
Source code in ms_core/bases/base_crud.py
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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
|
create(payload, **kwargs)
async
classmethod
Implements the create operation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
payload
|
PydanticModel
|
The data to create the instance. |
required |
**kwargs
|
Additional arguments for the creation process. |
{}
|
Returns:
Type | Description |
---|---|
Schema
|
The created instance as a PydanticModel. |
Source code in ms_core/bases/base_crud.py
delete_by(**kwargs)
async
classmethod
Implements the delete operation.
Deletes an item based on the provided kwargs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
**kwargs
|
Search parameters to find the item to delete. |
{}
|
Returns:
Type | Description |
---|---|
bool
|
True if the item was successfully deleted, False otherwise. |
Source code in ms_core/bases/base_crud.py
filter_by(**kwargs)
async
classmethod
Exposes the filter method of Tortoise Model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
**kwargs
|
Criteria to filter the items. |
{}
|
Returns:
Type | Description |
---|---|
list[Schema] | None
|
A list of matching items or None if not found. |
Source code in ms_core/bases/base_crud.py
get_all(prefetch=False, limit=50, offset=0, **extra_filters)
async
classmethod
Bulk fetches items from the database.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prefetch
|
bool
|
Whether to prefetch related data. Defaults to False. |
False
|
limit
|
int
|
The number of items to fetch. Defaults to 50. |
50
|
offset
|
int
|
The offset from which to start fetching. Defaults to 0. |
0
|
**extra_filters
|
Additional filters to apply. |
{}
|
Returns:
Type | Description |
---|---|
list[Schema]
|
A list of fetched PydanticModels. Empty list if no items are found. |
Source code in ms_core/bases/base_crud.py
get_by(**kwargs)
async
classmethod
Fetches an item based on the provided kwargs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
**kwargs
|
Criteria to search for the item. |
{}
|
Returns:
Type | Description |
---|---|
Schema | None
|
The corresponding PydanticModel or None if not found. |
Source code in ms_core/bases/base_crud.py
get_by_id(id_)
async
classmethod
Fetches an item by its ID.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
id_
|
int
|
The ID of the item. |
required |
Returns:
Type | Description |
---|---|
Schema | None
|
The corresponding PydanticModel or None if not found. |
Source code in ms_core/bases/base_crud.py
get_or_create(**kwargs)
async
classmethod
Implements the get or create operation.
If the item is not found, it will try to create it from the provided kwargs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
**kwargs
|
Arguments to search for and create the instance if not found. |
{}
|
Returns:
Type | Description |
---|---|
tuple[Schema, bool]
|
The found or created instance and a boolean indicating whether it was created. |
Raises:
Type | Description |
---|---|
IntegrityError
|
If creation fails. |
Source code in ms_core/bases/base_crud.py
update_by(payload, **kwargs)
async
classmethod
Implements the update operation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
payload
|
Schema | dict
|
The data to update the instance. |
required |
**kwargs
|
Criteria to find the item to be updated. |
{}
|
Returns:
Type | Description |
---|---|
Schema | None
|
The updated instance or None if not found. |
Source code in ms_core/bases/base_crud.py
I18nCRUD
Bases: BaseCRUD[Model, Schema]
Alternated CRUD for i18n models