UNPKG

@tepez/mongo-cursor-pagination

Version:

Make it easy to return cursor-paginated results from a Mongo collection

37 lines (32 loc) 2.17 kB
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; } const find = require('./find'); const sanitizeQuery = require('./utils/sanitizeQuery'); /** * A wrapper around `find()` that make it easy to implement a basic HTTP API using Express. So your * user can call "/list?limit=1&fields=_id,name" and the querystring parameters will be passed * to this method on the Express request object. * * @param {ExpressRequest} req An express request object with the following on the querystring: * -limit: If a numeric string, passed to `find()` as the limit. If limit also passed in params * then this value cannot exceed it. * -next: If a non-empty string, passed to `find()` as the next cursor. * -previous: If a non-empty string, passed to `find()` as the previous cursor. * -fields: If a non-empty string, used to limit fields that are returned. Multiple fields * can be specified as a comma-delimited list. If field name used is not in params.fields, * it will be ignored. * @param {MongoCollection} collection A collection object returned from the MongoDB library's * @param {Object} params See documentation for `find()`, plus these options: * -overrideFields: an object containing fields that should override fields from the querystring, e.g. * {_id: 0} or {internalField: 1}. We only support field exclusion for _id, as we expect whitelists * for fields from both params.fields and params.overrideFields. */ module.exports = (() => { var _ref = _asyncToGenerator(function* (req, collection, params) { params = sanitizeQuery(req.query, params); return find(collection, params); }); function findWithReq(_x, _x2, _x3) { return _ref.apply(this, arguments); } return findWithReq; })();