@geekbears/gb-mongoose-query-parser 1.3.16 → 1.3.17

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.
@@ -112,4 +112,10 @@ export declare class QueryParser {
112
112
  * @returns the result of executing the provided query on the provided model
113
113
  */
114
114
  static docById<T = any>(model: Model<T>, _id: string, query: ParsedQs): Promise<IDocByQueryRes<T>>;
115
+ /**
116
+ * Transforms the filter param to correctly support `ObjectId`s
117
+ * @param filter the filter param
118
+ * @returns a casted filter
119
+ */
120
+ static transformFilter<T = any>(filter: T): T;
115
121
  }
@@ -1,64 +1,15 @@
1
1
  "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
- return new (P || (P = Promise))(function (resolve, reject) {
16
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
- step((generator = generator.apply(thisArg, _arguments || [])).next());
20
- });
21
- };
22
- var __generator = (this && this.__generator) || function (thisArg, body) {
23
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
24
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
- function verb(n) { return function (v) { return step([n, v]); }; }
26
- function step(op) {
27
- if (f) throw new TypeError("Generator is already executing.");
28
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
29
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
30
- if (y = 0, t) op = [op[0] & 2, t.value];
31
- switch (op[0]) {
32
- case 0: case 1: t = op; break;
33
- case 4: _.label++; return { value: op[1], done: false };
34
- case 5: _.label++; y = op[1]; op = [0]; continue;
35
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
- default:
37
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
- if (t[2]) _.ops.pop();
42
- _.trys.pop(); continue;
43
- }
44
- op = body.call(thisArg, _);
45
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
- }
48
- };
49
2
  Object.defineProperty(exports, "__esModule", { value: true });
50
3
  exports.QueryParser = void 0;
51
- var mongoose_1 = require("mongoose");
52
- var lodash_1 = require("lodash");
53
- var query_parser_1 = require("./query-parser");
54
- var class_validator_1 = require("class-validator");
4
+ const mongoose_1 = require("mongoose");
5
+ const lodash_1 = require("lodash");
6
+ const query_parser_1 = require("./query-parser");
7
+ const class_validator_1 = require("class-validator");
55
8
  /**
56
9
  * Helper class
57
10
  * Implements the `MongooseQueryParser` and exposes a single method to execute queries on models
58
11
  */
59
- var QueryParser = /** @class */ (function () {
60
- function QueryParser() {
61
- }
12
+ class QueryParser {
62
13
  /**
63
14
  * Build a Mongoose query on a given model from the parsed query string
64
15
  * @param model a mongoose `Model` object to perform the query on
@@ -66,16 +17,15 @@ var QueryParser = /** @class */ (function () {
66
17
  * @param count an optional flag that indicates wether a `count` operation should be performed
67
18
  * @returns a Mongoose query on the provided model
68
19
  */
69
- QueryParser.parseQuery = function (model, query, count) {
70
- if (count === void 0) { count = false; }
71
- var docsQuery;
72
- var countQueryDocuments;
20
+ static parseQuery(model, query, count = false) {
21
+ let docsQuery;
22
+ let countQueryDocuments;
73
23
  docsQuery = model.find({});
74
24
  countQueryDocuments = count ? model.countDocuments() : null;
75
25
  if (!(0, lodash_1.isEmpty)(query)) {
76
- var parser = new query_parser_1.MongooseQueryParser();
77
- var queryParams = parser.parse(query);
78
- (0, lodash_1.forOwn)(transformFilter(queryParams.filter), function (value, key) {
26
+ const parser = new query_parser_1.MongooseQueryParser();
27
+ const queryParams = parser.parse(query);
28
+ (0, lodash_1.forOwn)(this.transformFilter(queryParams.filter), (value, key) => {
79
29
  if ((0, class_validator_1.isMongoId)(value))
80
30
  value = mongoose_1.Types.ObjectId.createFromHexString(value.toString());
81
31
  docsQuery = docsQuery.where(key, value);
@@ -96,7 +46,7 @@ var QueryParser = /** @class */ (function () {
96
46
  docsQuery = docsQuery.skip(queryParams.skip);
97
47
  }
98
48
  return count && countQueryDocuments ? countQueryDocuments : docsQuery;
99
- };
49
+ }
100
50
  /**
101
51
  * Get all documents matching the provided query for a given model
102
52
  * @deprecated The method should not be used, please use ```docsByQuery```
@@ -105,26 +55,14 @@ var QueryParser = /** @class */ (function () {
105
55
  * @param count an optional flag that indicates wether a `count` operation should be performed
106
56
  * @returns the result of executing the provided query on the provided model
107
57
  */
108
- QueryParser.docByQuery = function (model_1, query_1) {
109
- return __awaiter(this, arguments, void 0, function (model, query, count) {
110
- var error_1;
111
- var _a;
112
- if (count === void 0) { count = false; }
113
- return __generator(this, function (_b) {
114
- switch (_b.label) {
115
- case 0:
116
- _b.trys.push([0, 2, , 3]);
117
- _a = {};
118
- return [4 /*yield*/, QueryParser.parseQuery(model, query, count).exec()];
119
- case 1: return [2 /*return*/, (_a.data = _b.sent(), _a)];
120
- case 2:
121
- error_1 = _b.sent();
122
- throw new Error(error_1);
123
- case 3: return [2 /*return*/];
124
- }
125
- });
126
- });
127
- };
58
+ static async docByQuery(model, query, count = false) {
59
+ try {
60
+ return { data: await QueryParser.parseQuery(model, query, count).exec() };
61
+ }
62
+ catch (error) {
63
+ throw new Error(error);
64
+ }
65
+ }
128
66
  /**
129
67
  * Get all documents matching the provided query for a given model
130
68
  * @param model a mongoose `Model` object to perform the query on
@@ -132,26 +70,14 @@ var QueryParser = /** @class */ (function () {
132
70
  * @param count an optional flag that indicates wether a `count` operation should be performed
133
71
  * @returns the result of executing the provided query on the provided model
134
72
  */
135
- QueryParser.docsByQuery = function (model_1, query_1) {
136
- return __awaiter(this, arguments, void 0, function (model, query, count) {
137
- var error_2;
138
- var _a;
139
- if (count === void 0) { count = false; }
140
- return __generator(this, function (_b) {
141
- switch (_b.label) {
142
- case 0:
143
- _b.trys.push([0, 2, , 3]);
144
- _a = {};
145
- return [4 /*yield*/, QueryParser.parseQuery(model, query, count).exec()];
146
- case 1: return [2 /*return*/, (_a.data = _b.sent(), _a)];
147
- case 2:
148
- error_2 = _b.sent();
149
- throw new Error(error_2);
150
- case 3: return [2 /*return*/];
151
- }
152
- });
153
- });
154
- };
73
+ static async docsByQuery(model, query, count = false) {
74
+ try {
75
+ return { data: await QueryParser.parseQuery(model, query, count).exec() };
76
+ }
77
+ catch (error) {
78
+ throw new Error(error);
79
+ }
80
+ }
155
81
  /**
156
82
  * Get all documents matching the provided query for a given model to a mongodb cursor resource
157
83
  * @deprecated The method should not be used, please use ```docsByQueryToCursor```
@@ -160,7 +86,7 @@ var QueryParser = /** @class */ (function () {
160
86
  * @param count an optional flag that indicates wether a `count` operation should be performed
161
87
  * @returns the cursor of executing the provided query on the provided model
162
88
  */
163
- QueryParser.docByQueryToCursor = function (model, query) {
89
+ static docByQueryToCursor(model, query) {
164
90
  try {
165
91
  return {
166
92
  data: QueryParser.parseQuery(model, query, false).cursor(),
@@ -169,7 +95,7 @@ var QueryParser = /** @class */ (function () {
169
95
  catch (error) {
170
96
  throw new Error(error);
171
97
  }
172
- };
98
+ }
173
99
  /**
174
100
  * Get all documents matching the provided query for a given model to a mongodb cursor resource
175
101
  * @param model a mongoose `Model` object to perform the query on
@@ -177,7 +103,7 @@ var QueryParser = /** @class */ (function () {
177
103
  * @param count an optional flag that indicates wether a `count` operation should be performed
178
104
  * @returns the cursor of executing the provided query on the provided model
179
105
  */
180
- QueryParser.docsByQueryToCursor = function (model, query) {
106
+ static docsByQueryToCursor(model, query) {
181
107
  try {
182
108
  return {
183
109
  data: QueryParser.parseQuery(model, query, false).cursor(),
@@ -186,7 +112,7 @@ var QueryParser = /** @class */ (function () {
186
112
  catch (error) {
187
113
  throw new Error(error);
188
114
  }
189
- };
115
+ }
190
116
  /**
191
117
  * Build a Mongoose `findOne` or `find` query on a given model with additional params
192
118
  * @param model a Mongoose `Model` object to perform the query on
@@ -195,14 +121,14 @@ var QueryParser = /** @class */ (function () {
195
121
  * @param query a query string object received by the controller
196
122
  * @returns a Mongoose query on the provided model
197
123
  */
198
- QueryParser.parseFromCustomFilterToQuery = function (model, singleMode, filter, query) {
199
- var docQuery;
200
- docQuery = singleMode ? model.findOne(__assign({}, filter)) : model.find(__assign({}, filter));
124
+ static parseFromCustomFilterToQuery(model, singleMode, filter, query) {
125
+ let docQuery;
126
+ docQuery = singleMode ? model.findOne({ ...filter }) : model.find({ ...filter });
201
127
  if (!(0, lodash_1.isEmpty)(query)) {
202
- var parser = new query_parser_1.MongooseQueryParser({
128
+ const parser = new query_parser_1.MongooseQueryParser({
203
129
  blacklist: ['filter', 'sort', 'limit', 'skip'],
204
130
  });
205
- var queryParams = parser.parse(query);
131
+ const queryParams = parser.parse(query);
206
132
  if (queryParams.populate)
207
133
  docQuery = docQuery.populate(queryParams.populate);
208
134
  if (queryParams.deepPopulate)
@@ -211,7 +137,7 @@ var QueryParser = /** @class */ (function () {
211
137
  docQuery = docQuery.select(queryParams.select);
212
138
  }
213
139
  return docQuery;
214
- };
140
+ }
215
141
  /**
216
142
  * Build a Mongoose `findOne` query on a given model with additional params
217
143
  * @param model a Mongoose `Model` object to perform the query on
@@ -219,14 +145,14 @@ var QueryParser = /** @class */ (function () {
219
145
  * @param query a query string object received by the controller
220
146
  * @returns a Mongoose query on the provided model
221
147
  */
222
- QueryParser.parseFilterQuery = function (model, filter, query) {
223
- var docQuery;
224
- docQuery = model.findOne(__assign({}, filter));
148
+ static parseFilterQuery(model, filter, query) {
149
+ let docQuery;
150
+ docQuery = model.findOne({ ...filter });
225
151
  if (!(0, lodash_1.isEmpty)(query)) {
226
- var parser = new query_parser_1.MongooseQueryParser({
152
+ const parser = new query_parser_1.MongooseQueryParser({
227
153
  blacklist: ['filter', 'sort', 'limit', 'skip'],
228
154
  });
229
- var queryParams = parser.parse(query);
155
+ const queryParams = parser.parse(query);
230
156
  if (queryParams.populate)
231
157
  docQuery = docQuery.populate(queryParams.populate);
232
158
  if (queryParams.deepPopulate)
@@ -235,7 +161,7 @@ var QueryParser = /** @class */ (function () {
235
161
  docQuery = docQuery.select(queryParams.select);
236
162
  }
237
163
  return docQuery;
238
- };
164
+ }
239
165
  /**
240
166
  * Get a document matching the provided query for a given model
241
167
  * @param model a Mongoose `Model` object to perform the query on
@@ -243,26 +169,16 @@ var QueryParser = /** @class */ (function () {
243
169
  * @param query a query string object received by the controller
244
170
  * @returns the result of executing the provided query on the provided model
245
171
  */
246
- QueryParser.docByFilter = function (model, filter, query) {
247
- return __awaiter(this, void 0, void 0, function () {
248
- var error_3;
249
- var _a;
250
- return __generator(this, function (_b) {
251
- switch (_b.label) {
252
- case 0:
253
- _b.trys.push([0, 2, , 3]);
254
- _a = {};
255
- return [4 /*yield*/, QueryParser.parseFilterQuery(model, filter, query).exec()];
256
- case 1: return [2 /*return*/, (_a.data = _b.sent(),
257
- _a)];
258
- case 2:
259
- error_3 = _b.sent();
260
- throw new Error(error_3);
261
- case 3: return [2 /*return*/];
262
- }
263
- });
264
- });
265
- };
172
+ static async docByFilter(model, filter, query) {
173
+ try {
174
+ return {
175
+ data: await QueryParser.parseFilterQuery(model, filter, query).exec(),
176
+ };
177
+ }
178
+ catch (error) {
179
+ throw new Error(error);
180
+ }
181
+ }
266
182
  /**
267
183
  * Get a document matching the provided query for a given model
268
184
  * @param model a Mongoose `Model` object to perform the query on
@@ -271,7 +187,7 @@ var QueryParser = /** @class */ (function () {
271
187
  * @param query a query string object received by the controller
272
188
  * @returns the result of executing the provided query on the provided model
273
189
  */
274
- QueryParser.docsFromCustomFilterToCursor = function (model, singleMode, filter, query) {
190
+ static docsFromCustomFilterToCursor(model, singleMode, filter, query) {
275
191
  try {
276
192
  return {
277
193
  data: QueryParser.parseFromCustomFilterToQuery(model, singleMode, filter, query).cursor(),
@@ -280,7 +196,7 @@ var QueryParser = /** @class */ (function () {
280
196
  catch (error) {
281
197
  throw new Error(error);
282
198
  }
283
- };
199
+ }
284
200
  /**
285
201
  * Get a document matching the provided query for a given model
286
202
  * @param model a Mongoose `Model` object to perform the query on
@@ -289,26 +205,16 @@ var QueryParser = /** @class */ (function () {
289
205
  * @param query a query string object received by the controller
290
206
  * @returns the result of executing the provided query on the provided model
291
207
  */
292
- QueryParser.docsByFilter = function (model, singleMode, filter, query) {
293
- return __awaiter(this, void 0, void 0, function () {
294
- var error_4;
295
- var _a;
296
- return __generator(this, function (_b) {
297
- switch (_b.label) {
298
- case 0:
299
- _b.trys.push([0, 2, , 3]);
300
- _a = {};
301
- return [4 /*yield*/, QueryParser.parseFromCustomFilterToQuery(model, singleMode, filter, query).exec()];
302
- case 1: return [2 /*return*/, (_a.data = _b.sent(),
303
- _a)];
304
- case 2:
305
- error_4 = _b.sent();
306
- throw new Error(error_4);
307
- case 3: return [2 /*return*/];
308
- }
309
- });
310
- });
311
- };
208
+ static async docsByFilter(model, singleMode, filter, query) {
209
+ try {
210
+ return {
211
+ data: await QueryParser.parseFromCustomFilterToQuery(model, singleMode, filter, query).exec(),
212
+ };
213
+ }
214
+ catch (error) {
215
+ throw new Error(error);
216
+ }
217
+ }
312
218
  /**
313
219
  * Build a Mongoose `findOneById` query on a given model with additional params
314
220
  * @param model a Mongoose `Model` object to perform the query on
@@ -316,9 +222,9 @@ var QueryParser = /** @class */ (function () {
316
222
  * @param query a query string object received by the controller
317
223
  * @returns a Mongoose query on the provided model
318
224
  */
319
- QueryParser.parseByIdQuery = function (model, _id, query) {
320
- return QueryParser.parseFilterQuery(model, { _id: _id }, query);
321
- };
225
+ static parseByIdQuery(model, _id, query) {
226
+ return QueryParser.parseFilterQuery(model, { _id }, query);
227
+ }
322
228
  /**
323
229
  * Get a document matching the provided query for a given model
324
230
  * @param model a Mongoose `Model` object to perform the query on
@@ -326,46 +232,42 @@ var QueryParser = /** @class */ (function () {
326
232
  * @param query a query string object received by the controller
327
233
  * @returns the result of executing the provided query on the provided model
328
234
  */
329
- QueryParser.docById = function (model, _id, query) {
330
- return QueryParser.docByFilter(model, { _id: _id }, query);
331
- };
332
- return QueryParser;
333
- }());
334
- exports.QueryParser = QueryParser;
335
- /**
336
- * Transforms the filter param to correctly support `ObjectId`s
337
- * @param filter the filter param
338
- * @returns a casted filter
339
- */
340
- function transformFilter(filter) {
341
- var walk = function (obj) {
342
- var _loop_1 = function (i, key) {
343
- if (Object.prototype.hasOwnProperty.call(obj, key)) {
344
- var value = obj[key];
345
- var validId = (0, mongoose_1.isValidObjectId)(value);
346
- if (validId && (0, class_validator_1.isMongoId)(value))
347
- obj[key] = mongoose_1.Types.ObjectId.createFromHexString(value.toString());
348
- else {
349
- if (i % 1000 === 0) {
350
- setImmediate(function () {
235
+ static docById(model, _id, query) {
236
+ return QueryParser.docByFilter(model, { _id }, query);
237
+ }
238
+ /**
239
+ * Transforms the filter param to correctly support `ObjectId`s
240
+ * @param filter the filter param
241
+ * @returns a casted filter
242
+ */
243
+ static transformFilter(filter) {
244
+ const walk = (obj) => {
245
+ for (const [i, [key, value]] of Object.entries(obj).entries()) {
246
+ if (Object.prototype.hasOwnProperty.call(obj, key)) {
247
+ const validId = (0, mongoose_1.isObjectIdOrHexString)(value);
248
+ if (validId && (0, class_validator_1.isMongoId)(value)) {
249
+ obj[key] = mongoose_1.Types.ObjectId.createFromHexString(value.toString());
250
+ }
251
+ else {
252
+ if (i % 1000 === 0) {
253
+ setImmediate(() => {
254
+ walk(obj[key]);
255
+ });
256
+ }
257
+ else
351
258
  walk(obj[key]);
352
- });
353
259
  }
354
- else
355
- walk(obj[key]);
260
+ // if (typeof value === 'object') {
261
+ // console.log('walking......');
262
+ // walk(obj[key]);
263
+ // } else obj[key] = Types.ObjectId.isValid(value) ? new Types.ObjectId(value) : value;
356
264
  }
357
- // if (typeof value === 'object') {
358
- // console.log('walking......');
359
- // walk(obj[key]);
360
- // } else obj[key] = Types.ObjectId.isValid(value) ? new Types.ObjectId(value) : value;
361
265
  }
266
+ return obj;
362
267
  };
363
- for (var _i = 0, obj_1 = obj; _i < obj_1.length; _i++) {
364
- var _a = obj_1[_i], i = _a[0], key = _a[1][0];
365
- _loop_1(i, key);
366
- }
367
- return obj;
368
- };
369
- return walk(__assign({}, filter));
268
+ const res = walk({ ...filter });
269
+ return res;
270
+ }
370
271
  }
272
+ exports.QueryParser = QueryParser;
371
273
  //# sourceMappingURL=query-parser-helper.js.map