Skip to main content

Advanced query manager for SQLAlchemy

Project description

Managers for SQLAlchemy.

Manager for model, methods were added during runtime to query.

Installation

    $ [sudo] pip install advanced-sqlalchemy-manager

Documentation

ManagedQuery

Managed query that replaces sqlalchemy.query class

Example:

from sqlalchemy.orm import sessionmaker
from sqlalchemy import create_engine
from sqlalchemy.orm import declarative_base
from sqlalchemy import Column, Integer, String
from alchmanager import ManagedQuery, BaseQueryManager

engine = create_engine('sqlite:///:memory:')
session_factory = sessionmaker(query_cls=ManagedQuery, bind=engine)
session = session_factory()

Base = declarative_base()


class Person(Base):
    __tablename__ = 'persons'

    id = Column(Integer, primary_key=True)
    name = Column(String(50), nullable=False)

    age = Column(Integer, nullable=False)


class PersonQueryManager(BaseQueryManager):
    __model__ = Person

    @staticmethod
    def older_than(query: ManagedQuery, age: int) -> ManagedQuery:
        return query.filter(Person.age > age)

    @staticmethod
    def younger_than(query: ManagedQuery, age: int):
        return query.filter(Person.age < age)

    @staticmethod
    def first_of_exact_age(query: ManagedQuery, age: int):
        return query.filter(Person.age == age).first()


filtered_persons = session.query(Person).older_than(30).filter(Person.name.contains('_')).younger_than(60).all()
person_25_years_old = session.query(Person).first_of_exact_age(25)

ManagedSession

Managed session. Use decorator load_manager() to register query managers into session. Registered that way session managers will be usable on any model.

Example:

from sqlalchemy.orm import sessionmaker
from sqlalchemy import create_engine
from sqlalchemy.orm import declarative_base
from sqlalchemy import Column, Integer, String, Boolean
from alchmanager import ManagedQuery, ManagedSession, BaseQueryManager

engine = create_engine('sqlite:///:memory:')
session_factory = sessionmaker(class_=ManagedSession, bind=engine)
session = session_factory()

Base = declarative_base()


class Person(Base):
    __tablename__ = 'persons'

    id = Column(Integer, primary_key=True)
    name = Column(String(50), nullable=False)

    age = Column(Integer, nullable=False)


class Book(Base):
    __tablename__ = 'books'

    id = Column(Integer, primary_key=True)
    title = Column(String)

    is_public = Column(Boolean, nullable=False, default=False)


@session.load_manager()
class BookQueryManager(BaseQueryManager):
    @staticmethod
    def is_book_public(query: ManagedQuery) -> ManagedQuery:
        return query.filter(Book.is_public.is_(True))


count_of_filtered_books = session.query(Book).is_book_public().count()

# This will produce broken query because is_public does not exists in Person model
persons = session.query(Person).is_book_public().count()

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

advanced-sqlalchemy-manager-0.1.1.tar.gz (16.2 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file advanced-sqlalchemy-manager-0.1.1.tar.gz.

File metadata

  • Download URL: advanced-sqlalchemy-manager-0.1.1.tar.gz
  • Upload date:
  • Size: 16.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.2

File hashes

Hashes for advanced-sqlalchemy-manager-0.1.1.tar.gz
Algorithm Hash digest
SHA256 832ef670764249bab9a341e0aa938a7f41e80b866f5fe98dc9c0783a99ade84c
MD5 c7182dcc81e590c8b2f3de7c11d5058e
BLAKE2b-256 ea0daedb18c0d6b8f948eb9bebd39db256b09291381aaab1cd2f6f5a06b06760

See more details on using hashes here.

File details

Details for the file advanced_sqlalchemy_manager-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: advanced_sqlalchemy_manager-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 16.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.2

File hashes

Hashes for advanced_sqlalchemy_manager-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8166b0d786804c0dba20efa3a3229cf5e70b0e179dd3f642779be6f9ff3b51cd
MD5 1a23d6b903bc91d27010c21ec9c9905c
BLAKE2b-256 40dd7c4dfd6ffafe710ca16ac78aad0d77e745b6a4053e031556126f7ae3c3ee

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page