Skip to main content

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.
  • ReadMixin support paginated data from database.
  • StatementMaker class for create query 'per-one-model'.
  • 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 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 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 InputSchemaOfRead(Schema):
    id = fields.UUID()


class OutputSchema(InputSchemaOfUpdate):
    created_at = fields.DateTime()


class ItemController(CreateMixin, ReadMixin, UpdateMixin, DeleteMixin, BaseCRUD):
    class Meta:
        session = session
        model = Items
        input_schema_of_create = InputSchemaOfCreate
        input_schema_of_update = InputSchemaOfUpdate
        output_schema_of_create = OutputSchema
        input_schema_of_read = InputSchemaOfRead
        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': 'first'}, deserialize=True)
    print('Item as object:', first_new_item)
    second_new_item = item.create({'data': 'second'}, deserialize=True, serialize=True)
    print('Item as dict:', second_new_item)

    first_item = item.read({'id': first_new_item.id})
    print('Item as object:', first_item)
    first_item = item.read({'id': first_new_item.id})
    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


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-3.0.0.tar.gz (12.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

db_first-3.0.0-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

File details

Details for the file db_first-3.0.0.tar.gz.

File metadata

  • Download URL: db_first-3.0.0.tar.gz
  • Upload date:
  • Size: 12.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.7

File hashes

Hashes for db_first-3.0.0.tar.gz
Algorithm Hash digest
SHA256 bc89118037369efdc6f1728526bdfa7e0e81ec7e8bcfabdf1595e85dd8304422
MD5 6fa75d86465a712b7e9304d4c8498576
BLAKE2b-256 a3e32b70c215166ac758c1026394c586dc64fc82727806d5fa03b0425f8aa9ee

See more details on using hashes here.

File details

Details for the file db_first-3.0.0-py3-none-any.whl.

File metadata

  • Download URL: db_first-3.0.0-py3-none-any.whl
  • Upload date:
  • Size: 9.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.7

File hashes

Hashes for db_first-3.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 794326a622fa147bbf52ebffb3de9b8fd5ab6b11dfa50f24a17b01841ab5aa08
MD5 678fbe95727657add34667a082b63f1d
BLAKE2b-256 e899d218aad332c43bfb6ae02f289a52fe7923cf87c11edc64c330cc4f4ef66a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page