Skip to main content

Query SQLAlchemy models with MongoDB syntax.

Project description

Build Status Coverage Status 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, otherwise you’ll open the possibility of leaked passwords and all sorts of other scary stuff.

So, can I actually use this for a serious project?

Maybe? There’s some decent test coverage, but this certainly isn’t a very mature project yet.

I’ll be pretty active in supporting this, so if you are using this and run into problems, I should be pretty quick to fix them.

How fast is it?

I’m sure my actual syntax parsing is inefficient and has loads of room for improvement, but the time it takes to parse should be minimal compared to the actual database query, so this shouldn’t slow your queries down too much.

Supported Operators

  • $and

  • $or

  • $not

  • $nor

  • $in

  • $nin

  • $gt

  • $gte

  • $lt

  • $lte

  • $ne

  • $mod

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 = apply_mql_filters(db_session, Album, filters, whitelist)
matching_records = query.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

MQLAlchemy-0.2.0.zip (391.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

MQLAlchemy-0.2.0-py2.py3-none-any.whl (14.3 kB view details)

Uploaded Python 2Python 3

File details

Details for the file MQLAlchemy-0.2.0.zip.

File metadata

  • Download URL: MQLAlchemy-0.2.0.zip
  • Upload date:
  • Size: 391.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for MQLAlchemy-0.2.0.zip
Algorithm Hash digest
SHA256 279aaecaf867f91ab6381e3602ea2d5927c27ca4af6496a045f49499f60368c6
MD5 d1b102f1a67c64bfad6f8b84c312bb01
BLAKE2b-256 11ee6f41bb280e3e81fc43a6ad13a0593b0f7eb94445dd37f3f73dadf51dd833

See more details on using hashes here.

File details

Details for the file MQLAlchemy-0.2.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for MQLAlchemy-0.2.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 0a96a359646ee5e66db05409768fd1e2a02269af52835166c3875e0fbf1950e3
MD5 b11604950de8957a3acaab00e5aecaf0
BLAKE2b-256 fbfa60465750f90d647911fd948c11f702dc20617075e8fad08047215e2cd726

See more details on using hashes here.

Supported by

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