Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add collation and fix tests #173

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Test",
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
"args": [
"-u",
"tdd",
"--timeout",
"999999",
"--colors",
"${workspaceFolder}/tests/*"
],
"internalConsoleOptions": "openOnSessionStart",
"env": {
},
"outputCapture": "std"
},
]
}
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ Promise fulfilled with object having properties:
* `total` {Number} - Total number of documents in collection that match a query
* `limit` {Number} - Limit that was used
* `[page]` {Number} - Only if specified or default `page`/`offset` values were used
* `[pages]` {Number} - Only if `page` specified or default `page`/`offset` values were used
* `[pages]` {Number} - Only if `page` specified or default `page`/`offset` values were used
* `[prev]` {Number} - Only if there is a previous page to go to.
* `[next]` {Number} - Only if there is a next page to go to.
* `[offset]` {Number} - Only if specified or default `page`/`offset` values were used

### Examples
Expand Down
55 changes: 35 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* @param {Object} [options={}]
* @param {Object|String} [options.select]
* @param {Object|String} [options.sort]
* @param {Object} [options.collation]
* @param {Array|Object|String} [options.populate]
* @param {Boolean} [options.lean=false]
* @param {Boolean} [options.leanWithId=true]
Expand All @@ -22,74 +23,88 @@ function paginate(query, options, callback) {
let select = options.select;
let sort = options.sort;
let populate = options.populate;
let collation = options.collation || {};
let lean = options.lean || false;
let leanWithId = options.leanWithId ? options.leanWithId : true;
let limit = options.limit ? options.limit : 10;
let page, offset, skip, promises;
if (options.offset) {
let leanWithId = options.hasOwnProperty('leanWithId') ? options.leanWithId : true;
let limit = options.hasOwnProperty('limit') ? options.limit : 10;
let page, offset, skip, promises = [];
if (options.hasOwnProperty('offset')) {
offset = options.offset;
skip = offset;
} else if (options.page) {
} else if (options.hasOwnProperty('page')) {
page = options.page;
skip = (page - 1) * limit;
} else {
page = 1;
offset = 0;
skip = offset;
}
if (limit) {

promises.push(this.count(query).exec());

if (limit > 0) {
let docsQuery = this.find(query)
.select(select)
.sort(sort)
.skip(skip)
.limit(limit)
.lean(lean);
.lean(lean)
.collation(collation);
if (populate) {
[].concat(populate).forEach((item) => {
docsQuery.populate(item);
});
}
promises = {
docs: docsQuery.exec(),
count: this.count(query).exec()
};

let docPromise = docsQuery.exec();

if (lean && leanWithId) {
promises.docs = promises.docs.then((docs) => {
docPromise = docPromise.then((docs) => {
docs.forEach((doc) => {
doc.id = String(doc._id);
});
return docs;
});
}

promises.push(docPromise);
}
promises = Object.keys(promises).map((x) => promises[x]);

return Promise.all(promises).then((data) => {
let result = {
docs: data.docs,
total: data.count,
total: data[0],
docs: data[1] || [],
limit: limit
};
if (offset !== undefined) {
result.offset = offset;
}
if (page !== undefined) {
result.page = page;
result.pages = Math.ceil(data.count / limit) || 1;
result.pages = Math.ceil(result.total / limit) || 1;
let prev = page - 1;
let next = page + 1;

if (prev > 0) {
result.prev = prev;
}

if (next <= result.pages) {
result.next = next;
}
}
if (typeof callback === 'function') {
return callback(null, result);
}
let promise = new Promise();
promise.resolve(result);
return promise;
return result;
});
}

/**
* @param {Schema} schema
*/

module.exports = function(schema) {
module.exports = function (schema) {
schema.statics.paginate = paginate;
};

Expand Down
Loading