1.0.0 • Published 4 years ago
@zishone/monique v1.0.0
monique
A middleware that utilized rsql-mongodb to parse url queries into mongodb filter and options. Who's monique? It is not a who, it's just short for Mongo I Query.
Installation
$ npm i @zishone/monique
Usage
const express = require('express');
const { mquery } = require('@zishone/monique');
const MongoClient = require('mongodb').MongoClient;
const app = express();
// Use Middleware
app.use(mquery());
MongoClient.connect('mongodb://localhost:27017', (err, client) => {
const db = client.db('myDB');
app.get('/get', async (req, res) => {
// FilterQuery and FindOneOptions should be available here
const { filter, options } = req.mquery;
const cursor = db.collection('myCollection').find(filter, options);
const data = await cursor.toArray();
res.send({ data });
});
app.listen(3000, () => {
console.log('Listening at port 3000')
})
});
Queries
- filter
- To filters list of objects.
- Example:
More at rsql-mongodb/get?filter=key==value
- fields
- To specify which fields of the objects to be returned.
- Example:
/get?fields=key1;key2
- sort
- To sort list of objects.
- Example:
Defaults to/get?sort=key1==asc;key2==desc;key3
asc
if not specified.
- skip
- To skip elements in list of objects.
- Example:
/get?skip=1
- limit
- To limit elements in list of objects.
- Example:
/get?limit=1
- page
- To skip elements in list of objects based on the given limit.
- Example:
/get?page=1&limit=10
Authors
- Zishran Garces
See also the list of contributors who participated in this project.
License
This project is licensed under the MIT License - see the LICENSE file for details.