mongo-cursor-pagination
Advanced tools
Comparing version
## Changelog | ||
* 6.2.0 Added support for 'after' and 'before' parameters - thanks @lirbank | ||
* 6.1.0 Added support for native mongodb driver (https://github.com/mixmaxhq/mongo-cursor-pagination/pull/24 - thanks @lirbank) | ||
@@ -4,0 +6,0 @@ |
@@ -28,3 +28,16 @@ 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"); }); }; } | ||
* -previous {String} The value to start querying previous page. | ||
* -after {String} The _id to start querying the page. | ||
* -before {String} The _id to start querying previous page. | ||
*/ | ||
function getPropertyViaDotNotation(propertyName, object) { | ||
const parts = propertyName.split('.'); | ||
let prop = object; | ||
for (let i = 0; i < parts.length; i++) { | ||
prop = prop[parts[i]]; | ||
} | ||
return prop; | ||
} | ||
module.exports = (() => { | ||
@@ -51,2 +64,44 @@ var _ref = _asyncToGenerator(function* (collection, params) { | ||
// | ||
// params.after - overides params.next | ||
// | ||
// The 'after' param sets the start position for the next page. This is similar to the | ||
// 'next' param, with the difference that 'after' takes a plain _id instead of an encoded | ||
// string of both _id and paginatedField values. | ||
if (params.after) { | ||
if (shouldSecondarySortOnId) { | ||
// Since the primary sort field is not provided by the 'after' pagination cursor we | ||
// have to look it up when the paginated field is not _id. | ||
const doc = yield collection.findOne({ _id: params.after }, { [params.paginatedField]: true, _id: false }); | ||
if (doc) { | ||
// Handle usage of dot notation in paginatedField | ||
const prop = getPropertyViaDotNotation(params.paginatedField, doc); | ||
params.next = [prop, params.after]; | ||
} | ||
} else { | ||
params.next = params.after; | ||
} | ||
} | ||
// | ||
// params.before - overides params.previous | ||
// | ||
// The 'before' param sets the start position for the previous page. This is similar to the | ||
// 'previous' param, with the difference that 'before' takes a plain _id instead of an encoded | ||
// string of both _id and paginatedField values. | ||
if (params.before) { | ||
if (shouldSecondarySortOnId) { | ||
// Since the primary sort field is not provided by the 'before' pagination cursor we | ||
// have to look it up when the paginated field is not _id. | ||
const doc = yield collection.findOne({ _id: params.before }, { [params.paginatedField]: true, _id: false }); | ||
if (doc) { | ||
// Handle usage of dot notation in paginatedField | ||
const prop = getPropertyViaDotNotation(params.paginatedField, doc); | ||
params.previous = [prop, params.before]; | ||
} | ||
} else { | ||
params.previous = params.before; | ||
} | ||
} | ||
var fields; | ||
@@ -53,0 +108,0 @@ var removePaginatedFieldInResponse = false; |
{ | ||
"name": "mongo-cursor-pagination", | ||
"version": "6.1.0", | ||
"version": "6.2.0", | ||
"description": "Make it easy to return cursor-paginated results from a Mongo collection", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -26,3 +26,16 @@ var _ = require('underscore'); | ||
* -previous {String} The value to start querying previous page. | ||
* -after {String} The _id to start querying the page. | ||
* -before {String} The _id to start querying previous page. | ||
*/ | ||
function getPropertyViaDotNotation(propertyName, object) { | ||
const parts = propertyName.split('.'); | ||
let prop = object; | ||
for (let i=0; i < parts.length; i++) { | ||
prop = prop[parts[i]]; | ||
} | ||
return prop; | ||
} | ||
module.exports = async function(collection, params) { | ||
@@ -48,2 +61,50 @@ if (params.previous) params.previous = bsonUrlEncoding.decode(params.previous); | ||
// | ||
// params.after - overides params.next | ||
// | ||
// The 'after' param sets the start position for the next page. This is similar to the | ||
// 'next' param, with the difference that 'after' takes a plain _id instead of an encoded | ||
// string of both _id and paginatedField values. | ||
if (params.after) { | ||
if (shouldSecondarySortOnId) { | ||
// Since the primary sort field is not provided by the 'after' pagination cursor we | ||
// have to look it up when the paginated field is not _id. | ||
const doc = await collection.findOne( | ||
{ _id: params.after }, | ||
{ [params.paginatedField]: true, _id: false }, | ||
); | ||
if (doc) { | ||
// Handle usage of dot notation in paginatedField | ||
const prop = getPropertyViaDotNotation(params.paginatedField, doc); | ||
params.next = [prop, params.after]; | ||
} | ||
} else { | ||
params.next = params.after; | ||
} | ||
} | ||
// | ||
// params.before - overides params.previous | ||
// | ||
// The 'before' param sets the start position for the previous page. This is similar to the | ||
// 'previous' param, with the difference that 'before' takes a plain _id instead of an encoded | ||
// string of both _id and paginatedField values. | ||
if (params.before) { | ||
if (shouldSecondarySortOnId) { | ||
// Since the primary sort field is not provided by the 'before' pagination cursor we | ||
// have to look it up when the paginated field is not _id. | ||
const doc = await collection.findOne( | ||
{ _id: params.before }, | ||
{ [params.paginatedField]: true, _id: false }, | ||
); | ||
if (doc) { | ||
// Handle usage of dot notation in paginatedField | ||
const prop = getPropertyViaDotNotation(params.paginatedField, doc); | ||
params.previous = [prop, params.before]; | ||
} | ||
} else { | ||
params.previous = params.before; | ||
} | ||
} | ||
var fields; | ||
@@ -50,0 +111,0 @@ var removePaginatedFieldInResponse = false; |
61327
7.76%864
13.98%