Metadata-Version: 2.4
Name: DB-First
Version: 5.1.3
Summary: Web-framework independent CRUD tools for working with database via SQLAlchemy.
Author-email: Konstantin Fadeev <fadeev@legalact.pro>
License: MIT License
        
        Copyright (c) 2024 Konstantin Fadeev
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Project-URL: changelog, https://github.com/flask-pro/db-first/blob/master/CHANGES.md
Project-URL: repository, https://github.com/flask-pro/db-first
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: Implementation :: CPython
Classifier: Topic :: Database
Classifier: Topic :: Software Development :: Libraries
Requires-Python: >=3.11
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: SQLAlchemy>=2.0.0
Requires-Dist: marshmallow>=3.14.1
Provides-Extra: dev
Requires-Dist: build==1.4.0; extra == "dev"
Requires-Dist: psycopg[binary]==3.3.2; extra == "dev"
Requires-Dist: pre-commit==4.5.1; extra == "dev"
Requires-Dist: pytest==9.0.2; extra == "dev"
Requires-Dist: pytest-cov==7.0.0; extra == "dev"
Requires-Dist: python-dotenv==1.2.1; extra == "dev"
Requires-Dist: tox==4.34.1; extra == "dev"
Requires-Dist: twine==6.2.0; extra == "dev"
Dynamic: license-file

# DB-First

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

<!--TOC-->

- [DB-First](#db-first)
  - [Features](#features)
  - [Installation](#installation)
  - [Examples](#examples)
    - [Full example](#full-example)

<!--TOC-->

## 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`:

```shell
$ pip install -U db_first
```

## Examples

### Full example

```python
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.')
```
