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.
  • 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 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(deserialize=True, data='first')
    print('Item as object:', first_new_item)
    second_new_item = item.create(deserialize=True, data='second', 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.read(sort_created_at='desc')
    print('Items as objects:', items)
    items = item.read(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-2.1.0.tar.gz (11.9 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-2.1.0-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for db_first-2.1.0.tar.gz
Algorithm Hash digest
SHA256 85892f3843fd12f005f8f9c11505593c5c2fabe83177c8cf01a5f47fe58e6150
MD5 244e47d5c60f7942eb60bc7617b7c911
BLAKE2b-256 12ce7828110cb3869621d5bf225cabf7c7b8b9ac0f52e96a19ba450c9183359f

See more details on using hashes here.

File details

Details for the file DB_First-2.1.0-py3-none-any.whl.

File metadata

  • Download URL: DB_First-2.1.0-py3-none-any.whl
  • Upload date:
  • Size: 9.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for DB_First-2.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 89ffec58d56cf9569a8da8968ee28e3c0e9608319e9d936cb483ff12ab55e7e9
MD5 31f2c0fb925a958945a540d39b9df782
BLAKE2b-256 f7bd4499df17f2f04d7322d8a8fa35d63624fff52e90dd662297f58a2f5717e0

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