Skip to main content

Web-framework independent CRUD tools for working with database via SQLAlchemy.

Project description

DB-First

Web-framework independent CRUD tools for working with database via SQLAlchemy.

Features

  • DBAL - database access layer.
  • Actions templates.
  • Bulk methods for create, read, update and delete object from database.
  • Method of paginating data.
  • 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.
  • Datetime with UTC timezone validation in BaseSchema.

Installation

Recommended using the latest version of Python. DB-First supports Python 3.12 and newer.

Install and update using pip:

$ pip install -U db_first

Examples

Full example

from db_first.actions import BaseAction
from db_first.base_model import ModelMixin
from db_first.dbal import SqlaDBAL
from db_first.dbal.exceptions import DBALObjectNotFoundException
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 ItemsDBAL(SqlaDBAL[Items]):
    """Items DBAL."""


class ItemSchema(Schema):
    id = fields.UUID()
    data = fields.String()
    created_at = fields.DateTime()


class CreateItemAction(BaseAction):
    def validate(self) -> None:
        ItemSchema(exclude=['id', 'created_at']).load(self._data)

    def action(self) -> Items:
        return ItemsDBAL(self._session).create(**self._data)


class ReadItemAction(BaseAction):
    def validate(self) -> None:
        ItemSchema().load(self._data)

    def action(self) -> Items:
        return ItemsDBAL(self._session).read(**self._data)


class UpdateItemAction(BaseAction):
    def validate(self) -> None:
        ItemSchema(only=['id', 'data']).load(self._data)

    def action(self) -> Items:
        return ItemsDBAL(self._session).update(**self._data)


class DeleteItemAction(BaseAction):
    def validate(self) -> None:
        ItemSchema(only=['id']).load(self._data)

    def action(self) -> None:
        return ItemsDBAL(self._session).delete(**self._data)


if __name__ == '__main__':
    new_item = CreateItemAction(session, {'data': 'data'}).run()
    print('New item:', new_item)

    item = ReadItemAction(session, {'id': new_item.id}).run()
    print('Item:', item)

    updated_item = UpdateItemAction(session, {'id': new_item.id, 'data': 'updated_data'}).run()
    print('Updated item:', updated_item)

    DeleteItemAction(session, {'id': new_item.id}).run()
    try:
        item = ReadItemAction(session, {'id': new_item.id}).run()
    except DBALObjectNotFoundException:
        print('Deleted item.')

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-5.1.2.tar.gz (14.5 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-5.1.2-py3-none-any.whl (12.9 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for db_first-5.1.2.tar.gz
Algorithm Hash digest
SHA256 de1a14b32c6324b2acb655a8a11153746369c921881e518de74ed6d25bf0d4ec
MD5 0a6826e4410e9f76b6fb4a2ff3e06441
BLAKE2b-256 d28c2aa9422be5784b1a5b0ba416fb145c3761cc11fe4676436484f8437c3eb2

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for db_first-5.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 5ddd69c08a600ed55aeff3a2e93cc3d187f6dc4a0ea7eb5000393e038046e060
MD5 b55cc377d7ea05f05b42caf5cc8d2134
BLAKE2b-256 4693037fed0024d598c3d3f6a1e46fa5f546209cefefdb28eab608e6d915c319

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