@geekbears/gb-mongoose-query-parser 1.2.1 → 1.3.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 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
  // {
package/lib/index.d.ts CHANGED
@@ -1,4 +1,3 @@
1
1
  export * from './query-parser';
2
2
  export * from './query-parser-helper';
3
- export * from './decorators';
4
3
  export * from './types';
package/lib/index.js CHANGED
@@ -1,7 +1,11 @@
1
1
  "use strict";
2
2
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
3
  if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
5
9
  }) : (function(o, m, k, k2) {
6
10
  if (k2 === undefined) k2 = k;
7
11
  o[k2] = m[k];
@@ -12,6 +16,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
12
16
  Object.defineProperty(exports, "__esModule", { value: true });
13
17
  __exportStar(require("./query-parser"), exports);
14
18
  __exportStar(require("./query-parser-helper"), exports);
15
- __exportStar(require("./decorators"), exports);
16
19
  __exportStar(require("./types"), exports);
17
20
  //# sourceMappingURL=index.js.map
@@ -25,7 +25,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
25
25
  function verb(n) { return function (v) { return step([n, v]); }; }
26
26
  function step(op) {
27
27
  if (f) throw new TypeError("Generator is already executing.");
28
- while (_) try {
28
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
29
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
30
  if (y = 0, t) op = [op[0] & 2, t.value];
31
31
  switch (op[0]) {
@@ -2,12 +2,12 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.MongooseQueryParser = void 0;
4
4
  var Moment = require("moment");
5
- var qs = require("querystring");
6
- var _ = require("lodash");
5
+ var qs_1 = require("qs");
6
+ var lodash_1 = require("lodash");
7
7
  var MongooseQueryParser = /** @class */ (function () {
8
8
  function MongooseQueryParser(options) {
9
- var _this = this;
10
9
  if (options === void 0) { options = {}; }
10
+ var _this = this;
11
11
  this.options = options;
12
12
  this.defaultDateFormat = [Moment.ISO_8601];
13
13
  this.builtInCaster = {
@@ -18,7 +18,7 @@ var MongooseQueryParser = /** @class */ (function () {
18
18
  return m.toDate();
19
19
  }
20
20
  else {
21
- throw new Error("Invalid date string: [" + val + "]");
21
+ throw new Error("Invalid date string: [".concat(val, "]"));
22
22
  }
23
23
  },
24
24
  };
@@ -40,7 +40,7 @@ var MongooseQueryParser = /** @class */ (function () {
40
40
  this.options.blacklist = options.blacklist || [];
41
41
  this.operators.forEach(function (_a) {
42
42
  var operator = _a.operator, method = _a.method, defaultKey = _a.defaultKey;
43
- _this.options.blacklist.push(_this.options[operator + "Key"] || defaultKey);
43
+ _this.options.blacklist.push(_this.options["".concat(operator, "Key")] || defaultKey);
44
44
  });
45
45
  }
46
46
  /**
@@ -51,12 +51,12 @@ var MongooseQueryParser = /** @class */ (function () {
51
51
  */
52
52
  MongooseQueryParser.prototype.parse = function (query, context) {
53
53
  var _this = this;
54
- var params = _.isString(query) ? qs.parse(query) : query;
54
+ var params = (0, lodash_1.isString)(query) ? (0, qs_1.parse)(query) : query;
55
55
  var options = this.options;
56
56
  var result = {};
57
57
  this.operators.forEach(function (_a) {
58
58
  var operator = _a.operator, method = _a.method, defaultKey = _a.defaultKey;
59
- var key = options[operator + "Key"] || defaultKey;
59
+ var key = options["".concat(operator, "Key")] || defaultKey;
60
60
  var value = params[key];
61
61
  if (value || operator === 'filter') {
62
62
  result[operator] = method.call(_this, value, params);
@@ -125,7 +125,7 @@ var MongooseQueryParser = /** @class */ (function () {
125
125
  var parsedFilter = filter ? this.parseFilter(filter) : {};
126
126
  return Object.keys(params)
127
127
  .map(function (val) {
128
- var join = params[val] ? val + "=" + params[val] : val;
128
+ var join = params[val] ? "".concat(val, "=").concat(params[val]) : val;
129
129
  // Separate key, operators and value
130
130
  var _a = join.match(/(!?)([^><!=]+)([><]=?|!?=|)(.*)/), prefix = _a[1], key = _a[2], op = _a[3], value = _a[4];
131
131
  return { prefix: prefix, key: key, op: _this.parseOperator(op), value: _this.parseValue(value, key) };
@@ -165,7 +165,7 @@ var MongooseQueryParser = /** @class */ (function () {
165
165
  return JSON.parse(filter);
166
166
  }
167
167
  catch (err) {
168
- throw new Error("Invalid JSON string: " + filter);
168
+ throw new Error("Invalid JSON string: ".concat(filter));
169
169
  }
170
170
  };
171
171
  MongooseQueryParser.prototype.parseOperator = function (operator) {
@@ -270,7 +270,7 @@ var MongooseQueryParser = /** @class */ (function () {
270
270
  return JSON.parse(deepPopulate);
271
271
  }
272
272
  catch (err) {
273
- throw new Error("Invalid JSON string: " + deepPopulate);
273
+ throw new Error("Invalid JSON string: ".concat(deepPopulate));
274
274
  }
275
275
  };
276
276
  /**
@@ -289,7 +289,7 @@ var MongooseQueryParser = /** @class */ (function () {
289
289
  */
290
290
  MongooseQueryParser.prototype.parseUnaries = function (unaries, values) {
291
291
  if (values === void 0) { values = { plus: 1, minus: -1 }; }
292
- var unariesAsArray = _.isString(unaries) ? unaries.split(',') : unaries;
292
+ var unariesAsArray = (0, lodash_1.isString)(unaries) ? unaries.split(',') : unaries;
293
293
  return unariesAsArray
294
294
  .map(function (x) { return x.match(/^(\+|-)?(.*)/); })
295
295
  .reduce(function (result, _a) {
@@ -341,51 +341,51 @@ var MongooseQueryParser = /** @class */ (function () {
341
341
  var match = str.match(reg);
342
342
  var val = undefined;
343
343
  if (match) {
344
- val = _.property(match[1])(context);
344
+ val = (0, lodash_1.property)(match[1])(context);
345
345
  if (val === undefined) {
346
- throw new Error("No predefined query found for the provided reference [" + match[1] + "]");
346
+ throw new Error("No predefined query found for the provided reference [".concat(match[1], "]"));
347
347
  }
348
348
  }
349
349
  return { match: !!match, val: val };
350
350
  };
351
351
  var _transform_1 = function (obj) {
352
- return _.reduce(obj, function (prev, curr, key) {
352
+ return (0, lodash_1.reduce)(obj, function (prev, curr, key) {
353
353
  var _a, _b;
354
354
  var val = undefined, match = undefined;
355
- if (_.isString(key)) {
355
+ if ((0, lodash_1.isString)(key)) {
356
356
  (_a = _match_1(key), match = _a.match, val = _a.val);
357
357
  if (match) {
358
- if (_.has(curr, '$exists')) {
358
+ if ((0, lodash_1.has)(curr, '$exists')) {
359
359
  // 1). as a key: {'${qry}': {$exits: true}} => {${qry object}}
360
- return _.merge(prev, val);
360
+ return (0, lodash_1.merge)(prev, val);
361
361
  }
362
- else if (_.isString(val)) {
362
+ else if ((0, lodash_1.isString)(val)) {
363
363
  // 1). as a key: {'${qry}': 'something'} => {'${qry object}': 'something'}
364
364
  key = val;
365
365
  }
366
366
  else {
367
- throw new Error("Invalid query string at " + key);
367
+ throw new Error("Invalid query string at ".concat(key));
368
368
  }
369
369
  }
370
370
  }
371
- if (_.isString(curr)) {
371
+ if ((0, lodash_1.isString)(curr)) {
372
372
  (_b = _match_1(curr), match = _b.match, val = _b.val);
373
373
  if (match) {
374
- _.isNumber(key)
374
+ (0, lodash_1.isNumber)(key)
375
375
  ? prev.push(val) // 3). as an item of array: ['${qry}', ...] => [${qry object}, ...]
376
376
  : (prev[key] = val); // 2). as a value: {prop: '${qry}'} => {prop: ${qry object}}
377
377
  return prev;
378
378
  }
379
379
  }
380
- if (_.isObject(curr) && !_.isRegExp(curr) && !_.isDate(curr)) {
380
+ if ((0, lodash_1.isObject)(curr) && !(0, lodash_1.isRegExp)(curr) && !(0, lodash_1.isDate)(curr)) {
381
381
  // iterate all props & keys recursively
382
- _.isNumber(key) ? prev.push(_transform_1(curr)) : (prev[key] = _transform_1(curr));
382
+ (0, lodash_1.isNumber)(key) ? prev.push(_transform_1(curr)) : (prev[key] = _transform_1(curr));
383
383
  }
384
384
  else {
385
- _.isNumber(key) ? prev.push(curr) : (prev[key] = curr);
385
+ (0, lodash_1.isNumber)(key) ? prev.push(curr) : (prev[key] = curr);
386
386
  }
387
387
  return prev;
388
- }, _.isArray(obj) ? [] : {});
388
+ }, (0, lodash_1.isArray)(obj) ? [] : {});
389
389
  };
390
390
  return _transform_1(query);
391
391
  }
package/lib/test.spec.js CHANGED
@@ -23,7 +23,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
23
23
  function verb(n) { return function (v) { return step([n, v]); }; }
24
24
  function step(op) {
25
25
  if (f) throw new TypeError("Generator is already executing.");
26
- while (_) try {
26
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
27
27
  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;
28
28
  if (y = 0, t) op = [op[0] & 2, t.value];
29
29
  switch (op[0]) {
@@ -112,7 +112,7 @@ var Tester = /** @class */ (function () {
112
112
  var obj = {
113
113
  $or: [{ key1: 'value1' }, { key2: 'value2' }],
114
114
  };
115
- var qry = "filter=" + JSON.stringify(obj) + "&name=Google";
115
+ var qry = "filter=".concat(JSON.stringify(obj), "&name=Google");
116
116
  var parsed = parser.parse(qry);
117
117
  chai_1.assert.isArray(parsed.filter['$or']);
118
118
  chai_1.assert.isOk(parsed.filter['name'] === 'Google');
@@ -1,7 +1,11 @@
1
1
  "use strict";
2
2
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
3
  if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
5
9
  }) : (function(o, m, k, k2) {
6
10
  if (k2 === undefined) k2 = k;
7
11
  o[k2] = m[k];
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@geekbears/gb-mongoose-query-parser",
3
- "version": "1.2.1",
3
+ "version": "1.3.0",
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",
7
7
  "scripts": {
8
8
  "clean": "rimraf lib",
9
- "compile": "npm run clean && tsc",
9
+ "compile": "npm run clean && NODE_OPTIONS='--max-old-space-size=8192' tsc -p tsconfig.json",
10
10
  "prepublishOnly": "npm run compile",
11
11
  "pretest": "npm run compile",
12
12
  "test": "mocha ./lib/**/*.spec.js",
@@ -32,25 +32,24 @@
32
32
  "homepage": "https://gitlab.com/geekbears/utilities/backend/gb-mongoose-query-parser#readme",
33
33
  "devDependencies": {
34
34
  "@testdeck/mocha": "^0.3.3",
35
- "@types/chai": "^4.3.4",
36
- "@types/lodash": "^4.14.194",
35
+ "@types/chai": "^4.3.5",
37
36
  "@types/mocha": "^10.0.1",
38
- "@types/node": "^18.16.2",
37
+ "@types/node": "^20.4.7",
38
+ "@types/qs": "^6.9.7",
39
39
  "chai": "^4.3.7",
40
40
  "mocha": "^10.2.0",
41
- "prettier": "^2.8.8",
42
- "rimraf": "^5.0.0",
41
+ "prettier": "^3.0.1",
42
+ "rimraf": "^5.0.1",
43
43
  "ts-node": "^10.9.1",
44
44
  "tslint": "^6.1.3",
45
- "typescript": "^5.0.4"
45
+ "typescript": "^5.1.6",
46
+ "@types/lodash": "^4.14.196"
46
47
  },
47
48
  "dependencies": {
48
49
  "lodash": "^4.17.21",
49
50
  "moment": "^2.29.4"
50
51
  },
51
52
  "peerDependencies": {
52
- "@nestjs/common": "^9.0.0",
53
- "@nestjs/swagger": "^6.0.0",
54
53
  "mongoose": "^7.0.0",
55
54
  "qs": "^6.10.1"
56
55
  }
@@ -1 +0,0 @@
1
- export declare const ApiSingleQuery: () => MethodDecorator;
@@ -1,25 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ApiSingleQuery = void 0;
4
- var common_1 = require("@nestjs/common");
5
- var swagger_1 = require("@nestjs/swagger");
6
- var ApiSingleQuery = function () {
7
- return (0, common_1.applyDecorators)((0, swagger_1.ApiQuery)({
8
- name: 'populate',
9
- description: 'A comma-separated list of virtual fields to populate',
10
- schema: { type: 'string' },
11
- required: false,
12
- }), (0, swagger_1.ApiQuery)({
13
- name: 'deepPopulate',
14
- description: 'A stringified populate JSON object',
15
- schema: { type: 'string' },
16
- required: false,
17
- }), (0, swagger_1.ApiQuery)({
18
- name: 'select',
19
- description: 'A comma-separated list of fields to select',
20
- schema: { type: 'string' },
21
- required: false,
22
- }));
23
- };
24
- exports.ApiSingleQuery = ApiSingleQuery;
25
- //# sourceMappingURL=api-single-query.decorator.js.map
@@ -1,6 +0,0 @@
1
- /**
2
- * Add geekbears standard query parameters
3
- * @description Swagger decorator. Adds all standard query parameters
4
- * @returns A method decorator wrapping calls to `@ApiQuery()` decorator
5
- */
6
- export declare const ApiStandardQuery: () => MethodDecorator;
@@ -1,50 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ApiStandardQuery = void 0;
4
- var common_1 = require("@nestjs/common");
5
- var swagger_1 = require("@nestjs/swagger");
6
- /**
7
- * Add geekbears standard query parameters
8
- * @description Swagger decorator. Adds all standard query parameters
9
- * @returns A method decorator wrapping calls to `@ApiQuery()` decorator
10
- */
11
- var ApiStandardQuery = function () {
12
- return (0, common_1.applyDecorators)((0, swagger_1.ApiQuery)({
13
- name: 'filter',
14
- description: 'A stringified filter JSON object',
15
- schema: { type: 'string' },
16
- required: false,
17
- }), (0, swagger_1.ApiQuery)({
18
- name: 'populate',
19
- description: 'A comma-separated list of virtual fields to populate',
20
- schema: { type: 'string' },
21
- required: false,
22
- }), (0, swagger_1.ApiQuery)({
23
- name: 'deepPopulate',
24
- description: 'A stringified populate JSON object',
25
- schema: { type: 'string' },
26
- required: false,
27
- }), (0, swagger_1.ApiQuery)({
28
- name: 'sort',
29
- description: 'The sorting configuration to apply to returned elements',
30
- schema: { type: 'string' },
31
- required: false,
32
- }), (0, swagger_1.ApiQuery)({
33
- name: 'limit',
34
- description: 'The max amount of records to return',
35
- schema: { type: 'string' },
36
- required: false,
37
- }), (0, swagger_1.ApiQuery)({
38
- name: 'select',
39
- description: 'A comma-separated list of fields to select',
40
- schema: { type: 'string' },
41
- required: false,
42
- }), (0, swagger_1.ApiQuery)({
43
- name: 'skip',
44
- description: 'The amount of records to skip before returning',
45
- schema: { type: 'string' },
46
- required: false,
47
- }));
48
- };
49
- exports.ApiStandardQuery = ApiStandardQuery;
50
- //# sourceMappingURL=api-standard-query.decorator.js.map
@@ -1,2 +0,0 @@
1
- export * from './api-standard-query.decorator';
2
- export * from './api-single-query.decorator';
@@ -1,15 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
- }) : (function(o, m, k, k2) {
6
- if (k2 === undefined) k2 = k;
7
- o[k2] = m[k];
8
- }));
9
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
- };
12
- Object.defineProperty(exports, "__esModule", { value: true });
13
- __exportStar(require("./api-standard-query.decorator"), exports);
14
- __exportStar(require("./api-single-query.decorator"), exports);
15
- //# sourceMappingURL=index.js.map