@orion-js/paginated-mongodb 3.12.0 → 3.13.1

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.
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const models_1 = require("@orion-js/models");
4
+ const resolvers_1 = require("@orion-js/resolvers");
5
+ exports.default = ({ collection, modelName }) => {
6
+ const items = (0, resolvers_1.modelResolver)({
7
+ returns: [collection.model],
8
+ async resolve(params) {
9
+ const { cursor } = params;
10
+ return await cursor.toArray();
11
+ }
12
+ });
13
+ return (0, models_1.createModel)({
14
+ name: modelName || `tokenPaginated${collection.model.name}`,
15
+ resolvers: {
16
+ items
17
+ }
18
+ });
19
+ };
@@ -0,0 +1,10 @@
1
+ export default function ({ collection, params, resolve, ...otherOptions }: {
2
+ [x: string]: any;
3
+ collection: any;
4
+ params: any;
5
+ resolve: any;
6
+ }): import("@orion-js/resolvers").Resolver<(params: any, viewer: any, info?: any) => Promise<{
7
+ params: any;
8
+ cursor: any;
9
+ viewer: any;
10
+ }>, undefined>;
@@ -0,0 +1,128 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const resolvers_1 = require("@orion-js/resolvers");
7
+ const getReturnModel_1 = __importDefault(require("./getReturnModel"));
8
+ const getArgs_1 = require("@orion-js/resolvers/lib/resolver/getArgs");
9
+ function default_1({ collection, params, resolve, ...otherOptions }) {
10
+ /* executes the resolve function, obtaining the query that will
11
+ be applied to the collection */
12
+ const runResolve = async (...args) => {
13
+ if (resolve) {
14
+ return await resolve(...args);
15
+ }
16
+ return { query: {} };
17
+ };
18
+ /* This function does the query to the collection. The logic is based
19
+ in this article:
20
+ https://medium.com/swlh/mongodb-pagination-fast-consistent-ece2a97070f3 */
21
+ const getCursor = async ({ query, sort: sortingCriteria, limit, idOffset }) => {
22
+ if (sortingCriteria && Object.keys(sortingCriteria).length > 1)
23
+ throw new Error('sorting criteria supports at most one field');
24
+ if (!sortingCriteria || !Object.keys(sortingCriteria).length) {
25
+ sortingCriteria = { _id: 1 };
26
+ if (idOffset)
27
+ query = { ...query, _id: { $gt: idOffset } };
28
+ }
29
+ else {
30
+ const sortingField = Object.keys(sortingCriteria)[0];
31
+ sortingCriteria = { ...sortingCriteria, _id: 1 };
32
+ if (idOffset) {
33
+ const offsetDocument = await collection.findOne({ _id: idOffset });
34
+ const { [sortingField]: originalSortingFieldQuery, ...restOfQuery } = query;
35
+ /* Suppose the following documents, and pages with 2 elements on each page:
36
+ [
37
+ {_id: 1, name: 'a', v: 1},
38
+ {_id: 2, name: 'b', v: 2},
39
+ {_id: 3, name: 'c', v: 2},
40
+ {_id: 4, name: 'd', v: 3},
41
+ {_id: 5, name: 'e', v: 4},
42
+ ]
43
+
44
+ If the query results are sorted by {v: 1}, then the first page will
45
+ contain the documents with ids 1 & 2, and the last one will be the document
46
+ with id 2.
47
+ In order to get the documents of the second page, we cannot get the documents
48
+ with the sorting criteria (v) greater than the last document, because we
49
+ would skip the document with id 3. In that case, we need to get all the
50
+ documents where either:
51
+ 1.- The sorting field is the same than the last one, and the _id field is greater
52
+ 2.- The sorting field is greater than the last one.
53
+
54
+ In this case, we can get the documents of the second page with the following query:
55
+
56
+ {
57
+ $or: [
58
+ {
59
+ v: 2,
60
+ _id: {$gt: 2}
61
+ },
62
+ {
63
+ v: {$gt: 2}
64
+ }
65
+ ]
66
+ }
67
+
68
+ For decreasing order it is exactly the same, but the second part of the or has
69
+ to be changed for $lt.
70
+ */
71
+ const sortOperator = sortingCriteria[sortingField] === 1 ? '$gt' : '$lt';
72
+ query = {
73
+ $or: [
74
+ { ...restOfQuery, [sortingField]: offsetDocument[sortingField], _id: { $gt: idOffset } },
75
+ { ...query, [sortingField]: { [sortOperator]: offsetDocument[sortingField] } }
76
+ ]
77
+ };
78
+ }
79
+ }
80
+ return collection.find(query).sort(sortingCriteria).limit(limit);
81
+ };
82
+ const validateQuery = async (query) => {
83
+ if (typeof query === 'object') {
84
+ const fields = Object.keys(query);
85
+ fields.forEach(field => {
86
+ if (['$or', '$expr'].includes(field)) {
87
+ throw new Error("tokenPaginatedResolvers don't support $or nor $expr on query");
88
+ }
89
+ if (typeof query[field] === 'object')
90
+ validateQuery(query[field]);
91
+ else if (Array.isArray(query[field]))
92
+ query[field].forEach(queryElement => validateQuery(queryElement));
93
+ });
94
+ }
95
+ };
96
+ const { modelName } = otherOptions;
97
+ return (0, resolvers_1.resolver)({
98
+ params: {
99
+ ...params,
100
+ idOffset: {
101
+ type: 'ID',
102
+ optional: true
103
+ },
104
+ limit: {
105
+ type: 'integer',
106
+ defaultValue: 10,
107
+ min: 1,
108
+ max: 200
109
+ }
110
+ },
111
+ returns: (0, getReturnModel_1.default)({ modelName, collection }),
112
+ async resolve(...args) {
113
+ const { params, viewer } = (0, getArgs_1.getArgs)(...args);
114
+ const { query, sort } = await runResolve(...args);
115
+ if (!query)
116
+ throw new Error("'query' object not found in return of resolve function");
117
+ await validateQuery(query);
118
+ const cursor = await getCursor({ ...params, query, sort });
119
+ return {
120
+ params,
121
+ cursor,
122
+ viewer
123
+ };
124
+ },
125
+ ...otherOptions
126
+ });
127
+ }
128
+ exports.default = default_1;
package/package.json CHANGED
@@ -1,45 +1,41 @@
1
1
  {
2
2
  "name": "@orion-js/paginated-mongodb",
3
- "version": "3.12.0",
4
- "main": "./dist/index.cjs",
5
- "types": "./dist/index.d.ts",
3
+ "version": "3.13.1",
4
+ "main": "lib/index.js",
5
+ "types": "lib/index.d.ts",
6
6
  "files": [
7
- "dist"
7
+ "/lib"
8
8
  ],
9
9
  "author": "nicolaslopezj",
10
10
  "license": "MIT",
11
11
  "scripts": {
12
- "test": "bun test",
12
+ "test": "jest",
13
13
  "prepare": "yarn run build",
14
- "clean": "rm -rf ./dist",
15
- "build": "bun run build.ts",
14
+ "clean": "rm -rf ./lib",
15
+ "build": "yarn run clean && tsc",
16
16
  "watch": "tsc -w",
17
- "upgrade-interactive": "yarn upgrade-interactive",
18
- "dev": "bun --watch src/index.ts"
17
+ "upgrade-interactive": "yarn upgrade-interactive"
19
18
  },
20
19
  "dependencies": {
21
- "@orion-js/helpers": "3.12.0",
22
- "@orion-js/models": "3.12.0",
23
- "@orion-js/mongodb": "3.12.0",
24
- "@orion-js/resolvers": "3.12.0",
25
- "@orion-js/schema": "3.12.0",
26
- "@orion-js/services": "3.12.0",
27
- "@orion-js/typed-model": "3.12.0"
20
+ "@orion-js/helpers": "^3.13.1",
21
+ "@orion-js/models": "^3.13.1",
22
+ "@orion-js/mongodb": "^3.13.1",
23
+ "@orion-js/resolvers": "^3.13.1",
24
+ "@orion-js/schema": "^3.13.1",
25
+ "@orion-js/services": "^3.13.1",
26
+ "@orion-js/typed-model": "^3.13.1"
28
27
  },
29
28
  "devDependencies": {
29
+ "@shelf/jest-mongodb": "^4.3.2",
30
+ "@types/jest": "^29.5.12",
30
31
  "@types/lodash": "4.14.176",
31
- "typescript": "^5.4.5",
32
- "@types/bun": "^1.2.4"
32
+ "@types/node": "16.11.7",
33
+ "jest": "29.7.0",
34
+ "ts-jest": "29.1.2",
35
+ "typescript": "^4.4.4"
33
36
  },
34
37
  "publishConfig": {
35
38
  "access": "public"
36
39
  },
37
- "gitHead": "9fd28b6f6b348cebc9f0dc207805647969277372",
38
- "type": "module",
39
- "module": "./dist/index.js",
40
- "exports": {
41
- "types": "./dist/index.d.ts",
42
- "import": "./dist/index.js",
43
- "require": "./dist/index.cjs"
44
- }
45
- }
40
+ "gitHead": "18ab5a9711863e6bd3d3c61e7bb8f9067afca82c"
41
+ }