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.
  • CRUD methods for create, read, update and delete object from database.
  • 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 for pagination.
  • 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.base_model import ModelMixin
from db_first.dbal import SqlaDBAL
from db_first.dbal.exceptions import DBALObjectNotFoundException
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."""


if __name__ == '__main__':
    new_item = ItemsDBAL(session).create(data='data')
    print('=>', 'New item:', new_item)

    item = ItemsDBAL(session).read(id=new_item.id)
    print('=>', 'Item:', item)

    updated_item = ItemsDBAL(session).update(id=new_item.id, data='updated_data')
    print('=>', 'Updated item:', updated_item)

    ItemsDBAL(session).delete(id=new_item.id)
    try:
        ItemsDBAL(session).read(id=new_item.id)
    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.3.1.tar.gz (14.2 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.3.1-py3-none-any.whl (11.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: db_first-5.3.1.tar.gz
  • Upload date:
  • Size: 14.2 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.3.1.tar.gz
Algorithm Hash digest
SHA256 6be7739ef566a2c88a4b4b350d02d008edd0cc8136e1dd77b4dead68d4ca2430
MD5 dd651d3da376a72db9e73a851de4ea4d
BLAKE2b-256 ad00191519f7b52c1865b765b10b2e7fca044e9aa67098ea0f517f0e5b9e3daf

See more details on using hashes here.

File details

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

File metadata

  • Download URL: db_first-5.3.1-py3-none-any.whl
  • Upload date:
  • Size: 11.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.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 19e01048eb8442bb9ad2cf8b2dd0b5d68069a12e475e9b3b15f9eeb26a24f29c
MD5 539d45cfc43e4167caf2770019448087
BLAKE2b-256 da0b1e7800485168fdb59488a85821c5409d3bda88b4a8e3c422d8eb0073b0fb

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