c2-mongoose 2.1.123 → 2.1.125
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/flow/CrudFlow.d.ts +1 -1
- package/dist/flow/SearchFlow.d.ts +2 -10
- package/dist/flow/SearchFlow.js +66 -91
- package/package.json +1 -1
- package/src/flow/CrudFlow.ts +1 -1
- package/src/flow/SearchFlow.ts +63 -68
package/dist/flow/CrudFlow.d.ts
CHANGED
|
@@ -12,7 +12,7 @@ declare class CrudFlow<D> {
|
|
|
12
12
|
update(id: string, data: D, session?: any): Promise<D>;
|
|
13
13
|
updateAny(id: string, data: any, session?: any): Promise<D>;
|
|
14
14
|
updateByModel(filter: any, data: any, params: any): Promise<D>;
|
|
15
|
-
find(options
|
|
15
|
+
find(options: SearchOptions): Promise<SearchResponse<D>>;
|
|
16
16
|
getOne(model: D, session?: ClientSession): Promise<D>;
|
|
17
17
|
get(id: string, pop?: string, sel?: string, session?: ClientSession): Promise<D>;
|
|
18
18
|
getById(id: string, session?: ClientSession): Promise<Partial<D>>;
|
|
@@ -23,18 +23,10 @@ declare abstract class SearchFlow {
|
|
|
23
23
|
buildPath(target: string, nested?: string): any;
|
|
24
24
|
buildOrdenation(): any;
|
|
25
25
|
buildRegex(searchText: string): RegExp;
|
|
26
|
+
searchNoPageable(model: mongoose.Model<any>, options: SearchOptions): Promise<any>;
|
|
26
27
|
searchPageable(model: mongoose.Model<any>, options: SearchOptions): Promise<any>;
|
|
27
28
|
transformObject(originalObject: any): any;
|
|
28
|
-
search(model: mongoose.Model<any>, options
|
|
29
|
-
/**
|
|
30
|
-
*
|
|
31
|
-
* @param model
|
|
32
|
-
* @deprecated
|
|
33
|
-
* @returns
|
|
34
|
-
*/
|
|
35
|
-
private searchPageableOld;
|
|
36
|
-
private searchNoPageable;
|
|
37
|
-
private result;
|
|
29
|
+
search(model: mongoose.Model<any>, options: SearchOptions): Promise<SearchResponse<any>>;
|
|
38
30
|
count(model: mongoose.Model<any>): Promise<number>;
|
|
39
31
|
findOne(repository: mongoose.Model<any>, model: any, session?: ClientSession): Promise<any>;
|
|
40
32
|
sumBy(model: mongoose.Model<any>, _sum: any, _by: any, items?: any, session?: ClientSession): Promise<any[]>;
|
package/dist/flow/SearchFlow.js
CHANGED
|
@@ -52,39 +52,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
52
52
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
53
53
|
var moment_1 = __importDefault(require("moment"));
|
|
54
54
|
var mongoose_1 = require("mongoose");
|
|
55
|
-
var Environment_1 = require("../configuration/Environment");
|
|
56
55
|
var Utils_1 = require("../utils/Utils");
|
|
57
56
|
var SearchFlow = /** @class */ (function () {
|
|
58
57
|
function SearchFlow(params) {
|
|
59
|
-
var _this = this;
|
|
60
|
-
/**
|
|
61
|
-
*
|
|
62
|
-
* @param model
|
|
63
|
-
* @deprecated
|
|
64
|
-
* @returns
|
|
65
|
-
*/
|
|
66
|
-
this.searchPageableOld = function (model, session) { return __awaiter(_this, void 0, void 0, function () {
|
|
67
|
-
var sort, items;
|
|
68
|
-
return __generator(this, function (_a) {
|
|
69
|
-
switch (_a.label) {
|
|
70
|
-
case 0:
|
|
71
|
-
sort = this.buildOrdenation();
|
|
72
|
-
return [4 /*yield*/, model
|
|
73
|
-
.find(this.filters, this.select)
|
|
74
|
-
.populate(this.populate)
|
|
75
|
-
.skip((this.page - 1) * this.limit)
|
|
76
|
-
.limit(this.limit)
|
|
77
|
-
.sort(sort)
|
|
78
|
-
.session(session)
|
|
79
|
-
.collation({
|
|
80
|
-
locale: Environment_1.dbCollation || "pt"
|
|
81
|
-
})];
|
|
82
|
-
case 1:
|
|
83
|
-
items = _a.sent();
|
|
84
|
-
return [2 /*return*/, this.result(model, items)];
|
|
85
|
-
}
|
|
86
|
-
});
|
|
87
|
-
}); };
|
|
88
58
|
this.searchText = params.searchText || "";
|
|
89
59
|
this.order = params.order || "asc";
|
|
90
60
|
this.orderBy = params.orderBy || "_id";
|
|
@@ -184,9 +154,67 @@ var SearchFlow = /** @class */ (function () {
|
|
|
184
154
|
.replace(/[u|ü|ú|ù|U|Ú|Ü|Ù]/g, '[u,ü,ú,ù,U,Ú,Ü,Ù]');
|
|
185
155
|
return new RegExp("".concat(regexExpression), 'i');
|
|
186
156
|
};
|
|
157
|
+
SearchFlow.prototype.searchNoPageable = function (model, options) {
|
|
158
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
159
|
+
var stagesItems, stagesMetadata, facet, _i, stagesMetadata_1, metadata, result, items, _a, _b, populate, resultAux;
|
|
160
|
+
return __generator(this, function (_c) {
|
|
161
|
+
switch (_c.label) {
|
|
162
|
+
case 0:
|
|
163
|
+
stagesItems = [{ $match: this.filters }];
|
|
164
|
+
if ((0, Utils_1.isNotEmpty)(options.unions)) {
|
|
165
|
+
stagesItems.push.apply(stagesItems, options.unions);
|
|
166
|
+
}
|
|
167
|
+
if ((0, Utils_1.isNotEmpty)(this.projection)) {
|
|
168
|
+
stagesItems.push({ $project: this.projection });
|
|
169
|
+
}
|
|
170
|
+
if ((0, Utils_1.isNotEmpty)(options.pipelines)) {
|
|
171
|
+
stagesItems.push.apply(stagesItems, options.pipelines);
|
|
172
|
+
}
|
|
173
|
+
stagesItems.push({ $sort: this.sort });
|
|
174
|
+
stagesItems.push({ $skip: ((this.page - 1) * this.limit) || 0 });
|
|
175
|
+
stagesItems.push({ $limit: this.limit });
|
|
176
|
+
stagesMetadata = [];
|
|
177
|
+
if ((0, Utils_1.isNotEmpty)(options.metadata)) {
|
|
178
|
+
stagesMetadata.push.apply(stagesMetadata, options.metadata);
|
|
179
|
+
}
|
|
180
|
+
facet = {
|
|
181
|
+
items: stagesItems
|
|
182
|
+
};
|
|
183
|
+
for (_i = 0, stagesMetadata_1 = stagesMetadata; _i < stagesMetadata_1.length; _i++) {
|
|
184
|
+
metadata = stagesMetadata_1[_i];
|
|
185
|
+
facet[metadata.id] = metadata.conditions;
|
|
186
|
+
}
|
|
187
|
+
return [4 /*yield*/, model.aggregate([
|
|
188
|
+
{
|
|
189
|
+
$facet: facet
|
|
190
|
+
},
|
|
191
|
+
]).session(options === null || options === void 0 ? void 0 : options.session)];
|
|
192
|
+
case 1:
|
|
193
|
+
result = _c.sent();
|
|
194
|
+
items = result[0].items;
|
|
195
|
+
if (!((0, Utils_1.isNotEmpty)(this.populate) && Array.isArray(this.populate))) return [3 /*break*/, 5];
|
|
196
|
+
_a = 0, _b = this.populate;
|
|
197
|
+
_c.label = 2;
|
|
198
|
+
case 2:
|
|
199
|
+
if (!(_a < _b.length)) return [3 /*break*/, 5];
|
|
200
|
+
populate = _b[_a];
|
|
201
|
+
return [4 /*yield*/, model.populate(result[0].items, populate)];
|
|
202
|
+
case 3:
|
|
203
|
+
items = _c.sent();
|
|
204
|
+
_c.label = 4;
|
|
205
|
+
case 4:
|
|
206
|
+
_a++;
|
|
207
|
+
return [3 /*break*/, 2];
|
|
208
|
+
case 5:
|
|
209
|
+
resultAux = this.transformObject(result[0]);
|
|
210
|
+
return [2 /*return*/, resultAux];
|
|
211
|
+
}
|
|
212
|
+
});
|
|
213
|
+
});
|
|
214
|
+
};
|
|
187
215
|
SearchFlow.prototype.searchPageable = function (model, options) {
|
|
188
216
|
return __awaiter(this, void 0, void 0, function () {
|
|
189
|
-
var stagesItems, stagesPaging, stagesMetadata, facet, _i,
|
|
217
|
+
var stagesItems, stagesPaging, stagesMetadata, facet, _i, stagesMetadata_2, metadata, result, items, _a, _b, populate, resultAux;
|
|
190
218
|
return __generator(this, function (_c) {
|
|
191
219
|
switch (_c.label) {
|
|
192
220
|
case 0:
|
|
@@ -217,8 +245,8 @@ var SearchFlow = /** @class */ (function () {
|
|
|
217
245
|
items: stagesItems,
|
|
218
246
|
paging: stagesPaging,
|
|
219
247
|
};
|
|
220
|
-
for (_i = 0,
|
|
221
|
-
metadata =
|
|
248
|
+
for (_i = 0, stagesMetadata_2 = stagesMetadata; _i < stagesMetadata_2.length; _i++) {
|
|
249
|
+
metadata = stagesMetadata_2[_i];
|
|
222
250
|
facet[metadata.id] = metadata.conditions;
|
|
223
251
|
}
|
|
224
252
|
return [4 /*yield*/, model.aggregate([
|
|
@@ -244,13 +272,7 @@ var SearchFlow = /** @class */ (function () {
|
|
|
244
272
|
return [3 /*break*/, 2];
|
|
245
273
|
case 5:
|
|
246
274
|
resultAux = this.transformObject(result[0]);
|
|
247
|
-
return [2 /*return*/, resultAux
|
|
248
|
-
// return {
|
|
249
|
-
// ...result[0],
|
|
250
|
-
// items,
|
|
251
|
-
// paging: result[0]?.paging[0] || { total: 0, page: 1, limit: this.limit },
|
|
252
|
-
// }
|
|
253
|
-
];
|
|
275
|
+
return [2 /*return*/, resultAux];
|
|
254
276
|
}
|
|
255
277
|
});
|
|
256
278
|
});
|
|
@@ -266,66 +288,19 @@ var SearchFlow = /** @class */ (function () {
|
|
|
266
288
|
transformedObject[key] = originalObject[key];
|
|
267
289
|
}
|
|
268
290
|
}
|
|
269
|
-
|
|
291
|
+
var hasPagination = (originalObject === null || originalObject === void 0 ? void 0 : originalObject.paging) ? true : false;
|
|
292
|
+
return __assign(__assign({}, transformedObject), { metadata: metadata, paging: hasPagination ? (originalObject === null || originalObject === void 0 ? void 0 : originalObject.paging[0]) || { total: 0, page: 1, limit: this.limit } : undefined });
|
|
270
293
|
};
|
|
271
294
|
SearchFlow.prototype.search = function (model, options) {
|
|
272
295
|
return __awaiter(this, void 0, void 0, function () {
|
|
273
296
|
return __generator(this, function (_a) {
|
|
274
297
|
switch (_a.label) {
|
|
275
298
|
case 0:
|
|
276
|
-
if (!(this.pageable == true)) return [3 /*break*/,
|
|
277
|
-
if (!(options === null || options === void 0 ? void 0 : options.pipelines)) return [3 /*break*/, 2];
|
|
299
|
+
if (!(this.pageable == true)) return [3 /*break*/, 2];
|
|
278
300
|
return [4 /*yield*/, this.searchPageable(model, options)];
|
|
279
301
|
case 1: return [2 /*return*/, _a.sent()];
|
|
280
|
-
case 2: return [4 /*yield*/, this.
|
|
302
|
+
case 2: return [4 /*yield*/, this.searchNoPageable(model, options)];
|
|
281
303
|
case 3: return [2 /*return*/, _a.sent()];
|
|
282
|
-
case 4: return [4 /*yield*/, this.searchNoPageable(model, options === null || options === void 0 ? void 0 : options.session)];
|
|
283
|
-
case 5: return [2 /*return*/, _a.sent()];
|
|
284
|
-
}
|
|
285
|
-
});
|
|
286
|
-
});
|
|
287
|
-
};
|
|
288
|
-
SearchFlow.prototype.searchNoPageable = function (model, session) {
|
|
289
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
290
|
-
var sort, items;
|
|
291
|
-
return __generator(this, function (_a) {
|
|
292
|
-
switch (_a.label) {
|
|
293
|
-
case 0:
|
|
294
|
-
sort = this.buildOrdenation();
|
|
295
|
-
return [4 /*yield*/, model
|
|
296
|
-
.find(this.filters, this.select)
|
|
297
|
-
.populate(this.populate)
|
|
298
|
-
.sort(sort)
|
|
299
|
-
.session(session)
|
|
300
|
-
.collation({
|
|
301
|
-
locale: Environment_1.dbCollation || "pt"
|
|
302
|
-
})];
|
|
303
|
-
case 1:
|
|
304
|
-
items = _a.sent();
|
|
305
|
-
return [2 /*return*/, this.result(model, items)];
|
|
306
|
-
}
|
|
307
|
-
});
|
|
308
|
-
});
|
|
309
|
-
};
|
|
310
|
-
SearchFlow.prototype.result = function (model, items) {
|
|
311
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
312
|
-
var searchResponse, total, paging;
|
|
313
|
-
return __generator(this, function (_a) {
|
|
314
|
-
switch (_a.label) {
|
|
315
|
-
case 0:
|
|
316
|
-
searchResponse = {};
|
|
317
|
-
searchResponse.items = items;
|
|
318
|
-
if (!(this.pageable == true)) return [3 /*break*/, 2];
|
|
319
|
-
return [4 /*yield*/, this.count(model)];
|
|
320
|
-
case 1:
|
|
321
|
-
total = _a.sent();
|
|
322
|
-
paging = {};
|
|
323
|
-
paging.total = total;
|
|
324
|
-
paging.page = Number(this.page);
|
|
325
|
-
paging.limit = Number(this.limit);
|
|
326
|
-
searchResponse.paging = paging;
|
|
327
|
-
_a.label = 2;
|
|
328
|
-
case 2: return [2 /*return*/, searchResponse];
|
|
329
304
|
}
|
|
330
305
|
});
|
|
331
306
|
});
|
package/package.json
CHANGED
package/src/flow/CrudFlow.ts
CHANGED
|
@@ -44,7 +44,7 @@ class CrudFlow<D> {
|
|
|
44
44
|
return dataAfter as D
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
-
public async find(options
|
|
47
|
+
public async find(options: SearchOptions): Promise<SearchResponse<D>> {
|
|
48
48
|
return await this.search.search(this.repository, options)
|
|
49
49
|
}
|
|
50
50
|
|
package/src/flow/SearchFlow.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import moment from "moment"
|
|
2
2
|
import mongoose, { ClientSession, Types } from "mongoose"
|
|
3
|
-
import {
|
|
4
|
-
import { Pagination, SearchOptions, SearchResponse } from "../types/SearchResponse"
|
|
3
|
+
import { SearchOptions, SearchResponse } from "../types/SearchResponse"
|
|
5
4
|
import { isEmpty, isNotEmpty } from "../utils/Utils"
|
|
6
5
|
|
|
7
6
|
abstract class SearchFlow {
|
|
@@ -122,6 +121,60 @@ abstract class SearchFlow {
|
|
|
122
121
|
return new RegExp(`${regexExpression}`, 'i');
|
|
123
122
|
}
|
|
124
123
|
|
|
124
|
+
async searchNoPageable(model: mongoose.Model<any>, options: SearchOptions) {
|
|
125
|
+
|
|
126
|
+
let stagesItems: any[] = [{ $match: this.filters }]
|
|
127
|
+
|
|
128
|
+
if (isNotEmpty(options.unions)) {
|
|
129
|
+
stagesItems.push(...options.unions)
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (isNotEmpty(this.projection)) {
|
|
133
|
+
stagesItems.push({ $project: this.projection })
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
if (isNotEmpty(options.pipelines)) {
|
|
137
|
+
stagesItems.push(...options.pipelines)
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
stagesItems.push({ $sort: this.sort })
|
|
141
|
+
stagesItems.push({ $skip: ((this.page - 1) * this.limit) || 0 })
|
|
142
|
+
stagesItems.push({ $limit: this.limit })
|
|
143
|
+
|
|
144
|
+
let stagesMetadata: any[] = []
|
|
145
|
+
if (isNotEmpty(options.metadata)) {
|
|
146
|
+
stagesMetadata.push(...options.metadata)
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
const facet: { [key: string]: any } = {
|
|
151
|
+
items: stagesItems
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
for (let metadata of stagesMetadata) {
|
|
155
|
+
facet[metadata.id] = metadata.conditions
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const result = await model.aggregate(
|
|
159
|
+
[
|
|
160
|
+
{
|
|
161
|
+
$facet: facet
|
|
162
|
+
},
|
|
163
|
+
]
|
|
164
|
+
).session(options?.session as ClientSession)
|
|
165
|
+
|
|
166
|
+
var items = result[0].items
|
|
167
|
+
|
|
168
|
+
if (isNotEmpty(this.populate) && Array.isArray(this.populate)) {
|
|
169
|
+
for (var populate of this.populate) {
|
|
170
|
+
items = await model.populate(result[0].items, populate)
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
let resultAux = this.transformObject(result[0])
|
|
175
|
+
return resultAux
|
|
176
|
+
}
|
|
177
|
+
|
|
125
178
|
async searchPageable(model: mongoose.Model<any>, options: SearchOptions) {
|
|
126
179
|
|
|
127
180
|
let stagesItems: any[] = [{ $match: this.filters }]
|
|
@@ -185,17 +238,12 @@ abstract class SearchFlow {
|
|
|
185
238
|
|
|
186
239
|
let resultAux = this.transformObject(result[0])
|
|
187
240
|
return resultAux
|
|
188
|
-
// return {
|
|
189
|
-
// ...result[0],
|
|
190
|
-
// items,
|
|
191
|
-
// paging: result[0]?.paging[0] || { total: 0, page: 1, limit: this.limit },
|
|
192
|
-
// }
|
|
193
241
|
}
|
|
194
242
|
|
|
195
243
|
transformObject(originalObject: any): any {
|
|
196
244
|
const metadata: any = {};
|
|
197
245
|
const transformedObject: any = {};
|
|
198
|
-
|
|
246
|
+
|
|
199
247
|
for (const key in originalObject) {
|
|
200
248
|
if (key.startsWith('metadata-')) {
|
|
201
249
|
metadata[key.replace("metadata-", "")] = originalObject[key][0];
|
|
@@ -203,75 +251,22 @@ abstract class SearchFlow {
|
|
|
203
251
|
transformedObject[key] = originalObject[key];
|
|
204
252
|
}
|
|
205
253
|
}
|
|
206
|
-
|
|
254
|
+
|
|
255
|
+
let hasPagination = originalObject?.paging ? true : false
|
|
256
|
+
|
|
207
257
|
return {
|
|
208
258
|
...transformedObject,
|
|
209
259
|
metadata,
|
|
210
|
-
paging: originalObject?.paging[0] || { total: 0, page: 1, limit: this.limit },
|
|
260
|
+
paging: hasPagination ? originalObject?.paging[0] || { total: 0, page: 1, limit: this.limit } : undefined,
|
|
211
261
|
};
|
|
212
262
|
}
|
|
213
263
|
|
|
214
|
-
public async search(model: mongoose.Model<any>, options
|
|
215
|
-
if (this.pageable == true) {
|
|
216
|
-
if (options?.pipelines) {
|
|
217
|
-
return await this.searchPageable(model, options)
|
|
218
|
-
}
|
|
219
|
-
return await this.searchPageableOld(model, options?.session)
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
return await this.searchNoPageable(model, options?.session)
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
/**
|
|
226
|
-
*
|
|
227
|
-
* @param model
|
|
228
|
-
* @deprecated
|
|
229
|
-
* @returns
|
|
230
|
-
*/
|
|
231
|
-
private searchPageableOld = async (model: mongoose.Model<any>, session?: ClientSession): Promise<SearchResponse<any>> => {
|
|
232
|
-
const sort = this.buildOrdenation()
|
|
233
|
-
var items = await model
|
|
234
|
-
.find(this.filters, this.select)
|
|
235
|
-
.populate(this.populate)
|
|
236
|
-
.skip((this.page - 1) * this.limit)
|
|
237
|
-
.limit(this.limit)
|
|
238
|
-
.sort(sort)
|
|
239
|
-
.session(session!)
|
|
240
|
-
.collation({
|
|
241
|
-
locale: dbCollation || "pt"
|
|
242
|
-
}) as []
|
|
243
|
-
|
|
244
|
-
return this.result(model, items)
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
private async searchNoPageable(model: mongoose.Model<any>, session?: ClientSession): Promise<SearchResponse<any>> {
|
|
248
|
-
const sort = this.buildOrdenation()
|
|
249
|
-
var items = await model
|
|
250
|
-
.find(this.filters, this.select)
|
|
251
|
-
.populate(this.populate)
|
|
252
|
-
.sort(sort)
|
|
253
|
-
.session(session!)
|
|
254
|
-
.collation({
|
|
255
|
-
locale: dbCollation || "pt"
|
|
256
|
-
}) as []
|
|
257
|
-
|
|
258
|
-
return this.result(model, items)
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
private async result(model: mongoose.Model<any>, items: []): Promise<SearchResponse<any>> {
|
|
262
|
-
var searchResponse: SearchResponse<any> = {}
|
|
263
|
-
searchResponse.items = items
|
|
264
|
-
|
|
264
|
+
public async search(model: mongoose.Model<any>, options: SearchOptions): Promise<SearchResponse<any>> {
|
|
265
265
|
if (this.pageable == true) {
|
|
266
|
-
|
|
267
|
-
var paging: Pagination = {}
|
|
268
|
-
paging.total = total
|
|
269
|
-
paging.page = Number(this.page)
|
|
270
|
-
paging.limit = Number(this.limit)
|
|
271
|
-
searchResponse.paging = paging
|
|
266
|
+
return await this.searchPageable(model, options)
|
|
272
267
|
}
|
|
273
268
|
|
|
274
|
-
return
|
|
269
|
+
return await this.searchNoPageable(model, options)
|
|
275
270
|
}
|
|
276
271
|
|
|
277
272
|
public async count(model: mongoose.Model<any>): Promise<number> {
|