BaseCRUD
BaseCRUD
Bases: Generic[ModelClass]
Simple class providing base CRUD operations on given Model
__init__(model, db)
Class constructor
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
Type[ModelClass]
|
The model onto perform operations |
required |
db |
Session
|
Database engine Session |
required |
bulk_create(instances)
Insert multiple instances in the database
Parameters:
Name | Type | Description | Default |
---|---|---|---|
instances |
List[ModelClass]
|
List of instances to be added into the Database |
required |
Returns:
Type | Description |
---|---|
List[ModelClass]
|
List[ModelClass]: the inserted instances |
create(instance)
Persists an item into the Database
Parameters:
Name | Type | Description | Default |
---|---|---|---|
instance |
SQLModel
|
model to persist |
required |
Returns:
Type | Description |
---|---|
Optional[SQLModel]
|
Optional[SQLModel]: the created instance itself |
delete(instance)
Removes an instance from the database
Parameters:
Name | Type | Description | Default |
---|---|---|---|
instance |
Type[ModelClass]
|
the instance to remove |
required |
Returns:
Type | Description |
---|---|
Type[ModelClass]
|
Type[ModelClass]: the instance removed |
filter(offset=0, limit=100, *args, **kwargs)
Gets one or more instances from the database, filtering them by one or more column
Parameters:
Name | Type | Description | Default |
---|---|---|---|
offset |
Optional[int]
|
specifies the point from where to start returning data |
0
|
limit |
Optional[int]
|
parameter that limits the number of results |
100
|
*args |
BinaryExpression
|
filter args |
()
|
**kwargs |
Any
|
filter args |
{}
|
Returns:
Name | Type | Description |
---|---|---|
List |
List[Any]
|
List of retrieved items from the database |
get(*args, **kwargs)
Gets a single record from the database
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*args |
BinaryExpression
|
filter args |
()
|
**kwargs |
Any
|
filter args |
{}
|
Returns:
Type | Description |
---|---|
Optional[ModelClass]
|
Optional[ModelClass]: the retrieved instance or None |
get_all()
Gets all instances of given module from the Database
Returns:
Name | Type | Description |
---|---|---|
List |
Optional[List[ModelClass]]
|
List of all instances of that model in the database. |
update(instance)
Updates a record into database. It is equal to create data process, so it will call that method
Parameters:
Name | Type | Description | Default |
---|---|---|---|
instance |
SQLModel
|
the instance to update |
required |
Returns:
Type | Description |
---|---|
Optional[SQLModel]
|
Optional[SQLModel]: the updated instance |