-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
537e553
commit c9f83bf
Showing
22 changed files
with
480 additions
and
16 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import paginate from './paginate'; | ||
import * as paginateTypes from './paginate.types'; | ||
|
||
export { paginate, paginateTypes }; | ||
// # sourceMappingURL=index.js.map |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
const paginate = (schema) => { | ||
/** | ||
* @typedef {Object} QueryResult | ||
* @property {Document[]} results - Results found | ||
* @property {number} page - Current page | ||
* @property {number} limit - Maximum number of results per page | ||
* @property {number} totalPages - Total number of pages | ||
* @property {number} totalResults - Total number of documents | ||
*/ | ||
/** | ||
* Query for documents with pagination | ||
* @param {Object} [filter] - Mongo filter | ||
* @param {Object} [options] - Query options | ||
* @param {string} [options.sortBy] - Sorting criteria using the format: sortField:(desc|asc). Multiple sorting criteria should be separated by commas (,) | ||
* @param {string} [options.populate] - Populate data fields. Hierarchy of fields should be separated by (.). Multiple populating criteria should be separated by commas (,) | ||
* @param {number} [options.limit] - Maximum number of results per page (default = 10) | ||
* @param {number} [options.page] - Current page (default = 1) | ||
* @param {string} [options.projectBy] - Fields to hide or include (default = '') | ||
* @returns {Promise<QueryResult>} | ||
*/ | ||
schema.static('paginate', async function (filter, options) { | ||
let sort = ''; | ||
if (options.sortBy) { | ||
const sortingCriteria = []; | ||
options.sortBy.split(',').forEach((sortOption) => { | ||
const [key, order] = sortOption.split(':'); | ||
sortingCriteria.push((order === 'desc' ? '-' : '') + key); | ||
}); | ||
sort = sortingCriteria.join(' '); | ||
} else { | ||
sort = 'createdAt'; | ||
} | ||
let project = ''; | ||
if (options.projectBy) { | ||
const projectionCriteria = []; | ||
options.projectBy.split(',').forEach((projectOption) => { | ||
const [key, include] = projectOption.split(':'); | ||
projectionCriteria.push((include === 'hide' ? '-' : '') + key); | ||
}); | ||
project = projectionCriteria.join(' '); | ||
} else { | ||
project = '-createdAt -updatedAt'; | ||
} | ||
const limit = options.limit && parseInt(options.limit.toString(), 10) > 0 ? parseInt(options.limit.toString(), 10) : 10; | ||
const page = options.page && parseInt(options.page.toString(), 10) > 0 ? parseInt(options.page.toString(), 10) : 1; | ||
const skip = (page - 1) * limit; | ||
const countPromise = this.countDocuments(filter).exec(); | ||
let docsPromise = this.find(filter).sort(sort).skip(skip).limit(limit).select(project); | ||
if (options.populate) { | ||
options.populate.split(',').forEach((populateOption) => { | ||
docsPromise = docsPromise.populate( | ||
populateOption | ||
.split('.') | ||
.reverse() | ||
.reduce((a, b) => ({ path: b, populate: a })) | ||
); | ||
}); | ||
} | ||
docsPromise = docsPromise.exec(); | ||
return Promise.all([countPromise, docsPromise]).then((values) => { | ||
const [totalResults, results] = values; | ||
const totalPages = Math.ceil(totalResults / limit); | ||
const result = { | ||
results, | ||
page, | ||
limit, | ||
totalPages, | ||
totalResults, | ||
}; | ||
return Promise.resolve(result); | ||
}); | ||
}); | ||
}; | ||
export default paginate; | ||
// # sourceMappingURL=paginate.js.map |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.