@tepez/mongo-cursor-pagination 8.0.1 → 9.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -2
- package/dist/node/aggregate.js +2 -7
- package/dist/node/find.js +4 -6
- package/dist/node/findWithReq.js +0 -1
- package/dist/node/search.js +2 -7
- package/dist/node/utils/sanitizeParams.js +1 -1
- package/index.d.ts +117 -0
- package/package.json +20 -19
- package/src/aggregate.js +2 -7
- package/src/find.js +5 -7
- package/src/findWithReq.js +0 -1
- package/src/mongoose.plugin.js +3 -3
- package/src/search.js +3 -8
- package/src/utils/bsonUrlEncoding.js +2 -2
- package/src/utils/sanitizeParams.js +1 -1
- package/CHANGELOG.md +0 -76
package/README.md
CHANGED
|
@@ -42,8 +42,7 @@ Call `find()` with the following parameters:
|
|
|
42
42
|
Performs a find() query on a passed-in Mongo collection, using criteria you specify. The results
|
|
43
43
|
are ordered by the paginatedField.
|
|
44
44
|
|
|
45
|
-
@param {MongoCollection} collection A collection object returned from the MongoDB library's
|
|
46
|
-
or the mongoist package's `db.collection(<collectionName>)` method.
|
|
45
|
+
@param {MongoCollection} collection A collection object returned from the MongoDB library's.
|
|
47
46
|
@param {Object} params
|
|
48
47
|
-query {Object} The find query.
|
|
49
48
|
-limit {Number} The page size. Must be between 1 and `config.MAX_LIMIT`.
|
package/dist/node/aggregate.js
CHANGED
|
@@ -20,8 +20,7 @@ const sanitizeParams = require('./utils/sanitizeParams');
|
|
|
20
20
|
* Additionally, an additional query will be appended to the first `$match` found in order to apply the offset
|
|
21
21
|
* required for the cursor.
|
|
22
22
|
*
|
|
23
|
-
* @param {MongoCollection} collection A collection object returned from the MongoDB library's
|
|
24
|
-
* or the mongoist package's `db.collection(<collectionName>)` method.
|
|
23
|
+
* @param {MongoCollection} collection A collection object returned from the MongoDB library's.
|
|
25
24
|
* @param {Object} params
|
|
26
25
|
* -aggregation {Object[]} The aggregation query.
|
|
27
26
|
* -limit {Number} The page size. Must be between 1 and `config.MAX_LIMIT`.
|
|
@@ -74,11 +73,7 @@ module.exports = (() => {
|
|
|
74
73
|
*/
|
|
75
74
|
const options = config.COLLATION ? { collation: config.COLLATION } : undefined;
|
|
76
75
|
|
|
77
|
-
|
|
78
|
-
// https://www.npmjs.com/package/mongoist#cursor-operations
|
|
79
|
-
const aggregateMethod = collection.aggregateAsCursor ? 'aggregateAsCursor' : 'aggregate';
|
|
80
|
-
|
|
81
|
-
const results = yield collection[aggregateMethod](params.aggregation, options).toArray();
|
|
76
|
+
const results = yield collection.aggregate(params.aggregation, options).toArray();
|
|
82
77
|
|
|
83
78
|
return prepareResponse(results, params);
|
|
84
79
|
});
|
package/dist/node/find.js
CHANGED
|
@@ -10,8 +10,7 @@ const sanitizeParams = require('./utils/sanitizeParams');
|
|
|
10
10
|
* Performs a find() query on a passed-in Mongo collection, using criteria you specify. The results
|
|
11
11
|
* are ordered by the paginatedField.
|
|
12
12
|
*
|
|
13
|
-
* @param {MongoCollection} collection A collection object returned from the MongoDB library's
|
|
14
|
-
* or the mongoist package's `db.collection(<collectionName>)` method.
|
|
13
|
+
* @param {MongoCollection} collection A collection object returned from the MongoDB library's.
|
|
15
14
|
* @param {Object} params
|
|
16
15
|
* -query {Object} The find query.
|
|
17
16
|
* -limit {Number} The page size. Must be between 1 and `config.MAX_LIMIT`.
|
|
@@ -38,11 +37,10 @@ module.exports = (() => {
|
|
|
38
37
|
const cursorQuery = generateCursorQuery(params);
|
|
39
38
|
const $sort = generateSort(params);
|
|
40
39
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
const findMethod = collection.findAsCursor ? 'findAsCursor' : 'find';
|
|
40
|
+
const findOptions = {};
|
|
41
|
+
if (params.fields) findOptions.projection = params.fields;
|
|
44
42
|
|
|
45
|
-
const query = collection
|
|
43
|
+
const query = collection.find({ $and: [cursorQuery, params.query] }, findOptions);
|
|
46
44
|
|
|
47
45
|
/**
|
|
48
46
|
* IMPORTANT
|
package/dist/node/findWithReq.js
CHANGED
|
@@ -17,7 +17,6 @@ const sanitizeQuery = require('./utils/sanitizeQuery');
|
|
|
17
17
|
* can be specified as a comma-delimited list. If field name used is not in params.fields,
|
|
18
18
|
* it will be ignored.
|
|
19
19
|
* @param {MongoCollection} collection A collection object returned from the MongoDB library's
|
|
20
|
-
* or the mongoist package's `db.collection(<collectionName>)` method.
|
|
21
20
|
* @param {Object} params See documentation for `find()`, plus these options:
|
|
22
21
|
* -overrideFields: an object containing fields that should override fields from the querystring, e.g.
|
|
23
22
|
* {_id: 0} or {internalField: 1}. We only support field exclusion for _id, as we expect whitelists
|
package/dist/node/search.js
CHANGED
|
@@ -11,8 +11,7 @@ const bsonUrlEncoding = require('./utils/bsonUrlEncoding');
|
|
|
11
11
|
* a paginatedField parameter. Note that this is less performant than find() because it must
|
|
12
12
|
* perform the full search on each call to this function.
|
|
13
13
|
*
|
|
14
|
-
* @param {MongoCollection} collection A collection object returned from the MongoDB library's
|
|
15
|
-
* or the mongoist package's `db.collection(<collectionName>)` method. This MUST have a Mongo
|
|
14
|
+
* @param {MongoCollection} collection A collection object returned from the MongoDB library's. This MUST have a Mongo
|
|
16
15
|
* $text index on it.
|
|
17
16
|
* See https://docs.mongodb.com/manual/core/index-text/.
|
|
18
17
|
* @param {String} searchString String to search on.
|
|
@@ -86,11 +85,7 @@ module.exports = (() => {
|
|
|
86
85
|
|
|
87
86
|
let response;
|
|
88
87
|
|
|
89
|
-
|
|
90
|
-
// https://www.npmjs.com/package/mongoist#cursor-operations
|
|
91
|
-
const aggregateMethod = collection.aggregateAsCursor ? 'aggregateAsCursor' : 'aggregate';
|
|
92
|
-
|
|
93
|
-
const aggregateQuery = collection[aggregateMethod](aggregate);
|
|
88
|
+
const aggregateQuery = collection.aggregate(aggregate);
|
|
94
89
|
const execMethod = aggregateQuery.toArray ? 'toArray' : 'exec';
|
|
95
90
|
const results = yield aggregateQuery[execMethod]();
|
|
96
91
|
|
|
@@ -2,9 +2,9 @@ function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, a
|
|
|
2
2
|
|
|
3
3
|
const _ = require('underscore');
|
|
4
4
|
|
|
5
|
-
const config = require('../config');
|
|
6
5
|
const bsonUrlEncoding = require('./bsonUrlEncoding');
|
|
7
6
|
const getPropertyViaDotNotation = require('./getPropertyViaDotNotation');
|
|
7
|
+
const config = require('../config');
|
|
8
8
|
|
|
9
9
|
module.exports = (() => {
|
|
10
10
|
var _ref = _asyncToGenerator(function* (collection, params) {
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Based on https://github.com/mixmaxhq/mongo-cursor-pagination/blob/v7.3.1/README.md
|
|
3
|
+
*/
|
|
4
|
+
declare module '@tepez/mongo-cursor-pagination' {
|
|
5
|
+
import { Document, FilterQuery, HydratedDocument, Schema, TreatAsPrimitives, Types } from 'mongoose';
|
|
6
|
+
|
|
7
|
+
// copy LeanDocument from mongoose 6.11.1 because it was removed in 7.0.0 and we need it to infer the lean type from the document
|
|
8
|
+
// https://github.com/Automattic/mongoose/blob/6.11.1/types/index.d.ts
|
|
9
|
+
|
|
10
|
+
type LeanType<T> =
|
|
11
|
+
0 extends (1 & T) ? T : // any
|
|
12
|
+
T extends TreatAsPrimitives ? T : // primitives
|
|
13
|
+
T extends Types.ArraySubdocument ? Omit<LeanDocument<T>, 'parentArray' | 'ownerDocument' | 'parent'> :
|
|
14
|
+
T extends Types.Subdocument ? Omit<LeanDocument<T>, '$isSingleNested' | 'ownerDocument' | 'parent'> :
|
|
15
|
+
LeanDocument<T>; // Documents and everything else
|
|
16
|
+
type LeanArray<T extends unknown[]> = T extends unknown[][] ? LeanArray<T[number]>[] : LeanType<T[number]>[];
|
|
17
|
+
|
|
18
|
+
type _LeanDocument<T> = {
|
|
19
|
+
[K in keyof T]: LeanDocumentElement<T[K]>;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
// Keep this a separate type, to ensure that T is a naked type.
|
|
23
|
+
// This way, the conditional type is distributive over union types.
|
|
24
|
+
// This is required for PopulatedDoc.
|
|
25
|
+
type LeanDocumentElement<T> =
|
|
26
|
+
T extends unknown[] ? LeanArray<T> : // Array
|
|
27
|
+
T extends Document ? LeanDocument<T> : // Subdocument
|
|
28
|
+
T;
|
|
29
|
+
|
|
30
|
+
type LeanDocument<T> = Omit<_LeanDocument<T>, Exclude<keyof Document, '_id' | 'id' | '__v'> | '$isSingleNested'>;
|
|
31
|
+
|
|
32
|
+
type MongoosePlugin = Parameters<Schema['plugin']>[0]
|
|
33
|
+
|
|
34
|
+
export interface ICursorPaginationMongoosePluginOptions {
|
|
35
|
+
/**
|
|
36
|
+
* custom function name
|
|
37
|
+
* @default paginate
|
|
38
|
+
*/
|
|
39
|
+
name?: string;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface IPaginateOptions<T extends Document<T>> {
|
|
43
|
+
/**
|
|
44
|
+
* The find query
|
|
45
|
+
*/
|
|
46
|
+
query: FilterQuery<T>;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* The page size. Must be between 1 and `config.MAX_LIMIT`.
|
|
50
|
+
*/
|
|
51
|
+
limit: number;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Fields to query in the Mongo object format, e.g. {_id: 1, timestamp :1}.
|
|
55
|
+
* @default query all fields.
|
|
56
|
+
*/
|
|
57
|
+
fields?: any;
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* The field name to query the range for. The field must be:
|
|
61
|
+
* 1. Orderable. We must sort by this value. If duplicate values for paginatedField field
|
|
62
|
+
* exist, the results will be secondarily ordered by the _id.
|
|
63
|
+
* 2. Indexed. For large collections, this should be indexed for query performance.
|
|
64
|
+
* 3. Immutable. If the value changes between paged queries, it could appear twice.
|
|
65
|
+
* 4. Complete. A value must exist for all documents.
|
|
66
|
+
* The default is to use the Mongo built-in '_id' field, which satisfies the above criteria.
|
|
67
|
+
* The only reason to NOT use the Mongo _id field is if you chose to implement your own ids.
|
|
68
|
+
* @default _id
|
|
69
|
+
*/
|
|
70
|
+
paginatedField?: string;
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* True to sort using paginatedField ascending
|
|
74
|
+
* @default false - descending
|
|
75
|
+
*/
|
|
76
|
+
sortAscending?: boolean;
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* The value to start querying the page.
|
|
80
|
+
*/
|
|
81
|
+
next?: string;
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* The value to start querying previous page.
|
|
85
|
+
*/
|
|
86
|
+
previous?: string;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export interface IPaginateResult<T extends Document> {
|
|
90
|
+
results: HydratedDocument<T>[];
|
|
91
|
+
previous?: string;
|
|
92
|
+
/**
|
|
93
|
+
* base64 encoded
|
|
94
|
+
* Contains the ID of the last object in the page
|
|
95
|
+
*/
|
|
96
|
+
next: string;
|
|
97
|
+
hasNext: boolean;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export interface ICursorPaginationExtendedModel<T extends Document> {
|
|
101
|
+
paginate(options: IPaginateOptions<T>): IPaginateResult<T>;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export const mongoosePlugin: MongoosePlugin;
|
|
105
|
+
|
|
106
|
+
export const config: {
|
|
107
|
+
/**
|
|
108
|
+
* @default 50
|
|
109
|
+
*/
|
|
110
|
+
DEFAULT_LIMIT: number
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* @default 300
|
|
114
|
+
*/
|
|
115
|
+
MAX_LIMIT: number
|
|
116
|
+
};
|
|
117
|
+
}
|
package/package.json
CHANGED
|
@@ -1,20 +1,22 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tepez/mongo-cursor-pagination",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "9.0.0",
|
|
4
4
|
"description": "Make it easy to return cursor-paginated results from a Mongo collection",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"files": [
|
|
7
7
|
"index.js",
|
|
8
8
|
"dist",
|
|
9
|
-
"src"
|
|
9
|
+
"src",
|
|
10
|
+
"index.d.ts"
|
|
10
11
|
],
|
|
11
12
|
"scripts": {
|
|
12
13
|
"babelBuild": "babel src -d dist/node",
|
|
13
14
|
"babelWatch": "babel --watch src -d dist/node",
|
|
14
15
|
"lint": "eslint .",
|
|
15
16
|
"test-native": "npx cross-env DRIVER=native jest",
|
|
16
|
-
"test-
|
|
17
|
+
"test-native-debug": "npx cross-env DRIVER=native npx --node-options=--inspect-brk jest --runInBand",
|
|
17
18
|
"test": "npm run test-native",
|
|
19
|
+
"test-debug": "npm run test-native-debug",
|
|
18
20
|
"update": "npm run babelBuild && npx np --no-publish --branch tepez"
|
|
19
21
|
},
|
|
20
22
|
"repository": {
|
|
@@ -37,28 +39,27 @@
|
|
|
37
39
|
},
|
|
38
40
|
"homepage": "https://github.com/tepez/mongo-cursor-pagination#readme",
|
|
39
41
|
"dependencies": {
|
|
40
|
-
"base64-url": "^2.
|
|
41
|
-
"bson": "^
|
|
42
|
-
"dot-prop": "^
|
|
42
|
+
"base64-url": "^2.3.3",
|
|
43
|
+
"bson": "^5.3.0",
|
|
44
|
+
"dot-prop": "^6.0.1",
|
|
43
45
|
"projection-utils": "^1.1.0",
|
|
44
|
-
"semver": "^5.
|
|
45
|
-
"underscore": "^1.
|
|
46
|
+
"semver": "^7.5.1",
|
|
47
|
+
"underscore": "^1.13.6"
|
|
46
48
|
},
|
|
47
49
|
"devDependencies": {
|
|
48
|
-
"@mixmaxhq/prettier-config": "^1.
|
|
50
|
+
"@mixmaxhq/prettier-config": "^1.1.0",
|
|
49
51
|
"babel-cli": "^6.26.0",
|
|
50
|
-
"babel-core": "^6.26.
|
|
52
|
+
"babel-core": "^6.26.3",
|
|
51
53
|
"babel-plugin-transform-async-to-generator": "^6.24.1",
|
|
52
54
|
"cross-env": "^7.0.3",
|
|
53
|
-
"eslint": "^
|
|
54
|
-
"eslint-config-mixmax": "^
|
|
55
|
-
"jest": "^
|
|
56
|
-
"mongodb": "
|
|
57
|
-
"mongodb-memory-server-core": "^
|
|
58
|
-
"
|
|
59
|
-
"
|
|
60
|
-
"
|
|
61
|
-
"prettier": "^1.19.1"
|
|
55
|
+
"eslint": "^8.41.0",
|
|
56
|
+
"eslint-config-mixmax": "^5.1.0",
|
|
57
|
+
"jest": "^29.5.0",
|
|
58
|
+
"mongodb": "5.5.0",
|
|
59
|
+
"mongodb-memory-server-core": "^8.12.2",
|
|
60
|
+
"mongoose": "7.2.1",
|
|
61
|
+
"np": "^8.0.1",
|
|
62
|
+
"prettier": "^2.8.8"
|
|
62
63
|
},
|
|
63
64
|
"engines": {
|
|
64
65
|
"node": ">= 6.9.1"
|
package/src/aggregate.js
CHANGED
|
@@ -18,8 +18,7 @@ const sanitizeParams = require('./utils/sanitizeParams');
|
|
|
18
18
|
* Additionally, an additional query will be appended to the first `$match` found in order to apply the offset
|
|
19
19
|
* required for the cursor.
|
|
20
20
|
*
|
|
21
|
-
* @param {MongoCollection} collection A collection object returned from the MongoDB library's
|
|
22
|
-
* or the mongoist package's `db.collection(<collectionName>)` method.
|
|
21
|
+
* @param {MongoCollection} collection A collection object returned from the MongoDB library's.
|
|
23
22
|
* @param {Object} params
|
|
24
23
|
* -aggregation {Object[]} The aggregation query.
|
|
25
24
|
* -limit {Number} The page size. Must be between 1 and `config.MAX_LIMIT`.
|
|
@@ -69,11 +68,7 @@ module.exports = async function aggregate(collection, params) {
|
|
|
69
68
|
*/
|
|
70
69
|
const options = config.COLLATION ? { collation: config.COLLATION } : undefined;
|
|
71
70
|
|
|
72
|
-
|
|
73
|
-
// https://www.npmjs.com/package/mongoist#cursor-operations
|
|
74
|
-
const aggregateMethod = collection.aggregateAsCursor ? 'aggregateAsCursor' : 'aggregate';
|
|
75
|
-
|
|
76
|
-
const results = await collection[aggregateMethod](params.aggregation, options).toArray();
|
|
71
|
+
const results = await collection.aggregate(params.aggregation, options).toArray();
|
|
77
72
|
|
|
78
73
|
return prepareResponse(results, params);
|
|
79
74
|
};
|
package/src/find.js
CHANGED
|
@@ -8,8 +8,7 @@ const sanitizeParams = require('./utils/sanitizeParams');
|
|
|
8
8
|
* Performs a find() query on a passed-in Mongo collection, using criteria you specify. The results
|
|
9
9
|
* are ordered by the paginatedField.
|
|
10
10
|
*
|
|
11
|
-
* @param {MongoCollection} collection A collection object returned from the MongoDB library's
|
|
12
|
-
* or the mongoist package's `db.collection(<collectionName>)` method.
|
|
11
|
+
* @param {MongoCollection} collection A collection object returned from the MongoDB library's.
|
|
13
12
|
* @param {Object} params
|
|
14
13
|
* -query {Object} The find query.
|
|
15
14
|
* -limit {Number} The page size. Must be between 1 and `config.MAX_LIMIT`.
|
|
@@ -28,18 +27,17 @@ const sanitizeParams = require('./utils/sanitizeParams');
|
|
|
28
27
|
* -before {String} The _id to start querying previous page.
|
|
29
28
|
* -hint {String} An optional index hint to provide to the mongo query
|
|
30
29
|
*/
|
|
31
|
-
module.exports = async function(collection, params) {
|
|
30
|
+
module.exports = async function (collection, params) {
|
|
32
31
|
const removePaginatedFieldInResponse = params.fields && !params.fields[params.paginatedField];
|
|
33
32
|
|
|
34
33
|
params = _.defaults(await sanitizeParams(collection, params), { query: {} });
|
|
35
34
|
const cursorQuery = generateCursorQuery(params);
|
|
36
35
|
const $sort = generateSort(params);
|
|
37
36
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
const findMethod = collection.findAsCursor ? 'findAsCursor' : 'find';
|
|
37
|
+
const findOptions = {};
|
|
38
|
+
if (params.fields) findOptions.projection = params.fields;
|
|
41
39
|
|
|
42
|
-
const query = collection
|
|
40
|
+
const query = collection.find({ $and: [cursorQuery, params.query] }, findOptions);
|
|
43
41
|
|
|
44
42
|
/**
|
|
45
43
|
* IMPORTANT
|
package/src/findWithReq.js
CHANGED
|
@@ -15,7 +15,6 @@ const sanitizeQuery = require('./utils/sanitizeQuery');
|
|
|
15
15
|
* can be specified as a comma-delimited list. If field name used is not in params.fields,
|
|
16
16
|
* it will be ignored.
|
|
17
17
|
* @param {MongoCollection} collection A collection object returned from the MongoDB library's
|
|
18
|
-
* or the mongoist package's `db.collection(<collectionName>)` method.
|
|
19
18
|
* @param {Object} params See documentation for `find()`, plus these options:
|
|
20
19
|
* -overrideFields: an object containing fields that should override fields from the querystring, e.g.
|
|
21
20
|
* {_id: 0} or {internalField: 1}. We only support field exclusion for _id, as we expect whitelists
|
package/src/mongoose.plugin.js
CHANGED
|
@@ -11,12 +11,12 @@ const search = require('./search');
|
|
|
11
11
|
* @param {string} options.searchFnName name of the function.
|
|
12
12
|
*/
|
|
13
13
|
|
|
14
|
-
module.exports = function(schema, options) {
|
|
14
|
+
module.exports = function (schema, options) {
|
|
15
15
|
/**
|
|
16
16
|
* paginate function
|
|
17
17
|
* @param {Object} params required parameter
|
|
18
18
|
*/
|
|
19
|
-
const findFn = function(params) {
|
|
19
|
+
const findFn = function (params) {
|
|
20
20
|
if (!this.collection) {
|
|
21
21
|
throw new Error('collection property not found');
|
|
22
22
|
}
|
|
@@ -31,7 +31,7 @@ module.exports = function(schema, options) {
|
|
|
31
31
|
* @param {String} searchString String to search on. Required parameter
|
|
32
32
|
* @param {Object} params
|
|
33
33
|
*/
|
|
34
|
-
const searchFn = function(searchString, params) {
|
|
34
|
+
const searchFn = function (searchString, params) {
|
|
35
35
|
if (!this.collection) {
|
|
36
36
|
throw new Error('collection property not found');
|
|
37
37
|
}
|
package/src/search.js
CHANGED
|
@@ -9,8 +9,7 @@ const bsonUrlEncoding = require('./utils/bsonUrlEncoding');
|
|
|
9
9
|
* a paginatedField parameter. Note that this is less performant than find() because it must
|
|
10
10
|
* perform the full search on each call to this function.
|
|
11
11
|
*
|
|
12
|
-
* @param {MongoCollection} collection A collection object returned from the MongoDB library's
|
|
13
|
-
* or the mongoist package's `db.collection(<collectionName>)` method. This MUST have a Mongo
|
|
12
|
+
* @param {MongoCollection} collection A collection object returned from the MongoDB library's. This MUST have a Mongo
|
|
14
13
|
* $text index on it.
|
|
15
14
|
* See https://docs.mongodb.com/manual/core/index-text/.
|
|
16
15
|
* @param {String} searchString String to search on.
|
|
@@ -22,7 +21,7 @@ const bsonUrlEncoding = require('./utils/bsonUrlEncoding');
|
|
|
22
21
|
* -next {String} The value to start querying the page. Defaults to start at the beginning of
|
|
23
22
|
* the results.
|
|
24
23
|
*/
|
|
25
|
-
module.exports = async function(collection, searchString, params) {
|
|
24
|
+
module.exports = async function (collection, searchString, params) {
|
|
26
25
|
if (_.isString(params.limit)) params.limit = parseInt(params.limit, 10);
|
|
27
26
|
if (params.next) params.next = bsonUrlEncoding.decode(params.next);
|
|
28
27
|
|
|
@@ -90,11 +89,7 @@ module.exports = async function(collection, searchString, params) {
|
|
|
90
89
|
|
|
91
90
|
let response;
|
|
92
91
|
|
|
93
|
-
|
|
94
|
-
// https://www.npmjs.com/package/mongoist#cursor-operations
|
|
95
|
-
const aggregateMethod = collection.aggregateAsCursor ? 'aggregateAsCursor' : 'aggregate';
|
|
96
|
-
|
|
97
|
-
const aggregateQuery = collection[aggregateMethod](aggregate);
|
|
92
|
+
const aggregateQuery = collection.aggregate(aggregate);
|
|
98
93
|
const execMethod = aggregateQuery.toArray ? 'toArray' : 'exec';
|
|
99
94
|
const results = await aggregateQuery[execMethod]();
|
|
100
95
|
|
|
@@ -6,10 +6,10 @@ const { EJSON } = require('bson');
|
|
|
6
6
|
* encode/decode as a URL-safe string.
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
module.exports.encode = function(obj) {
|
|
9
|
+
module.exports.encode = function (obj) {
|
|
10
10
|
return base64url.encode(EJSON.stringify(obj));
|
|
11
11
|
};
|
|
12
12
|
|
|
13
|
-
module.exports.decode = function(str) {
|
|
13
|
+
module.exports.decode = function (str) {
|
|
14
14
|
return EJSON.parse(base64url.decode(str));
|
|
15
15
|
};
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
const _ = require('underscore');
|
|
2
2
|
|
|
3
|
-
const config = require('../config');
|
|
4
3
|
const bsonUrlEncoding = require('./bsonUrlEncoding');
|
|
5
4
|
const getPropertyViaDotNotation = require('./getPropertyViaDotNotation');
|
|
5
|
+
const config = require('../config');
|
|
6
6
|
|
|
7
7
|
module.exports = async function sanitizeParams(collection, params) {
|
|
8
8
|
if (params.previous) params.previous = bsonUrlEncoding.decode(params.previous);
|
package/CHANGELOG.md
DELETED
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
## [7.4.0](https://github.com/mixmaxhq/mongo-cursor-pagination/compare/v7.3.1...v7.4.0) (2021-03-08)
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
### Features
|
|
5
|
-
|
|
6
|
-
* **mongoose-plugin:** add search function ([0efd73c](https://github.com/mixmaxhq/mongo-cursor-pagination/commit/0efd73c9a5e53887226a4a1d2b61605a0e168514))
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
### Bug Fixes
|
|
10
|
-
|
|
11
|
-
* skip bad commit message ([4c85357](https://github.com/mixmaxhq/mongo-cursor-pagination/commit/4c85357f1079c6f73877ba6775b2eb6ad962c422))
|
|
12
|
-
|
|
13
|
-
### [7.3.1](https://github.com/mixmaxhq/mongo-cursor-pagination/compare/v7.3.0...v7.3.1) (2020-08-10)
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
### Bug Fixes
|
|
17
|
-
|
|
18
|
-
* **bson:** fixes regression where string _ids were no longer supported ([1487195](https://github.com/mixmaxhq/mongo-cursor-pagination/commit/1487195444fb1b6f151014522e498000d1dd452d))
|
|
19
|
-
|
|
20
|
-
## [7.3.0](https://github.com/mixmaxhq/mongo-cursor-pagination/compare/v7.2.1...v7.3.0) (2020-05-06)
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
### Features
|
|
24
|
-
|
|
25
|
-
* **find:** add optional hint parameter for the cursor ([17616da](https://github.com/mixmaxhq/mongo-cursor-pagination/commit/17616da43641ff2d455e70d96368e839afb216ae))
|
|
26
|
-
|
|
27
|
-
### [7.2.1](https://github.com/mixmaxhq/mongo-cursor-pagination/compare/v7.2.0...v7.2.1) (2020-05-06)
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
### Bug Fixes
|
|
31
|
-
|
|
32
|
-
* apply no-var rule changes ([8cf0620](https://github.com/mixmaxhq/mongo-cursor-pagination/commit/8cf0620b023ac460a62788b9d11763211d5a5f88))
|
|
33
|
-
* comply with new eslint rules ([e5851bd](https://github.com/mixmaxhq/mongo-cursor-pagination/commit/e5851bde1a86ab322aa6eac4c56995d98f80e74b))
|
|
34
|
-
* eslint cleanup ([3c3c913](https://github.com/mixmaxhq/mongo-cursor-pagination/commit/3c3c91311cab97d51896f16c8463d73bdb7d9225))
|
|
35
|
-
|
|
36
|
-
## Changelog
|
|
37
|
-
|
|
38
|
-
* 7.2.0 Add support for `COLLATION` configuration parameter.
|
|
39
|
-
|
|
40
|
-
* 7.1.0 Add support for `aggregate`.
|
|
41
|
-
|
|
42
|
-
* 7.0.1 Update base64-url to fix security issue (https://github.com/mixmaxhq/mongo-cursor-pagination/pull/41 - thanks @pwiebe).
|
|
43
|
-
|
|
44
|
-
* 7.0.0 Add findWithReq overrideFields support. Breaking: now throws errors on unusable `fields`/`overrideFields`, so check your inputs. Also changes our intersection mechanism, so it _could_ cause backwards-incompatible changes to fields resolution. If causes unexpected backwards-incompatible changes, please file an issue!
|
|
45
|
-
|
|
46
|
-
* 6.3.0 Can be used as a Mongoose plugin
|
|
47
|
-
|
|
48
|
-
* 6.2.0 Added support for 'after' and 'before' parameters - thanks @lirbank
|
|
49
|
-
|
|
50
|
-
* 6.1.0 Added support for native mongodb driver (https://github.com/mixmaxhq/mongo-cursor-pagination/pull/24 - thanks @lirbank)
|
|
51
|
-
|
|
52
|
-
* 6.0.1 Fix issue where calling `find` with a paginated field that has dot notation e.g. `start.dateTime` produces an invalid `next` token.
|
|
53
|
-
|
|
54
|
-
* 6.0.0 Breaking API change: `mongo-cursor-pagination` requires a Promise enabled mongodb instance from `mongoist` and returns Promises from `find`, `findWithReq`, and `search` rather than handling callbacks. *Note: Although the library now uses `async/await`, it is still useable in node >= 6.9.0.*
|
|
55
|
-
|
|
56
|
-
* 5.0.0 Now `50` results are returned by default, and up to `300` results can be returned if the `limit` parameter is used. These can be overridden by setting `mongoPaging.config.DEFAULT_LIMIT` and `mongoPaging.config.MAX_LIMIT` respectively.
|
|
57
|
-
|
|
58
|
-
* 4.1.1 Fixed bug that would overwrite `$or` in queries passed in.
|
|
59
|
-
|
|
60
|
-
* 4.1.0 Adds `sortAscending` option to sort by the `paginatedField` ascending. Defaults to false (existing behavior).
|
|
61
|
-
|
|
62
|
-
* 4.0.0 Breaking API change: `next` and `previous` attributes are now always returned with every response (in case the client wants to poll for new changes). New attributes `hasPrevious` and `hasNext` should now be used know if there are more results in the previous or next page. Before the change, `next` and `previously` could not be replied upon to know if there were more pages.
|
|
63
|
-
|
|
64
|
-
* 3.1.1 Don't use `let` for backwards compatibility.
|
|
65
|
-
|
|
66
|
-
* 3.1.0 `findInReq()` now accepts dot notation for fields. So you can pass `?fields=users.userId` to only turn the `userId` property for `users` in the response.
|
|
67
|
-
|
|
68
|
-
* 3.0.1 Fixed bug where the \_id field was always returned when a paginatedField was used.
|
|
69
|
-
|
|
70
|
-
* 3.0.0 Breaking API change: `find()` no longer accepts a string for `limit`. Added `findWithReq`.
|
|
71
|
-
|
|
72
|
-
* 2.0.0 Changed API to so you now set global config on the config object instead of the root export itself (e.g. `require('mongo-cursor-pagination').config.MAX_LIMIT = 100`). The default `MAX_LIMIT` is now a more reasonable 25 instead of 100. Added `search()`. Fixed edge case where pages will be incorrect if paginatedField has duplicate values.
|
|
73
|
-
|
|
74
|
-
* 1.1.0 Add `MAX_LIMIT` global setting to clamp
|
|
75
|
-
|
|
76
|
-
* 1.0.0 Initial release
|