fastapi-orm-ext


Namefastapi-orm-ext JSON
Version 0.1.0 PyPI version JSON
download
home_pageNone
SummaryFastAPI ORM Extensions
upload_time2025-08-03 18:17:54
maintainerNone
docs_urlNone
authorNone
requires_python>=3.13
licenseNone
keywords extensions fastapi orm
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # FastApi ORM Extensions

[![PyPI - Version](https://img.shields.io/pypi/v/fastapi-orm-ext.svg)](https://pypi.org/project/fastapi-orm-ext)
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/fastapi-orm-ext.svg)](https://pypi.org/project/fastapi-orm-ext)

-----

## Table of Contents
- [Installation](#installation)
- [About](#About)
- [License](#license)
- [Contribution](#contribution)


## Installation
```bash
pip install fastapi-orm-ext
```
or
```bash
uv add fastapi-orm-ext
```
or
```bash
poetry add fastapi-orm-ext
```
etc


## About
This library provides preset base class for your tables and repositories.
#### Use TableBase like so:
```python
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: handles 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.

If you don't need one or more of following mixins, create your own TableBase.

#### Use Repository like so:
```python
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](https://spdx.org/licenses/MIT.html) license.


## Contribution
Install repository:
```bash
https://github.com/pkozhem/fastapi-orm-ext.git
```

Create virtual environment, activate it and install dependencies:
```bash
uv venv
source .venv/bin/activate
uv sync
```

Create new branch from actual tag:
```bash
git checkout <tag>
git branch pull-<fix, feat, impr>: Short branch desc
```

Pull your changes and create pull request:
```bash
git pull origin <your_branch_name>
```

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "fastapi-orm-ext",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.13",
    "maintainer_email": null,
    "keywords": "extensions, fastapi, orm",
    "author": null,
    "author_email": "Pavel Kozhemjachenko <pkozhem@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/43/89/2110fe320a74d1b1a9c288e7dc639bf044f2e03d0e864ca3ca8d262b95f2/fastapi_orm_ext-0.1.0.tar.gz",
    "platform": null,
    "description": "# FastApi ORM Extensions\n\n[![PyPI - Version](https://img.shields.io/pypi/v/fastapi-orm-ext.svg)](https://pypi.org/project/fastapi-orm-ext)\n[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/fastapi-orm-ext.svg)](https://pypi.org/project/fastapi-orm-ext)\n\n-----\n\n## Table of Contents\n- [Installation](#installation)\n- [About](#About)\n- [License](#license)\n- [Contribution](#contribution)\n\n\n## Installation\n```bash\npip install fastapi-orm-ext\n```\nor\n```bash\nuv add fastapi-orm-ext\n```\nor\n```bash\npoetry add fastapi-orm-ext\n```\netc\n\n\n## About\nThis library provides preset base class for your tables and repositories.\n#### Use TableBase like so:\n```python\nfrom fastapi_orm_ext.table import TableBase\nfrom sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column\n\n\nclass Table(TableBase, DeclarativeBase):\n    # you can use this class in alembic's env.py file\n    # to specify target_metadata for example\n    __abstract__: bool = True\n\n\nclass User(Table):\n    name: Mapped[str] = mapped_column(nullable=False)\n    email: Mapped[str | None] = mapped_column(nullable=True)\n```\n\nTableBase consists of four mixins:\n- NameConventionMixin: handles name convention;\n- TableNameMixin: takes model's class name and convert it to snake case, use this name while creating table in DB;\n- TimestampsMixin: handles when record was created and updated;\n- UUIDPrimaryKeyMixin: makes PK of UUID4 type.\n\nIf you don't need one or more of following mixins, create your own TableBase.\n\n#### Use Repository like so:\n```python\nfrom fastapi_orm_ext.repository import RepositoryBase\nfrom pydantic import BaseModel\n\nfrom app.tables import User\n# the variant to get async session\nfrom app.utils import async_session\n\n\nclass CreateUserSchema(BaseModel):\n    name: str\n    email: str | None\n\n\nclass UserRepository(RepositoryBase[User]):\n    # specify the model to interact with\n    model = User\n    # choose flush or commit\n    auto_flush = True\n    auto_commit = False\n\n    # there you can define your methods\n    def get_by_email(self, email: str) -> User | None:\n        return (\n            await self.session.execute(\n                statement=select(self.model).where(self.model.email == email),\n            )\n        ).scalar()\n\n# initialize UserRepository\nrepo = UserRepository(async_session)\n# create new record in users table\ndata = CreateUserSchema(name=\"Bob\", email=\"bob@gmail.com\")\nawait repo.create(data)\n\n# get record in users table by Bob's email\nres: list[User] = await repo.get_by_email(\"bob@gmail.com\")\nprint(res)\n```\n\nTo see what else RepositoryBase can do, visit the source code of interface RepositoryBase inheriting from\n\n\n## License\n`fastapi-orm-ext` is distributed under the terms of the [MIT](https://spdx.org/licenses/MIT.html) license.\n\n\n## Contribution\nInstall repository:\n```bash\nhttps://github.com/pkozhem/fastapi-orm-ext.git\n```\n\nCreate virtual environment, activate it and install dependencies:\n```bash\nuv venv\nsource .venv/bin/activate\nuv sync\n```\n\nCreate new branch from actual tag:\n```bash\ngit checkout <tag>\ngit branch pull-<fix, feat, impr>: Short branch desc\n```\n\nPull your changes and create pull request:\n```bash\ngit pull origin <your_branch_name>\n```\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "FastAPI ORM Extensions",
    "version": "0.1.0",
    "project_urls": {
        "Documentation": "https://github.com/pkozhem/fastapi-orm-ext#readme",
        "Issues": "https://github.com/pkozhem/fastapi-orm-ext/issues",
        "Repository": "https://github.com/pkozhem/fastapi-orm-ext",
        "Source": "https://github.com/pkozhem/fastapi-orm-ext"
    },
    "split_keywords": [
        "extensions",
        " fastapi",
        " orm"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "5688feb1fdfd398343c38bffe530c63c9453eff11e991a04aa229a7b37f18149",
                "md5": "0ebc41f020ff28b5545461f5b326bff6",
                "sha256": "c19a02307bd4eadfdbe57f63f000c013b320aaefa77bfe950bb45559da2dcd1b"
            },
            "downloads": -1,
            "filename": "fastapi_orm_ext-0.1.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "0ebc41f020ff28b5545461f5b326bff6",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.13",
            "size": 8584,
            "upload_time": "2025-08-03T18:17:55",
            "upload_time_iso_8601": "2025-08-03T18:17:55.870118Z",
            "url": "https://files.pythonhosted.org/packages/56/88/feb1fdfd398343c38bffe530c63c9453eff11e991a04aa229a7b37f18149/fastapi_orm_ext-0.1.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "43892110fe320a74d1b1a9c288e7dc639bf044f2e03d0e864ca3ca8d262b95f2",
                "md5": "28a4d5cc50008e383fc83638c7db30c2",
                "sha256": "a6b3732198f27547a5a3152656094cb812d49cc92e96f6e1b046e30156963155"
            },
            "downloads": -1,
            "filename": "fastapi_orm_ext-0.1.0.tar.gz",
            "has_sig": false,
            "md5_digest": "28a4d5cc50008e383fc83638c7db30c2",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.13",
            "size": 24553,
            "upload_time": "2025-08-03T18:17:54",
            "upload_time_iso_8601": "2025-08-03T18:17:54.264122Z",
            "url": "https://files.pythonhosted.org/packages/43/89/2110fe320a74d1b1a9c288e7dc639bf044f2e03d0e864ca3ca8d262b95f2/fastapi_orm_ext-0.1.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-08-03 18:17:54",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "pkozhem",
    "github_project": "fastapi-orm-ext#readme",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "fastapi-orm-ext"
}
        
Elapsed time: 1.87003s