Skip to main content

Build Status Coverage Docs

Query SQLAlchemy models using MongoDB style syntax.

Why?

The need arose for me to be able to pass complex database filters from client side JavaScript to a Python server. I started building some JSON style syntax to do so, then realized such a thing already existed. I’ve never seriously used MongoDB, but the syntax for querying lends itself pretty perfectly to this use case.

That sounds pretty dangerous…

It can be. When using this with any sort of user input, you’ll want to pass in a whitelist of attributes that are ok to query, as well as any required filters for each model class, otherwise you’ll open the possibility of leaked passwords and all sorts of other scary stuff.

How fast is it?

The time it takes to parse should be minimal compared to the actual database query, so this shouldn’t slow your queries down noticably.

Supported Operators

  • $and

  • $or

  • $not

  • $nor

  • $in

  • $nin

  • $gt

  • $gte

  • $lt

  • $lte

  • $ne

  • $mod

  • $exists

Custom operators added for convenience:

  • $eq - Explicit equality check.

  • $like - Search a text field for the given value.

Not yet supported, but would like to add:

  • Index based relation queries. Album.tracks.0.track_id won’t work.

  • $regex

Examples

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from mqlalchemy import apply_mql_filters
from myapp.mymodels import Album

# get your sqlalchemy db session here
db_engine = create_engine("sqlite+pysqlite:///mydb.sqlite")
DBSession = sessionmaker(bind=db_engine)
db_session = DBSession()

# define which fields of Album are ok to query
whitelist = ["album_id", "artist.name", "tracks.playlists.name"]
# Find all albums that are either by Led Zeppelin or have a track
# that can be found on the "Grunge" playlist.
filters = {
    "$or": [
        {"tracks.playlists.name": "Grunge"},
        {"artist.name": "Led Zeppelin"}
    ]
}
query = select(Album)
query = apply_mql_filters(
    model_class=Album,
    query=select(Album),
    filters=filters,
    whitelist=whitelist)
matching_records = db_session.execute(query).scalars().all()

For more, please see the included tests, as they’re probably the easiest way to get an idea of how the library can be used.

Contributing

Submit a pull request and make sure to include an updated AUTHORS with your name along with an updated CHANGES.rst.

License

MIT

Release files for MQLAlchemy 1.0.1

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

Source distribution (sdist)

Source distribution for MQLAlchemy 1.0.1
File Size Uploaded
mqlalchemy-1.0.1.tar.gz 13.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for MQLAlchemy 1.0.1
File Interpreter ABI Platform
mqlalchemy-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 26.9 kB

Release files / mqlalchemy-1.0.1.tar.gz

Download URL mqlalchemy-1.0.1.tar.gz
Size 13.6 kB
Tags Source
SHA-256 checksum
How to use checksums
f416d9ae03efb4ed0d290756aa9f2e81ae31bc38872a9b1f2524a415db67729b
BLAKE2b-256 checksum
How to use checksums
60ca65a94f7fe8fc18ac882d463fc2f64994aa35f27139e527cf8e244ca1fdee
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.7

Release files / mqlalchemy-1.0.1-py3-none-any.whl

Download URL mqlalchemy-1.0.1-py3-none-any.whl
Size 13.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0c47f842cfbfa31b67faf881c5594c84fe7f15a40d40885800624446a2e011af
BLAKE2b-256 checksum
How to use checksums
079a1fac410cca348f60c1c9983c0c7afaf99931d8ef1e736f55693dfb2b155c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.7

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files

1.0.0

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.4

2 release files

0.1.3

2 release files

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