Metadata-Version: 2.4
Name: MQLAlchemy
Version: 1.0.0
Summary: Query SQLAlchemy models with MongoDB syntax.
Author-email: Nick Repole <n.repole@gmail.com>
License: MIT
Project-URL: Homepage, https://github.com/repole/mqlalchemy
Project-URL: Bug Reports, https://github.com/repole/mqlalchemy/issues
Project-URL: Source, https://github.com/repole/mqlalchemy
Project-URL: Documentation, https://mqlalchemy.readthedocs.io/
Project-URL: Changelog, https://github.com/repole/mqlalchemy/blob/master/CHANGELOG.rst
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Requires-Python: >=3.8
Description-Content-Type: text/x-rst
Requires-Dist: sqlalchemy>=2.0
Provides-Extra: test
Requires-Dist: coverage[toml]; extra == "test"
Provides-Extra: docs
Requires-Dist: sphinx>=8.1; extra == "docs"
Requires-Dist: sphinx-rtd-theme; extra == "docs"
Requires-Dist: sphinx-autodoc-typehints; extra == "docs"
Provides-Extra: dev
Requires-Dist: mqlalchemy[docs,test]; extra == "dev"

MQLAlchemy
==========

|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
--------

.. code:: python

    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

.. |Build Status| image:: https://github.com/repole/mqlalchemy/actions/workflows/ci-cd.yml/badge.svg
   :target: https://github.com/repole/mqlalchemy/actions/workflows/ci-cd.yml

.. |Coverage| image:: https://coveralls.io/repos/github/repole/mqlalchemy/badge.svg?branch=master
   :target: https://coveralls.io/github/repole/mqlalchemy?branch=master

.. |Docs| image:: https://readthedocs.org/projects/mqlalchemy/badge/?version=latest
   :target: http://mqlalchemy.readthedocs.org/en/latest/
