Query SQLAlchemy models with MongoDB syntax.
Project description
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
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mqlalchemy-1.0.1.tar.gz.
File metadata
- Download URL: mqlalchemy-1.0.1.tar.gz
- Upload date:
- Size: 13.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f416d9ae03efb4ed0d290756aa9f2e81ae31bc38872a9b1f2524a415db67729b
|
|
| MD5 |
0b00f04c964251662da9b7389564e29c
|
|
| BLAKE2b-256 |
60ca65a94f7fe8fc18ac882d463fc2f64994aa35f27139e527cf8e244ca1fdee
|
File details
Details for the file mqlalchemy-1.0.1-py3-none-any.whl.
File metadata
- Download URL: mqlalchemy-1.0.1-py3-none-any.whl
- Upload date:
- Size: 13.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0c47f842cfbfa31b67faf881c5594c84fe7f15a40d40885800624446a2e011af
|
|
| MD5 |
bd2b1748f8b2011fbf28bdc7963bd609
|
|
| BLAKE2b-256 |
079a1fac410cca348f60c1c9983c0c7afaf99931d8ef1e736f55693dfb2b155c
|