Skip to main content

FastAPI ORM Extensions

Project description

FastApi ORM Extensions

PyPI - Version PyPI - Python Version


Table of Contents

About

This library provides preset base class for your tables and repositories.

Use TableBase like so:

from fastapi_orm_ext.table import TableBase
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column


class Table(TableBase, DeclarativeBase):
    # you can use this class in alembic's env.py file
    # to specify target_metadata for example
    __abstract__: bool = True


class User(Table, DeclarativeBase):
    name: Mapped[str] = mapped_column(nullable=False)
    surnname: Mapped[str | None] = mapped_column(nullable=True)

TableBase consists of four mixins:

  • NameConventionMixin: handle name convention;
  • TableNameMixin: takes model's class name and convert it to snake case, use this name while creating table in DB;
  • TimestampsMixin: handles when record was created and updated;
  • UUIDPrimaryKeyMixin: makes PK of UUID4 type.

Use Repository like so:

from fastapi_orm_ext.repository import RepositoryBase
from pydantic import BaseModel

from app.tables import User
# the variant to get async session
from app.utils import get_async_session()


class CreateUserSchema(BaseModel):
    name: str
    surname: str | None


class UserRepository(RepositoryBase[User]):
    # specify the model to interact with
    model = User
    # choose flush or commit
    auto_flush = True
    auto_commit = False

# initialize UserRepository
repo = UserRepository(async_session)
# create new record in user table with name Bob and surname NULL
data = CreateUserSchema(name="Bob")
await repo.create(data)

# get all records in User table - in our case only Bob
res: list[User] = await repo.all()
print(res)

To see what else RepositoryBase can do, visit the source code of interface RepositoryBase inheriting from

Installation

pip install fastapi-orm-ext

or

uv add fastapi-orm-ext

or

poetry add fastapi-orm-ext

etc

License

fastapi-orm-ext is distributed under the terms of the MIT license.

Contribution

Install repository:

https://github.com/pkozhem/fastapi-orm-ext.git

Create virtual environment, activate it and install dependencies:

uv venv
source .venv/bin/activate
uv sync

Create new branch from actual tag:

git checkout <tag>
git branch pull-<fix, feat, impr>: Short branch desc

Pull your changes and create pull request:

git pull origin <your_branch_name>

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

fastapi_orm_ext-0.0.3.tar.gz (22.7 kB view details)

Uploaded Source

Built Distribution

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

fastapi_orm_ext-0.0.3-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_orm_ext-0.0.3.tar.gz.

File metadata

  • Download URL: fastapi_orm_ext-0.0.3.tar.gz
  • Upload date:
  • Size: 22.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.28.1

File hashes

Hashes for fastapi_orm_ext-0.0.3.tar.gz
Algorithm Hash digest
SHA256 c9a858e3714ce9aa76114ceef49fa0e80433ae77a70952bd90f4bad4829457ab
MD5 5aa3bc8a5e3f7ed263bf75922cd0ff23
BLAKE2b-256 642f552cb9b459f1b16321687c03edbb69deeb8d37f60502d46e88087a826271

See more details on using hashes here.

File details

Details for the file fastapi_orm_ext-0.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_orm_ext-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 59088c1083f2959ccdadb09bf0a92c3177c0fd2ec678ef193b2404cadc2fc0fb
MD5 c35183d9f8de4d05e375eeff67dce184
BLAKE2b-256 ba20be0f733f1411a9f8188f54c83399c4a59a83c7317dd1dbb670bdd8b76f50

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