Skip to main content

Convert query parameters from API urls to MongoDB queries !

Project description

MongoDBQueriesManager

Codecov Travis (.com) PyPI GitHub PyPI - Python Version Contributor Covenant

Convert query parameters from API urls to MongoDB queries !

This project was inspired by api-query-params (JS Library).

Features:

  • Powerful: Supports most of MongoDB operators ($in, $regexp, ...) and features (nested objects, type casting, ...)
  • Agnostic: Works with any web frameworks (Flask, Sanic, ...) and/or MongoDB libraries (pymongo, motor, ...)
  • Simple: ~150 LOC, Python typing
  • Tested: 100% tested

Installation:

pipenv install mongo-queries-manager

Usages:

Api

mqm(string_query: str) -> Dict[str, Any]:

Description

Converts string_query into a MongoDB query dict.

Arguments
  • string_query: query string of the requested API URL (ie, frist_name=John&limit=10), Works with url encoded. [required]
Returns

The resulting dictionary contains the following properties:

  • filter: Contains the query criteria.
  • sort: Contains the sort criteria (cursor modifiers).
  • skip: Contains the skip criteria (cursor modifiers).
  • limit: Contains the limit criteria (cursor modifiers).
Exception

In case of error the following exception was raised:

  • MongoDBQueriesManagerBaseError: Base MongoDBQueriesManager errors.
  • SkipError: Raised when skip is negative / bad value.
  • LimitError: Raised when limit is negative / bad value.
  • ListOperatorError: Raised list operator was not possible.
  • FilterError: Raised when parse filter method fail to find a valid match.
Examples:
from pymongo import MongoClient
from pymongo.collection import Collection
from pymongo.database import Database

from mongo_queries_manager import mqm

client: MongoClient = MongoClient('localhost', 27017)
db: Database = client['test-database']
collection: Collection = db['test-collection']

mongodb_query = mqm(string_query="status=sent&toto=true&timestamp>2016-01-01&"
                                 "author.firstName=/john/i&limit=100&skip=50&sort=-timestamp")

result = collection.find(**mongodb_query)

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

mongo-queries-manager-0.1.0.tar.gz (5.1 kB view hashes)

Uploaded Source

Built Distribution

mongo_queries_manager-0.1.0-py3-none-any.whl (6.5 kB view hashes)

Uploaded Python 3

Supported by

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