Convert query parameters from API urls to MongoDB queries !
Project description
MongoDBQueriesManager
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×tamp>2016-01-01&"
"author.firstName=/john/i&limit=100&skip=50&sort=-timestamp")
result = collection.find(**mongodb_query)
Project details
Release history Release notifications | RSS feed
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
Close
Hashes for mongo-queries-manager-0.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 290cf0c28a1c0cd3b6485ab5b6e0a6df41301eac4007a1962009358e9f7508ac |
|
MD5 | 9a7243cd95433ba96fda5cfc2afe4979 |
|
BLAKE2b-256 | a2d8f63a585a1de3c66a91068b8dd29cbda9a08ecd684d66b0e93452cbf2512d |
Close
Hashes for mongo_queries_manager-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f9ab50fe4a31a1523df6121642a0ee6b533d10f070e294ee0f64a4cd715930d8 |
|
MD5 | 3952817b781733c06eb8e9515bbc252a |
|
BLAKE2b-256 | 9e622739374d4ccd8b0d2a8f3c0c0148d377f1270e0258dd2b7aff80302303ec |