Skip to main content

A base manager class to handle CRUD on SQLAlchemy models.

Project description

A fully typed generic manager class to easily create CRUD operations for SQLAlchemy models. Designed to use in FastAPI projects.

Table of content

  1. Quick start
  2. Pagination

Quick start

Assume you have an SQLAlchemy model User and you want to perform CRUD on this model. You can simply create a manager for the model like this:

from sqlalchemy_manager import Manager

from .models import User


class UserManager(Manager[User]):
    pass

Manager is a generic class, you need to pass an SQLAlchemy model as it's type (Manager[User]) to configure the manager to operate the model.

By passing an SQLAlchemy model as manager's type you will also get type hints and autocompletion in your IDE.

That's it. You can now use the manager to do CRUD operations. You need to pass an SQlAlchemy session to a method as first argument. It can be Session or AsyncSession.

UserManager.create(session, User(firstname="Bob"))
UserManager.get(session, id=1)
UserManager.delete(session, id=1)

Manager

Manager class contains general CRUD methods alongside some extra methods such as get_or_create and search.

List of all methods:

- get
- create
- delete
- get_or_create
- search
- update

It is also has async methods, simple add async_ to a method name, e.g. async_create, async_delete etc.

Manager can accept either an SQLAlchemy model instance or a pydantic model instance.

from sqlalchemy_manager import Manager
from pydantic import BaseModel

from app.db import session
from app.managers import UserManager


class User(BaseModel):
    firstname: str


user = User(firstname="Bob")
UserManager.create(session, user)  # ok

Searching

Manager has search and async_search methods. It accepts search params as a pydantic model called Params. Each manager has its own search params model defined inside the class.

from sqlalchemy_manager import Manager
from pydantic import BaseModel

from .models import User


class UserManager(Manager[User]):
    class Params(BaseModel):
        age: int
        gender: str

Now search and async_search of UserManager will accept only age and gender params by validating them using the Params model.

You can pass either a dict or manager's Params object.

UserManager.search(session, **{'age': 10, 'gender': 'male'})  # okay
UserManager.search(session, UserManager.Params(age=10, gender='male'))  # okay

UserManager.search(session, **{'age': 10, 'name': 'Bob'})  # validation error

Pagination

You usually need a pagination when do search. Manager comes with simple builtin pagination for search and async_search methods.

These methods accept page argument to return a limited set of items belonging to the page.

In fastapi_manager.pagination you can find Paginator and Pagination classes.

Paginator

Paginator is responsible for doing the pagination and is used by manager's search and async_search methods. It does an offset limit pagination under the hood, and operates with page and per_page properties. Its main method paginate returns Pagination object which tells the structure of the pagination.

It has two properties: per_page = 25 and order_by = 'id' that can be customized.

You can customize it by inherit the Paginator and override these params in your own class:

from sqlalchemy_manager import Manager, Paginator


class CustomPaginator(Paginator):
    per_page = 100
    order_by = 'user_id'


class UserManager(Manager[User]):
    paginator_class = CustomPaginator

Pagination model

Pagination is a pydantic model that describes the structure of the pagination object to be returned.

class Pagination(BaseModel):
    page: int
    results: Union[Sequence, List]
    total: int
    has_prev: bool
    has_next: bool

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

sqlalchemy_orm_manager-0.0.2.tar.gz (18.0 kB view details)

Uploaded Source

Built Distribution

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

sqlalchemy_orm_manager-0.0.2-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file sqlalchemy_orm_manager-0.0.2.tar.gz.

File metadata

  • Download URL: sqlalchemy_orm_manager-0.0.2.tar.gz
  • Upload date:
  • Size: 18.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.4

File hashes

Hashes for sqlalchemy_orm_manager-0.0.2.tar.gz
Algorithm Hash digest
SHA256 9af20bda5e4dc32b3b19e47b4bfc6971592f510cdcdcfc71559b7ffb373bb9e2
MD5 0cb6392a38aa5f5648c1544761e61545
BLAKE2b-256 d17ba9c8315f12659879727c1bf044af8ad9719ecc834750d7260138f099bace

See more details on using hashes here.

File details

Details for the file sqlalchemy_orm_manager-0.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for sqlalchemy_orm_manager-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e38fee8883d63e1fe3ec70f18a3bb15b4f4e529d27d0cdd0720291d64330088d
MD5 bade2a77c47d4993f72e2bd1dfe272a8
BLAKE2b-256 b24256afada0e1abeedf8d54c214db8c24d96cb081ea1fca73d47cfdece8d98d

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