Skip to main content

Model DML

Latest Release Pipeline Supported Python versions Imports: isort PyPI - Downloads License: MIT Black Open-Source/Open-to-Contribution Mattermost-model_dml

Extends your SQLAlchemy models with DML mixins.

Features

  • Four methods for DML operations: select, insert, update, delete.
  • Add those methods to models through mixin classes.
  • Support returning selected columns for all methods.

How to grow with model_dml

Why not use model_dml

  • No typed return values. It is theoretically possible to add annotation that will infer the return type of the methods such as select(id, name) would return a namedtuple with id as int and name as str. If you want to add this feature, you should read the method code and implement it yourself. Without model_dml abstraction, the type inference works better.

How not to use model_dml

This package is not meant to be used systematically as an installed package. You may want to copy a specific function that you need in your project instead of installing the whole package.

What kind of change may be needed

Let's take the select method. It is implemented to return a sequence of rows. If you want to order the rows by a column, you need to change the SQL statement construction.

  1. Find the source code of select method returning rows

    def _select_result(cls, where, columns, limit):
        stmt = sqlalchemy.select(*columns).where(*where).limit(limit)
        with cls.session_maker().begin() as session:
            return session.execute(stmt).all()
    
  2. Copy implementation in your code

    # Your code
    stmt = sqlalchemy.select(*columns).where(*where).limit(limit)
    with cls.session_maker().begin() as session:
        return session.execute(stmt).all()
    # Your code
    
  3. Update implementation accordingly to your needs

    ! Here session_maker should be the session factory of your project. Please see session_basics - using a sessionmaker.

    # Your code
    stmt = sqlalchemy.select(User.id, User.name).limit(10).order_by(User.name)
    with session_maker().begin() as session: 
        results = session.execute(stmt).all()
    # Your code
    

    Bravo :D

Get model_dml for your project

Requirements

  • Python 3.8+

Installation

pip install model_dml

Use model_dml in your project

Modify your sqlalchemy Base

To use model_dml, you need to create a custom base class for your models with a session_maker method.

What is a sessionmaker:

class Base(sqlalchemy.orm.DeclarativeBase):
    @classmethod
    def session_maker(cls) -> sqlalchemy.orm.sessionmaker:
        return < Namespace >.Session

This method exist to allow DML methods to access the sessionmaker without creating a new reference that would also need to be monkey patched. By returning the sessionmaker from the namespace, only <Namespace>.Session needs to be monkey patched.

Compose your model with what you need

class User(base, model_dml.Insert, model_dml.Update, model_dml.Delete):
    __tablename__ = "users"
    id: sqlalchemy.orm.Mapped[int] = sqlalchemy.orm.mapped_column(primary_key=True)
    name: sqlalchemy.orm.Mapped[str] = sqlalchemy.orm.mapped_column(
        sqlalchemy.String(30))

You can use model_dml.DML which is a helper class that composes all the mixins together.

class User(base, model_dml.DML):
    __tablename__ = "users"
    id: sqlalchemy.orm.Mapped[int] = sqlalchemy.orm.mapped_column(primary_key=True)
    name: sqlalchemy.orm.Mapped[str] = sqlalchemy.orm.mapped_column(
        sqlalchemy.String(30))

Use the DML methods

User.insert(dict(name="John"))
User.insert({'name': "Jane"})
user = User.insert({'name': "Jack"}, returning=[User.id, User.name])

Metadata

Release files for model-dml 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for model-dml 0.1.0
File Size Uploaded Provenance
model_dml-0.1.0.tar.gz 16.5 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for model-dml 0.1.0
File Interpreter ABI Platform
model_dml-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 24.7 kB

Release files / model_dml-0.1.0.tar.gz

Download URL model_dml-0.1.0.tar.gz
Size 16.5 kB
Tags Source
SHA-256 checksum
How to use checksums
0f41db859334acb32073066555a51812508f65dddea7beddb3c8d5d790325ad6
BLAKE2b-256 checksum
How to use checksums
3dab3d7d778333cba00f39413c8520a8c384c3b5c371efdb7b22c894a3a06fd2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.3

Release files / model_dml-0.1.0-py3-none-any.whl

Download URL model_dml-0.1.0-py3-none-any.whl
Size 8.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fc798d92d4914d3e601409e463c4c6569b4ae0d91c990444c0945895304561e0
BLAKE2b-256 checksum
How to use checksums
fac6570a75ae605b13ff399c129529fcb27b7a98a0b9913e26eb7f3bfa9f283c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.3

Security for model-dml 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page