@orion-js/paginated-mongodb 3.11.15 → 3.12.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/dist/index.cjs +18878 -0
- package/dist/index.d.ts +58 -0
- package/dist/index.js +18850 -0
- package/package.json +27 -23
- package/LICENSE +0 -21
- package/lib/index.d.ts +0 -4
- package/lib/index.js +0 -21
- package/lib/paginatedResolver/getModel.d.ts +0 -5
- package/lib/paginatedResolver/getModel.js +0 -77
- package/lib/paginatedResolver/hash.d.ts +0 -1
- package/lib/paginatedResolver/hash.js +0 -8
- package/lib/paginatedResolver/index.d.ts +0 -36
- package/lib/paginatedResolver/index.js +0 -44
- package/lib/paginatedResolver/index.test.d.ts +0 -1
- package/lib/paginatedResolver/index.test.js +0 -127
- package/lib/paginatedResolver/params.d.ts +0 -5
- package/lib/paginatedResolver/params.js +0 -52
- package/lib/paginatedResolver/params.test.d.ts +0 -1
- package/lib/paginatedResolver/params.test.js +0 -149
- package/lib/paginatedResolver/setOptions.d.ts +0 -10
- package/lib/paginatedResolver/setOptions.js +0 -21
- package/lib/service/index.d.ts +0 -5
- package/lib/service/index.js +0 -23
- package/lib/tests/setup.d.ts +0 -1
- package/lib/tests/setup.js +0 -19
- package/lib/tokenPaginatedResolver/getReturnModel.d.ts +0 -5
- package/lib/tokenPaginatedResolver/getReturnModel.js +0 -19
- package/lib/tokenPaginatedResolver/index.d.ts +0 -10
- package/lib/tokenPaginatedResolver/index.js +0 -128
package/lib/service/index.d.ts
DELETED
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
import { PaginatedResolverOpts } from '../paginatedResolver';
|
|
2
|
-
export interface PagiantedQueryDescriptor extends Omit<PropertyDecorator, 'value'> {
|
|
3
|
-
value?: PaginatedResolverOpts['getCursor'];
|
|
4
|
-
}
|
|
5
|
-
export declare function PaginatedQuery(options: Omit<PaginatedResolverOpts, 'getCursor'>): (target: any, propertyKey: string, descriptor: PagiantedQueryDescriptor) => void;
|
package/lib/service/index.js
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
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
|
-
exports.PaginatedQuery = void 0;
|
|
7
|
-
const paginatedResolver_1 = __importDefault(require("../paginatedResolver"));
|
|
8
|
-
const services_1 = require("@orion-js/services");
|
|
9
|
-
function PaginatedQuery(options) {
|
|
10
|
-
return function (target, propertyKey, descriptor) {
|
|
11
|
-
if (!descriptor.value)
|
|
12
|
-
throw new Error(`You must pass resolver function to ${propertyKey}`);
|
|
13
|
-
target.resolvers = target.resolvers || {};
|
|
14
|
-
target.resolvers[propertyKey] = (0, paginatedResolver_1.default)({
|
|
15
|
-
...options,
|
|
16
|
-
getCursor: async (params, viewer) => {
|
|
17
|
-
const instance = (0, services_1.getInstance)(target.service);
|
|
18
|
-
return await instance[propertyKey](params, viewer);
|
|
19
|
-
}
|
|
20
|
-
});
|
|
21
|
-
};
|
|
22
|
-
}
|
|
23
|
-
exports.PaginatedQuery = PaginatedQuery;
|
package/lib/tests/setup.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/lib/tests/setup.js
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const mongodb_1 = require("@orion-js/mongodb");
|
|
4
|
-
const url = `${global.__MONGO_URI__}jest`;
|
|
5
|
-
process.env.MONGO_URL = url;
|
|
6
|
-
beforeAll(async () => {
|
|
7
|
-
const connection = (0, mongodb_1.getMongoConnection)({ name: 'main' });
|
|
8
|
-
await connection.connectionPromise;
|
|
9
|
-
});
|
|
10
|
-
afterAll(async () => {
|
|
11
|
-
/**
|
|
12
|
-
* We need to wait on indexes promises to be resolved to close all the handlers
|
|
13
|
-
*/
|
|
14
|
-
await Promise.all(mongodb_1.createIndexesPromises);
|
|
15
|
-
for (const connectionName in mongodb_1.connections) {
|
|
16
|
-
const connection = mongodb_1.connections[connectionName];
|
|
17
|
-
await connection.client.close();
|
|
18
|
-
}
|
|
19
|
-
});
|
|
@@ -1,19 +0,0 @@
|
|
|
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
|
-
};
|
|
@@ -1,10 +0,0 @@
|
|
|
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>;
|
|
@@ -1,128 +0,0 @@
|
|
|
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;
|