CRUD tools for working with database via SQLAlchemy.
Project description
DB-First
CRUD tools for working with database via SQLAlchemy.
Features
- CreateMixin, ReadMixin, UpdateMixin, DeleteMixin for CRUD operation for database.
- Bulk methods for CreateMixin, ReadMixin, UpdateMixin, DeleteMixin.
- ReadMixin support paginated data from database.
- StatementMaker class for create query 'per-one-model'.
- Decorators
Validationfor validation and serialize/deserialize arguments, parameters and return data for function and methods. - Marshmallow (https://github.com/marshmallow-code/marshmallow) schemas for serialization input data.
- Marshmallow schemas for deserialization SQLAlchemy result object to
dict.
Installation
Recommended using the latest version of Python. DB-First supports Python 3.9 and newer.
Install and update using pip:
$ pip install -U db_first
Examples
Full example
from db_first import BaseCRUD
from db_first.base_model import ModelMixin
from db_first.decorators import Validation
from db_first.mixins import CreateMixin
from db_first.mixins import DeleteMixin
from db_first.mixins import ReadMixin
from db_first.mixins import UpdateMixin
from marshmallow import fields
from marshmallow import Schema
from sqlalchemy import create_engine
from sqlalchemy import Result
from sqlalchemy.exc import NoResultFound
from sqlalchemy.orm import declarative_base
from sqlalchemy.orm import Mapped
from sqlalchemy.orm import mapped_column
from sqlalchemy.orm import Session
engine = create_engine('sqlite://', echo=True, future=True)
session = Session(engine)
Base = declarative_base()
class Items(ModelMixin, Base):
__tablename__ = 'items'
data: Mapped[str] = mapped_column(comment='Data of item.')
Base.metadata.create_all(engine)
class IdSchema(Schema):
id = fields.UUID()
class SchemaOfCreate(Schema):
data = fields.String()
class SchemaOfUpdate(IdSchema, SchemaOfCreate):
"""Update item schema."""
class OutputSchema(SchemaOfUpdate):
created_at = fields.DateTime()
class ItemController(CreateMixin, ReadMixin, UpdateMixin, DeleteMixin, BaseCRUD):
class Meta:
session = session
model = Items
sortable = ['created_at']
@Validation.input(SchemaOfCreate)
@Validation.output(OutputSchema, serialize=True)
def create(self, **data) -> Result:
return super().create_object(**data)
@Validation.input(IdSchema, keys=['id'])
@Validation.output(OutputSchema, serialize=True)
def read(self, **data) -> Result:
return super().read_object(data['id'])
@Validation.input(SchemaOfUpdate)
@Validation.output(OutputSchema, serialize=True)
def update(self, **data) -> Result:
return super().update_object(**data)
@Validation.input(IdSchema, keys=['id'])
def delete(self, **data) -> None:
super().delete_object(**data)
if __name__ == '__main__':
item_controller = ItemController()
new_item = item_controller.create(data='first')
print('Item as dict:', new_item)
item = item_controller.read(id=new_item['id'])
print('Item as dict:', item)
updated_item = item_controller.update(id=new_item['id'], data='updated_first')
print('Item as dict:', updated_item)
item_controller.delete(id=new_item['id'])
try:
item = item_controller.read(id=new_item['id'])
except NoResultFound:
print('Item deleted:', item)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
db_first-4.1.0.tar.gz
(13.3 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
db_first-4.1.0-py3-none-any.whl
(10.7 kB
view details)
File details
Details for the file db_first-4.1.0.tar.gz.
File metadata
- Download URL: db_first-4.1.0.tar.gz
- Upload date:
- Size: 13.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8d59246796aada378af8f537e10f0d5ec33d3bd6bf850c30c3ecc16c9ae0a1eb
|
|
| MD5 |
90244145249fffaa0a9d58e56521804f
|
|
| BLAKE2b-256 |
e7110ec16a4b11f1195c5f97473def3b16c4ed1270a8121eaaeb615017dfefe2
|
File details
Details for the file db_first-4.1.0-py3-none-any.whl.
File metadata
- Download URL: db_first-4.1.0-py3-none-any.whl
- Upload date:
- Size: 10.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cb7de2565af900435c703c2c731627e300ce1ac7005d5e77040887e0d1bf3ae0
|
|
| MD5 |
01a555a89bbdcd94adb9286d60c80d65
|
|
| BLAKE2b-256 |
c0d8546c06e21443fb76c6a6e3ac94d06b7a7f37cc818ac1cda02b3fc8da2160
|