Skip to main content

FastAPI ORM Extensions

Project description

FastApi ORM Extensions

PyPI - Version PyPI - Python Version


Table of Contents

Installation

pip install fastapi-orm-ext

or

uv add fastapi-orm-ext

or

poetry add fastapi-orm-ext

etc

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):
    name: Mapped[str] = mapped_column(nullable=False)
    email: 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 async_session


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


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

    # there you can define your methods
    def get_by_email(self, email: str) -> User | None:
        return (
            await self.session.execute(
                statement=select(self.model).where(self.model.email == email),
            )
        ).scalar()

# initialize UserRepository
repo = UserRepository(async_session)
# create new record in users table
data = CreateUserSchema(name="Bob", email="bob@gmail.com")
await repo.create(data)

# get record in users table by Bob's email
res: list[User] = await repo.get_by_email("bob@gmail.com")
print(res)

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

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.6.tar.gz (24.5 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.6-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for fastapi_orm_ext-0.0.6.tar.gz
Algorithm Hash digest
SHA256 ba2d0445140953708ed26a53740dc5609dc3a9ede9d66c581cabe42c1f770e07
MD5 e37f1a0eef15dad5a9d767a3b1a6b9b2
BLAKE2b-256 32d5dead09ae26e72cff69d14b8dd129d9809aab58aa47ffe7880244459ff22d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastapi_orm_ext-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 1d01bb99635013638d87a073fdb89f6b367e0e137f174e0af545fcf5753c687d
MD5 3699967fbf3e2b712d312d16b64add95
BLAKE2b-256 006c555f50659aecc8bf72b8bc6e3b5766de69bb57b600a3edfa72fada7397da

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