Skip to content

SQLAlchemy load option that loads from persisted session instances.

License

Notifications You must be signed in to change notification settings

jvllmr/sqlalchemy-sessionload

Repository files navigation

sqlalchemy-sessionload

PyPI - Downloads GitHub license Codecov Routine Checks

SQLAlchemy load option that loads from persisted session instances.

Supports SQLAlchemy 1.4

Basic usage

SessionLoad is available for basic queries and relationship loading

Filters and Order By constructs are also supported. If you miss something you are invited to contribute.

For installation the plugin has to be registered:

from sqlalchemy_sessionload import SQLAlchemySessionLoad

Session = sessionmaker(...)
SQLAlchemySessionLoad(Session)

Simple Query

from sqlalchemy_sessionload import SessionLoad
from project.model import Message

# assignment is needed
# otherwise instances are not saved in session
all_messages = session.query(Message).all()

session_messages = session.query(Message).options(SessionLoad(Message)).all()

Load relationship

Joined loading is currently only available with subqueryload.

from sqlalchemy_sessionload import SessionRelationshipLoad
from project.model import Message, User
import sqlalchemy.orm as sa_orm

# assignment is needed
# otherwise instances are not saved in session
all_users = session.query(User).all()


# users connected to messages are now loaded from session
session_messages = (
    session.query(Message)
    .options(sa_orm.subqueryload(Message.user), SessionRelationshipLoad(Message.user))
    .all()
)

Benchmark

A benchmark is available here