@geekbears/gb-mongoose-query-parser 1.3.0 → 1.3.2
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/lib/query-parser-helper.d.ts +45 -0
- package/lib/query-parser-helper.js +120 -3
- package/lib/query-parser.js +1 -1
- package/lib/test.spec.js +29 -0
- package/package.json +10 -10
|
@@ -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
|
|
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
|
-
|
|
186
|
-
throw new Error(
|
|
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/query-parser.js
CHANGED
|
@@ -6,8 +6,8 @@ var qs_1 = require("qs");
|
|
|
6
6
|
var lodash_1 = require("lodash");
|
|
7
7
|
var MongooseQueryParser = /** @class */ (function () {
|
|
8
8
|
function MongooseQueryParser(options) {
|
|
9
|
-
if (options === void 0) { options = {}; }
|
|
10
9
|
var _this = this;
|
|
10
|
+
if (options === void 0) { options = {}; }
|
|
11
11
|
this.options = options;
|
|
12
12
|
this.defaultDateFormat = [Moment.ISO_8601];
|
|
13
13
|
this.builtInCaster = {
|
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.3.
|
|
3
|
+
"version": "1.3.2",
|
|
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",
|
|
@@ -32,18 +32,18 @@
|
|
|
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.
|
|
36
|
-
"@types/mocha": "^10.0.
|
|
37
|
-
"@types/node": "^20.
|
|
38
|
-
"@types/qs": "^6.9.
|
|
39
|
-
"chai": "^4.3.
|
|
35
|
+
"@types/chai": "^4.3.10",
|
|
36
|
+
"@types/mocha": "^10.0.4",
|
|
37
|
+
"@types/node": "^20.9.0",
|
|
38
|
+
"@types/qs": "^6.9.10",
|
|
39
|
+
"chai": "^4.3.10",
|
|
40
40
|
"mocha": "^10.2.0",
|
|
41
|
-
"prettier": "^3.0.
|
|
42
|
-
"rimraf": "^5.0.
|
|
41
|
+
"prettier": "^3.0.3",
|
|
42
|
+
"rimraf": "^5.0.5",
|
|
43
43
|
"ts-node": "^10.9.1",
|
|
44
44
|
"tslint": "^6.1.3",
|
|
45
|
-
"typescript": "^5.
|
|
46
|
-
"@types/lodash": "^4.14.
|
|
45
|
+
"typescript": "^5.2.2",
|
|
46
|
+
"@types/lodash": "^4.14.201"
|
|
47
47
|
},
|
|
48
48
|
"dependencies": {
|
|
49
49
|
"lodash": "^4.17.21",
|