@conpago/mongo-cursor-pagination
Version:
Make it easy to return cursor-paginated results from a Mongo collection
34 lines (33 loc) • 2.41 kB
TypeScript
import { QueryParams, PaginationResponse } from "./types";
import { Collection } from "mongodb";
/**
* Performs a find() query on a passed-in Mongo collection, using criteria you specify. The results
* are ordered by the paginatedField.
*
* @param {Collection} collection A collection object returned from the MongoDB library's.
* @param {QueryParams} params
* @param {object} params.query The find query.
* @param {Number} params.limit The page size. Must be between 1 and `config.MAX_LIMIT`.
* @param {object} params.fields Fields to query in the Mongo object format, e.g. {_id: 1, timestamp :1}.
* The default is to query all fields.
* @param {String} params.paginatedField The field name to query the range for. The field must be:
* 1. Orderable. We must sort by this value. If duplicate values for paginatedField field
* exist, the results will be secondarily ordered by the _id.
* 2. Indexed. For large collections, this should be indexed for query performance.
* 3. Immutable. If the value changes between paged queries, it could appear twice.
4. Consistent. All values (except undefined and null values) must be of the same type.
* The default is to use the Mongo built-in '_id' field, which satisfies the above criteria.
* The only reason to NOT use the Mongo _id field is if you chose to implement your own ids.
* @param {boolean} params.sortAscending Whether to sort in ascending order by the `paginatedField`.
* @param {boolean} params.sortCaseInsensitive Whether to ignore case when sorting, in which case `paginatedField`
* @param {boolean} params.getTotal Whether to fetch the total count for the query results
* must be a string property.
* @param {String} params.next The value to start querying the page.
* @param {String} params.previous The value to start querying previous page.
* @param {String} params.after The _id to start querying the page.
* @param {String} params.before The _id to start querying previous page.
* @param {String} params.hint An optional index hint to provide to the mongo query
* @param {object} params.collation An optional collation to provide to the mongo query. E.g. { locale: 'en', strength: 2 }. When null, disables the global collation.
*/
declare const _default: (collection: Collection | any, params: QueryParams) => Promise<PaginationResponse>;
export default _default;