Skip to main content

Convert query parameters from API urls to MongoDB queries !

Project description

MongoDBQueriesManager

GitHub Contributor Covenant

Convert query parameters from API urls to MongoDB queries !

⚠️Work in progress⚠️

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

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:

  • MongoDBQueriesManagerError: This exception was raised if the library fail to parse query. Contains raison attribute.
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.0.1.tar.gz (4.5 kB view hashes)

Uploaded Source

Built Distribution

mongo_queries_manager-0.0.1-py3-none-any.whl (5.8 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