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.
- PaginateMixin for get paginated data from database.
- QueryMaker class for create query 'per-one-model'.
- Marshmallow (https://github.com/marshmallow-code/marshmallow) schemas for validating 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 uuid import UUID
from db_first import BaseCRUD
from db_first.base_model import ModelMixin
from db_first.mixins import CreateMixin
from db_first.mixins import DeleteMixin
from db_first.mixins import PaginationMixin
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.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 InputSchemaOfCreate(Schema):
data = fields.String()
class InputSchemaOfUpdate(InputSchemaOfCreate):
id = fields.UUID()
class OutputSchema(InputSchemaOfUpdate):
created_at = fields.DateTime()
class ItemController(CreateMixin, ReadMixin, UpdateMixin, DeleteMixin, PaginationMixin, BaseCRUD):
class Meta:
session = session
model = Items
input_schema_of_create = InputSchemaOfCreate
input_schema_of_update = InputSchemaOfUpdate
output_schema_of_create = OutputSchema
output_schema_of_read = OutputSchema
output_schema_of_update = OutputSchema
schema_of_paginate = OutputSchema
sortable = ['created_at']
if __name__ == '__main__':
item = ItemController()
first_new_item = item.create(data={'data': 'first'})
print('Item as object:', first_new_item)
second_new_item = item.create(data={'data': 'second'}, serialize=True)
print('Item as dict:', second_new_item)
first_item = item.read(first_new_item.id)
print('Item as object:', first_item)
first_item = item.read(first_new_item.id, serialize=True)
print('Item as dict:', first_item)
updated_first_item = item.update(data={'id': first_new_item.id, 'data': 'updated_first'})
print('Item as object:', updated_first_item)
updated_second_item = item.update(
data={'id': UUID(second_new_item['id']), 'data': 'updated_second'}, serialize=True
)
print('Item as dict:', updated_second_item)
items = item.paginate(sort_created_at='desc')
print('Items as objects:', items)
items = item.paginate(sort_created_at='desc', serialize=True)
print('Items as dicts:', items)
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-2.0.0.tar.gz
(11.8 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
File details
Details for the file db_first-2.0.0.tar.gz.
File metadata
- Download URL: db_first-2.0.0.tar.gz
- Upload date:
- Size: 11.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ddd92b46d7e03426ea74358bc6ad55a76602837588e9c30d8f4d321b0599638b
|
|
| MD5 |
9eeb01e56dc149d34de552947010c55b
|
|
| BLAKE2b-256 |
03f1661f330b2d2936a65d81d751232c4c4199f9c32bed2b432d64ec6df9bf89
|
File details
Details for the file DB_First-2.0.0-py3-none-any.whl.
File metadata
- Download URL: DB_First-2.0.0-py3-none-any.whl
- Upload date:
- Size: 9.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
59f438bd33365d04479f08c7fe07aa94ca1c97077e50e3d3200a7d1a3930657a
|
|
| MD5 |
310660cd19697d7fa9f729c33ff75f67
|
|
| BLAKE2b-256 |
2246c3aa09d97a56392bf4e6c789d6d190b3fafc2decd223972176b6117f9259
|