@geekbears/gb-mongoose-query-parser 1.2.3 → 1.3.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.
package/README.md CHANGED
@@ -1,6 +1,5 @@
1
1
  # gb-mongoose-query-parser
2
2
 
3
- ## Moved to: [@geekbears/gb-mongoose-quey-parser](https://www.npmjs.com/package/@geekbears/gb-mongoose-query-parser)
4
3
 
5
4
  #### This is a fork from: [mongoose-quey-parser](https://github.com/leodinas-hao/mongoose-query-parser)
6
5
 
@@ -103,25 +102,6 @@ const parsed = parser.parse('${vip}&status=${sentStatus}&timestamp>2017-10-01&au
103
102
 
104
103
  ```
105
104
 
106
- ## NestJS Swagger Decorator
107
-
108
- This package includes a `@nestjs/swagger` decorator to simplify the documentation of query parameters. The `@ApiStandardQuery()` decorator acts as an alias to `@ApiQuery()` and wraps all the standard query parameters used by Geekbears on `find` and `count` operations.
109
-
110
- ### Usage
111
-
112
- Import the decorator and use it on the corresponding method on the controller class.
113
-
114
- ```typescript
115
- import { ApiStandardQuery } from '@geekbears/gb-mongoose-query-parser';
116
-
117
- @Get()
118
- @ApiStandardQuery()
119
- async find(@Query() query: any): Promise<any> {
120
- // Do something
121
- }
122
-
123
- ```
124
-
125
105
 
126
106
  ## Supported features
127
107
 
@@ -404,7 +384,7 @@ const parser = new MongooseQueryParser({
404
384
  int: val => parseInt(val, 10),
405
385
  },
406
386
  castParams: {
407
- key3: 'lowercase'
387
+ key3: 'lowercase'
408
388
  }});
409
389
  parser.parse('key1=lowercase(VALUE)&key2=int(10.5)&key3=ABC');
410
390
  // {
@@ -21,20 +21,47 @@ export declare class QueryParser {
21
21
  static parseQuery<T = any>(model: Model<T>, query: ParsedQs, count?: boolean): Query<any, T>;
22
22
  /**
23
23
  * Get all documents matching the provided query for a given model
24
+ * @deprecated The method should not be used, please use ```docsByQuery```
24
25
  * @param model a mongoose `Model` object to perform the query on
25
26
  * @param query a query string object received by the controller
26
27
  * @param count an optional flag that indicates wether a `count` operation should be performed
27
28
  * @returns the result of executing the provided query on the provided model
28
29
  */
29
30
  static docByQuery<T = any>(model: Model<T>, query: ParsedQs, count?: boolean): Promise<IDocByQueryRes<T>>;
31
+ /**
32
+ * Get all documents matching the provided query for a given model
33
+ * @param model a mongoose `Model` object to perform the query on
34
+ * @param query a query string object received by the controller
35
+ * @param count an optional flag that indicates wether a `count` operation should be performed
36
+ * @returns the result of executing the provided query on the provided model
37
+ */
38
+ static docsByQuery<T = any>(model: Model<T>, query: ParsedQs, count?: boolean): Promise<IDocByQueryRes<T>>;
30
39
  /**
31
40
  * Get all documents matching the provided query for a given model to a mongodb cursor resource
41
+ * @deprecated The method should not be used, please use ```docsByQueryToCursor```
32
42
  * @param model a mongoose `Model` object to perform the query on
33
43
  * @param query a query string object received by the controller
34
44
  * @param count an optional flag that indicates wether a `count` operation should be performed
35
45
  * @returns the cursor of executing the provided query on the provided model
36
46
  */
37
47
  static docByQueryToCursor<T = any>(model: Model<T>, query: ParsedQs): IDocByQueryCursorRes;
48
+ /**
49
+ * Get all documents matching the provided query for a given model to a mongodb cursor resource
50
+ * @param model a mongoose `Model` object to perform the query on
51
+ * @param query a query string object received by the controller
52
+ * @param count an optional flag that indicates wether a `count` operation should be performed
53
+ * @returns the cursor of executing the provided query on the provided model
54
+ */
55
+ static docsByQueryToCursor<T = any>(model: Model<T>, query: ParsedQs): IDocByQueryCursorRes;
56
+ /**
57
+ * Build a Mongoose `findOne` or `find` query on a given model with additional params
58
+ * @param model a Mongoose `Model` object to perform the query on
59
+ * @param singleMode this defines if the query should be for many or just one item
60
+ * @param filter a Mongoose filter query on the provided model
61
+ * @param query a query string object received by the controller
62
+ * @returns a Mongoose query on the provided model
63
+ */
64
+ static parseFromCustomFilterToQuery<T = any>(model: Model<T>, singleMode: boolean, filter: FilterQuery<T>, query: ParsedQs): Query<any, T>;
38
65
  /**
39
66
  * Build a Mongoose `findOne` query on a given model with additional params
40
67
  * @param model a Mongoose `Model` object to perform the query on
@@ -51,6 +78,24 @@ export declare class QueryParser {
51
78
  * @returns the result of executing the provided query on the provided model
52
79
  */
53
80
  static docByFilter<T = any>(model: Model<T>, filter: FilterQuery<T>, query: ParsedQs): Promise<IDocByQueryRes<T>>;
81
+ /**
82
+ * Get a document matching the provided query for a given model
83
+ * @param model a Mongoose `Model` object to perform the query on
84
+ * @param singleMode this defines if the query should be for many or just one item
85
+ * @param filter a Mongoose filter query on the provided model
86
+ * @param query a query string object received by the controller
87
+ * @returns the result of executing the provided query on the provided model
88
+ */
89
+ static docsFromCustomFilterToCursor<T = any>(model: Model<T>, singleMode: boolean, filter: FilterQuery<T>, query: ParsedQs): IDocByQueryCursorRes<T>;
90
+ /**
91
+ * Get a document matching the provided query for a given model
92
+ * @param model a Mongoose `Model` object to perform the query on
93
+ * @param singleMode this defines if the query should be for many or just one item
94
+ * @param filter a Mongoose filter query on the provided model
95
+ * @param query a query string object received by the controller
96
+ * @returns the result of executing the provided query on the provided model
97
+ */
98
+ static docsByFilter<T = any>(model: Model<T>, singleMode: boolean, filter: FilterQuery<T>, query: ParsedQs): Promise<IDocByQueryRes<T>>;
54
99
  /**
55
100
  * Build a Mongoose `findOneById` query on a given model with additional params
56
101
  * @param model a Mongoose `Model` object to perform the query on
@@ -96,6 +96,7 @@ var QueryParser = /** @class */ (function () {
96
96
  };
97
97
  /**
98
98
  * Get all documents matching the provided query for a given model
99
+ * @deprecated The method should not be used, please use ```docsByQuery```
99
100
  * @param model a mongoose `Model` object to perform the query on
100
101
  * @param query a query string object received by the controller
101
102
  * @param count an optional flag that indicates wether a `count` operation should be performed
@@ -121,8 +122,36 @@ var QueryParser = /** @class */ (function () {
121
122
  });
122
123
  });
123
124
  };
125
+ /**
126
+ * Get all documents matching the provided query for a given model
127
+ * @param model a mongoose `Model` object to perform the query on
128
+ * @param query a query string object received by the controller
129
+ * @param count an optional flag that indicates wether a `count` operation should be performed
130
+ * @returns the result of executing the provided query on the provided model
131
+ */
132
+ QueryParser.docsByQuery = function (model, query, count) {
133
+ if (count === void 0) { count = false; }
134
+ return __awaiter(this, void 0, void 0, function () {
135
+ var error_2;
136
+ var _a;
137
+ return __generator(this, function (_b) {
138
+ switch (_b.label) {
139
+ case 0:
140
+ _b.trys.push([0, 2, , 3]);
141
+ _a = {};
142
+ return [4 /*yield*/, QueryParser.parseQuery(model, query, count).exec()];
143
+ case 1: return [2 /*return*/, (_a.data = _b.sent(), _a)];
144
+ case 2:
145
+ error_2 = _b.sent();
146
+ throw new Error(error_2);
147
+ case 3: return [2 /*return*/];
148
+ }
149
+ });
150
+ });
151
+ };
124
152
  /**
125
153
  * Get all documents matching the provided query for a given model to a mongodb cursor resource
154
+ * @deprecated The method should not be used, please use ```docsByQueryToCursor```
126
155
  * @param model a mongoose `Model` object to perform the query on
127
156
  * @param query a query string object received by the controller
128
157
  * @param count an optional flag that indicates wether a `count` operation should be performed
@@ -138,6 +167,48 @@ var QueryParser = /** @class */ (function () {
138
167
  throw new Error(error);
139
168
  }
140
169
  };
170
+ /**
171
+ * Get all documents matching the provided query for a given model to a mongodb cursor resource
172
+ * @param model a mongoose `Model` object to perform the query on
173
+ * @param query a query string object received by the controller
174
+ * @param count an optional flag that indicates wether a `count` operation should be performed
175
+ * @returns the cursor of executing the provided query on the provided model
176
+ */
177
+ QueryParser.docsByQueryToCursor = function (model, query) {
178
+ try {
179
+ return {
180
+ data: QueryParser.parseQuery(model, query, false).cursor(),
181
+ };
182
+ }
183
+ catch (error) {
184
+ throw new Error(error);
185
+ }
186
+ };
187
+ /**
188
+ * Build a Mongoose `findOne` or `find` query on a given model with additional params
189
+ * @param model a Mongoose `Model` object to perform the query on
190
+ * @param singleMode this defines if the query should be for many or just one item
191
+ * @param filter a Mongoose filter query on the provided model
192
+ * @param query a query string object received by the controller
193
+ * @returns a Mongoose query on the provided model
194
+ */
195
+ QueryParser.parseFromCustomFilterToQuery = function (model, singleMode, filter, query) {
196
+ var docQuery;
197
+ docQuery = singleMode ? model.findOne(__assign({}, filter)) : model.find(__assign({}, filter));
198
+ if (!(0, lodash_1.isEmpty)(query)) {
199
+ var parser = new query_parser_1.MongooseQueryParser({
200
+ blacklist: ['filter', 'sort', 'limit', 'skip'],
201
+ });
202
+ var queryParams = parser.parse(query);
203
+ if (queryParams.populate)
204
+ docQuery = docQuery.populate(queryParams.populate);
205
+ if (queryParams.deepPopulate)
206
+ docQuery = docQuery.populate(queryParams.deepPopulate);
207
+ if (queryParams.select)
208
+ docQuery = docQuery.select(queryParams.select);
209
+ }
210
+ return docQuery;
211
+ };
141
212
  /**
142
213
  * Build a Mongoose `findOne` query on a given model with additional params
143
214
  * @param model a Mongoose `Model` object to perform the query on
@@ -171,7 +242,7 @@ var QueryParser = /** @class */ (function () {
171
242
  */
172
243
  QueryParser.docByFilter = function (model, filter, query) {
173
244
  return __awaiter(this, void 0, void 0, function () {
174
- var error_2;
245
+ var error_3;
175
246
  var _a;
176
247
  return __generator(this, function (_b) {
177
248
  switch (_b.label) {
@@ -182,8 +253,54 @@ var QueryParser = /** @class */ (function () {
182
253
  case 1: return [2 /*return*/, (_a.data = _b.sent(),
183
254
  _a)];
184
255
  case 2:
185
- error_2 = _b.sent();
186
- throw new Error(error_2);
256
+ error_3 = _b.sent();
257
+ throw new Error(error_3);
258
+ case 3: return [2 /*return*/];
259
+ }
260
+ });
261
+ });
262
+ };
263
+ /**
264
+ * Get a document matching the provided query for a given model
265
+ * @param model a Mongoose `Model` object to perform the query on
266
+ * @param singleMode this defines if the query should be for many or just one item
267
+ * @param filter a Mongoose filter query on the provided model
268
+ * @param query a query string object received by the controller
269
+ * @returns the result of executing the provided query on the provided model
270
+ */
271
+ QueryParser.docsFromCustomFilterToCursor = function (model, singleMode, filter, query) {
272
+ try {
273
+ return {
274
+ data: QueryParser.parseFromCustomFilterToQuery(model, singleMode, filter, query).cursor(),
275
+ };
276
+ }
277
+ catch (error) {
278
+ throw new Error(error);
279
+ }
280
+ };
281
+ /**
282
+ * Get a document matching the provided query for a given model
283
+ * @param model a Mongoose `Model` object to perform the query on
284
+ * @param singleMode this defines if the query should be for many or just one item
285
+ * @param filter a Mongoose filter query on the provided model
286
+ * @param query a query string object received by the controller
287
+ * @returns the result of executing the provided query on the provided model
288
+ */
289
+ QueryParser.docsByFilter = function (model, singleMode, filter, query) {
290
+ return __awaiter(this, void 0, void 0, function () {
291
+ var error_4;
292
+ var _a;
293
+ return __generator(this, function (_b) {
294
+ switch (_b.label) {
295
+ case 0:
296
+ _b.trys.push([0, 2, , 3]);
297
+ _a = {};
298
+ return [4 /*yield*/, QueryParser.parseFromCustomFilterToQuery(model, singleMode, filter, query).exec()];
299
+ case 1: return [2 /*return*/, (_a.data = _b.sent(),
300
+ _a)];
301
+ case 2:
302
+ error_4 = _b.sent();
303
+ throw new Error(error_4);
187
304
  case 3: return [2 /*return*/];
188
305
  }
189
306
  });
package/lib/test.spec.js CHANGED
@@ -48,6 +48,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
48
48
  var mocha_1 = require("@testdeck/mocha");
49
49
  var chai_1 = require("chai");
50
50
  var _1 = require("./");
51
+ var lodash_1 = require("lodash");
51
52
  var Tester = /** @class */ (function () {
52
53
  function Tester() {
53
54
  }
@@ -155,6 +156,28 @@ var Tester = /** @class */ (function () {
155
156
  chai_1.assert.notNestedProperty(parsedUndefined, 'lean');
156
157
  chai_1.assert.isFalse(parsedNotBoolean.lean);
157
158
  };
159
+ Tester.prototype.parseComplex = function () {
160
+ var parser = new _1.MongooseQueryParser();
161
+ var preDefined = {
162
+ $and: [
163
+ { $or: [{ __keywords: { $elemMatch: { $regex: 'Stranger', $options: 'i' } } }] },
164
+ { dailyRate: { $gte: 0, $lte: 60 } },
165
+ {
166
+ geolocation: {
167
+ $near: {
168
+ $geometry: { type: 'Point', coordinates: [-122.16106, 37.37646] },
169
+ $minDistance: 0,
170
+ $maxDistance: 10,
171
+ },
172
+ },
173
+ },
174
+ { unavailableDates: { $nin: [] } },
175
+ ],
176
+ };
177
+ var query = 'filter={"$and":[{"$or":[{"__keywords":{"$elemMatch":{"$regex":"Stranger","$options":"i"}}}]},{"dailyRate":{"$gte":0,"$lte":60}},{"geolocation":{"$near":{"$geometry":{"type":"Point","coordinates":[-122.16106,37.37646]},"$minDistance":0,"$maxDistance":10}}},{"unavailableDates":{"$nin":[]}}]}';
178
+ var parsed = parser.parse(query);
179
+ chai_1.assert.isOk((0, lodash_1.isEqual)(parsed.filter, preDefined));
180
+ };
158
181
  __decorate([
159
182
  (0, mocha_1.test)('should parse general query'),
160
183
  __metadata("design:type", Function),
@@ -203,6 +226,12 @@ var Tester = /** @class */ (function () {
203
226
  __metadata("design:paramtypes", []),
204
227
  __metadata("design:returntype", void 0)
205
228
  ], Tester.prototype, "parseLean", null);
229
+ __decorate([
230
+ (0, mocha_1.test)('should parse complex'),
231
+ __metadata("design:type", Function),
232
+ __metadata("design:paramtypes", []),
233
+ __metadata("design:returntype", void 0)
234
+ ], Tester.prototype, "parseComplex", null);
206
235
  Tester = __decorate([
207
236
  (0, mocha_1.suite)('Tester')
208
237
  ], Tester);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@geekbears/gb-mongoose-query-parser",
3
- "version": "1.2.3",
3
+ "version": "1.3.1",
4
4
  "description": "Convert url query string to MongooseJs friendly query object including advanced filtering, sorting, population, deep population, string template, type casting and many more...",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./lib/index.d.ts",