c2-mongoose 2.1.14 → 2.1.18
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 +18 -18
- package/dist/configuration/Environment.d.ts +4 -4
- package/dist/configuration/Environment.js +5 -5
- package/dist/flow/CrudFlow.d.ts +19 -18
- package/dist/flow/CrudFlow.js +170 -149
- package/dist/flow/SearchFlow.d.ts +28 -28
- package/dist/flow/SearchFlow.js +308 -308
- package/dist/index.d.ts +4 -4
- package/dist/index.js +10 -10
- package/dist/utils/Utils.d.ts +6 -6
- package/dist/utils/Utils.js +56 -56
- package/package.json +1 -1
- package/src/configuration/Environment.ts +2 -2
- package/src/flow/CrudFlow.ts +66 -54
- package/src/flow/SearchFlow.ts +237 -237
- package/src/index.ts +5 -5
- package/src/types/SearchResponse.d.ts +9 -9
- package/src/utils/Utils.ts +56 -56
- package/tsconfig.json +12 -12
- package/dist/model/Search.d.ts +0 -26
- package/dist/model/Search.js +0 -265
package/dist/flow/SearchFlow.js
CHANGED
|
@@ -1,308 +1,308 @@
|
|
|
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
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
50
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
51
|
-
};
|
|
52
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
53
|
-
var moment_1 = __importDefault(require("moment"));
|
|
54
|
-
var Environment_1 = require("../configuration/Environment");
|
|
55
|
-
var Utils_1 = require("../utils/Utils");
|
|
56
|
-
var SearchFlow = /** @class */ (function () {
|
|
57
|
-
function SearchFlow(params) {
|
|
58
|
-
var _this = this;
|
|
59
|
-
this.isPageable = function () {
|
|
60
|
-
if ((0, Utils_1.isEmpty)(_this.page)) {
|
|
61
|
-
_this.page = 1;
|
|
62
|
-
}
|
|
63
|
-
if (_this.page >= 0) {
|
|
64
|
-
return true;
|
|
65
|
-
}
|
|
66
|
-
return false;
|
|
67
|
-
};
|
|
68
|
-
this.searchPageable = function (model) { return __awaiter(_this, void 0, void 0, function () {
|
|
69
|
-
var sort, items;
|
|
70
|
-
return __generator(this, function (_a) {
|
|
71
|
-
switch (_a.label) {
|
|
72
|
-
case 0:
|
|
73
|
-
sort = this.buildOrdenation();
|
|
74
|
-
return [4 /*yield*/, model
|
|
75
|
-
.find(this.filters, this.select)
|
|
76
|
-
.populate(this.populate)
|
|
77
|
-
.skip((this.page - 1) * this.limit)
|
|
78
|
-
.limit(this.limit)
|
|
79
|
-
.sort(sort)
|
|
80
|
-
.collation({
|
|
81
|
-
locale: Environment_1.dbCollation || "pt"
|
|
82
|
-
})];
|
|
83
|
-
case 1:
|
|
84
|
-
items = _a.sent();
|
|
85
|
-
return [2 /*return*/, this.result(model, items)];
|
|
86
|
-
}
|
|
87
|
-
});
|
|
88
|
-
}); };
|
|
89
|
-
this.searchText = params.searchText || "";
|
|
90
|
-
this.order = params.order || "asc";
|
|
91
|
-
this.orderBy = params.orderBy || "_id";
|
|
92
|
-
this.select = params.select;
|
|
93
|
-
this.populate = params.populate;
|
|
94
|
-
this.page = params.page || undefined;
|
|
95
|
-
this.limit = params.limit || 25;
|
|
96
|
-
this.buildPopulate();
|
|
97
|
-
}
|
|
98
|
-
SearchFlow.prototype.buildPopulate = function () {
|
|
99
|
-
if ((0, Utils_1.isEmpty)(this.populate)) {
|
|
100
|
-
return;
|
|
101
|
-
}
|
|
102
|
-
var propertiesArray = this.populate.toString().split(',');
|
|
103
|
-
var populates = [];
|
|
104
|
-
for (var _i = 0, propertiesArray_1 = propertiesArray; _i < propertiesArray_1.length; _i++) {
|
|
105
|
-
var property = propertiesArray_1[_i];
|
|
106
|
-
var _a = property.split('.'), first = _a[0], rest = _a.slice(1);
|
|
107
|
-
var nested = rest.join('.');
|
|
108
|
-
populates.push(this.buildPath(first, nested));
|
|
109
|
-
}
|
|
110
|
-
this.populate = populates;
|
|
111
|
-
};
|
|
112
|
-
SearchFlow.prototype.buildPath = function (target, nested) {
|
|
113
|
-
if (nested === void 0) { nested = ""; }
|
|
114
|
-
var populate = {};
|
|
115
|
-
populate.path = target;
|
|
116
|
-
if ((0, Utils_1.isNotEmpty)(nested)) {
|
|
117
|
-
var _a = nested.split('.'), first = _a[0], rest = _a.slice(1);
|
|
118
|
-
var nested2 = rest.join('.');
|
|
119
|
-
populate.populate = this.buildPath(first, nested2);
|
|
120
|
-
}
|
|
121
|
-
return populate;
|
|
122
|
-
};
|
|
123
|
-
SearchFlow.prototype.buildOrdenation = function () {
|
|
124
|
-
var order = {};
|
|
125
|
-
this.orderBy = this.orderBy || "_id";
|
|
126
|
-
order[this.orderBy] = this.order === "desc" ? -1 : 1;
|
|
127
|
-
return order;
|
|
128
|
-
};
|
|
129
|
-
SearchFlow.prototype.diacriticSensitiveRegex = function (string) {
|
|
130
|
-
if (string === void 0) { string = ""; }
|
|
131
|
-
return string
|
|
132
|
-
.replace(/[a|á|à|ä|â|A|Á|Â|Ã|Ä]/g, '[a,á,à,ä,â,A,Á,Â,Ã,Ä]')
|
|
133
|
-
.replace(/[e|é|ë|è|E|É|Ë|È]/g, '[e,é,ë,è,E,É,Ë,È]')
|
|
134
|
-
.replace(/[i|í|ï|ì|I|Í|Ï|Ì]/g, '[i,í,ï,ì,I,Í,Ï,Ì]')
|
|
135
|
-
.replace(/[o|ó|ö|ò|õ|O|Ó|Ö|Ô|Õ]/g, '[o,ó,ö,ò,õ,O,Ó,Ö,Ô,Õ]')
|
|
136
|
-
.replace(/[u|ü|ú|ù|U|Ú|Ü|Ù]/g, '[u,ü,ú,ù,U,Ú,Ü,Ù]')
|
|
137
|
-
.replace(/[ç|Ç|c|C]/g, '[c,C,ç,Ç]');
|
|
138
|
-
};
|
|
139
|
-
SearchFlow.prototype.search = function (model) {
|
|
140
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
141
|
-
return __generator(this, function (_a) {
|
|
142
|
-
switch (_a.label) {
|
|
143
|
-
case 0:
|
|
144
|
-
if (!this.isPageable()) return [3 /*break*/, 2];
|
|
145
|
-
return [4 /*yield*/, this.searchPageable(model)];
|
|
146
|
-
case 1: return [2 /*return*/, _a.sent()];
|
|
147
|
-
case 2: return [4 /*yield*/, this.searchNoPageable(model)];
|
|
148
|
-
case 3: return [2 /*return*/, _a.sent()];
|
|
149
|
-
}
|
|
150
|
-
});
|
|
151
|
-
});
|
|
152
|
-
};
|
|
153
|
-
SearchFlow.prototype.searchNoPageable = function (model) {
|
|
154
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
155
|
-
var sort, items;
|
|
156
|
-
return __generator(this, function (_a) {
|
|
157
|
-
switch (_a.label) {
|
|
158
|
-
case 0:
|
|
159
|
-
sort = this.buildOrdenation();
|
|
160
|
-
return [4 /*yield*/, model
|
|
161
|
-
.find(this.filters, this.select)
|
|
162
|
-
.populate(this.populate)
|
|
163
|
-
.sort(sort)
|
|
164
|
-
.collation({
|
|
165
|
-
locale: Environment_1.dbCollation || "pt"
|
|
166
|
-
})];
|
|
167
|
-
case 1:
|
|
168
|
-
items = _a.sent();
|
|
169
|
-
return [2 /*return*/, this.result(model, items)];
|
|
170
|
-
}
|
|
171
|
-
});
|
|
172
|
-
});
|
|
173
|
-
};
|
|
174
|
-
SearchFlow.prototype.result = function (model, items) {
|
|
175
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
176
|
-
var total, paging, searchResponse;
|
|
177
|
-
return __generator(this, function (_a) {
|
|
178
|
-
switch (_a.label) {
|
|
179
|
-
case 0: return [4 /*yield*/, this.count(model)];
|
|
180
|
-
case 1:
|
|
181
|
-
total = _a.sent();
|
|
182
|
-
paging = {};
|
|
183
|
-
paging.total = total;
|
|
184
|
-
paging.page = this.page;
|
|
185
|
-
paging.limit = this.limit;
|
|
186
|
-
searchResponse = {};
|
|
187
|
-
searchResponse.items = items;
|
|
188
|
-
searchResponse.paging = paging;
|
|
189
|
-
return [2 /*return*/, searchResponse];
|
|
190
|
-
}
|
|
191
|
-
});
|
|
192
|
-
});
|
|
193
|
-
};
|
|
194
|
-
SearchFlow.prototype.count = function (model) {
|
|
195
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
196
|
-
return __generator(this, function (_a) {
|
|
197
|
-
switch (_a.label) {
|
|
198
|
-
case 0: return [4 /*yield*/, model.countDocuments(this.filters).exec()];
|
|
199
|
-
case 1: return [2 /*return*/, _a.sent()];
|
|
200
|
-
}
|
|
201
|
-
});
|
|
202
|
-
});
|
|
203
|
-
};
|
|
204
|
-
SearchFlow.prototype.findOne = function (repository, model, params) {
|
|
205
|
-
if (params === void 0) { params = {}; }
|
|
206
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
207
|
-
return __generator(this, function (_a) {
|
|
208
|
-
switch (_a.label) {
|
|
209
|
-
case 0: return [4 /*yield*/, repository.findOne(model)
|
|
210
|
-
.sort(params.sort)
|
|
211
|
-
.select(params.select)
|
|
212
|
-
.populate(params.populate)];
|
|
213
|
-
case 1: return [2 /*return*/, _a.sent()];
|
|
214
|
-
}
|
|
215
|
-
});
|
|
216
|
-
});
|
|
217
|
-
};
|
|
218
|
-
SearchFlow.prototype.sumBy = function (model, _sum, _by) {
|
|
219
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
220
|
-
var ret;
|
|
221
|
-
return __generator(this, function (_a) {
|
|
222
|
-
switch (_a.label) {
|
|
223
|
-
case 0: return [4 /*yield*/, model.aggregate([
|
|
224
|
-
{
|
|
225
|
-
'$match': this.filters
|
|
226
|
-
},
|
|
227
|
-
{
|
|
228
|
-
$group: {
|
|
229
|
-
_id: _by,
|
|
230
|
-
totalValue: { "$sum": _sum },
|
|
231
|
-
count: { "$sum": 1 }
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
])];
|
|
235
|
-
case 1:
|
|
236
|
-
ret = _a.sent();
|
|
237
|
-
return [2 /*return*/, ret];
|
|
238
|
-
}
|
|
239
|
-
});
|
|
240
|
-
});
|
|
241
|
-
};
|
|
242
|
-
SearchFlow.prototype.buildDefaultFilters = function (objectSearch) {
|
|
243
|
-
var filters = { $and: [] };
|
|
244
|
-
Object.entries(objectSearch.model).forEach(function (_a) {
|
|
245
|
-
var key = _a[0], value = _a[1];
|
|
246
|
-
if ((0, Utils_1.isNotEmpty)(value)) {
|
|
247
|
-
var condition = {};
|
|
248
|
-
if (['order', 'orderBy', 'properties', 'populate', 'page', 'limit', 'model', 'select', 'searchText', 'isPageable', 'searchPageable'].includes(key)) {
|
|
249
|
-
return;
|
|
250
|
-
}
|
|
251
|
-
if (key.endsWith('DateRange') || key.endsWith('DateTimeRange')) {
|
|
252
|
-
var fieldName = key.replace('Range', '');
|
|
253
|
-
var values = value;
|
|
254
|
-
if ((0, Utils_1.isNotEmpty)(values[0])) {
|
|
255
|
-
var momentValue = (0, moment_1.default)(values[0]);
|
|
256
|
-
condition[fieldName] = __assign(__assign({}, condition[fieldName]), { $gte: momentValue.toDate() });
|
|
257
|
-
}
|
|
258
|
-
if ((0, Utils_1.isNotEmpty)(values[1])) {
|
|
259
|
-
var momentValue = (0, moment_1.default)(values[1]);
|
|
260
|
-
condition[fieldName] = __assign(__assign({}, condition[fieldName]), { $lte: momentValue.toDate().setHours(23, 59, 59) });
|
|
261
|
-
}
|
|
262
|
-
filters.$and.push(condition);
|
|
263
|
-
}
|
|
264
|
-
else if (key.endsWith('Like')) {
|
|
265
|
-
var fieldName = key.replace('Like', '');
|
|
266
|
-
condition[fieldName] = { $regex: value, $options: 'i' };
|
|
267
|
-
filters.$and.push(condition);
|
|
268
|
-
}
|
|
269
|
-
else {
|
|
270
|
-
if (!Array.isArray(value)) {
|
|
271
|
-
condition[key] = value;
|
|
272
|
-
}
|
|
273
|
-
else {
|
|
274
|
-
condition[key] = { $in: value };
|
|
275
|
-
}
|
|
276
|
-
filters.$and.push(condition);
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
});
|
|
280
|
-
// if (isNotEmpty(objectSearch.model)) {
|
|
281
|
-
// this.addFilterModel(objectSearch.model, filters)
|
|
282
|
-
// }
|
|
283
|
-
if (filters.$and.length === 0)
|
|
284
|
-
delete filters['$and'];
|
|
285
|
-
return filters;
|
|
286
|
-
};
|
|
287
|
-
SearchFlow.prototype.addFilterModel = function (model, filters) {
|
|
288
|
-
Object.entries(model).forEach(function (_a) {
|
|
289
|
-
var key = _a[0], value = _a[1];
|
|
290
|
-
if (key.endsWith('DateRange')) {
|
|
291
|
-
return;
|
|
292
|
-
}
|
|
293
|
-
if (key.endsWith('Like')) {
|
|
294
|
-
return;
|
|
295
|
-
}
|
|
296
|
-
if (['order', 'orderBy', 'properties', 'populate', 'page', 'limit', 'model', 'select', 'searchText', 'isPageable', 'searchPageable'].includes(key)) {
|
|
297
|
-
return;
|
|
298
|
-
}
|
|
299
|
-
if ((0, Utils_1.isNotEmpty)(value)) {
|
|
300
|
-
var condition = {};
|
|
301
|
-
condition[key] = value;
|
|
302
|
-
filters.$and.push(condition);
|
|
303
|
-
}
|
|
304
|
-
});
|
|
305
|
-
};
|
|
306
|
-
return SearchFlow;
|
|
307
|
-
}());
|
|
308
|
-
exports.default = SearchFlow;
|
|
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
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
50
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
51
|
+
};
|
|
52
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
53
|
+
var moment_1 = __importDefault(require("moment"));
|
|
54
|
+
var Environment_1 = require("../configuration/Environment");
|
|
55
|
+
var Utils_1 = require("../utils/Utils");
|
|
56
|
+
var SearchFlow = /** @class */ (function () {
|
|
57
|
+
function SearchFlow(params) {
|
|
58
|
+
var _this = this;
|
|
59
|
+
this.isPageable = function () {
|
|
60
|
+
if ((0, Utils_1.isEmpty)(_this.page)) {
|
|
61
|
+
_this.page = 1;
|
|
62
|
+
}
|
|
63
|
+
if (_this.page >= 0) {
|
|
64
|
+
return true;
|
|
65
|
+
}
|
|
66
|
+
return false;
|
|
67
|
+
};
|
|
68
|
+
this.searchPageable = function (model) { return __awaiter(_this, void 0, void 0, function () {
|
|
69
|
+
var sort, items;
|
|
70
|
+
return __generator(this, function (_a) {
|
|
71
|
+
switch (_a.label) {
|
|
72
|
+
case 0:
|
|
73
|
+
sort = this.buildOrdenation();
|
|
74
|
+
return [4 /*yield*/, model
|
|
75
|
+
.find(this.filters, this.select)
|
|
76
|
+
.populate(this.populate)
|
|
77
|
+
.skip((this.page - 1) * this.limit)
|
|
78
|
+
.limit(this.limit)
|
|
79
|
+
.sort(sort)
|
|
80
|
+
.collation({
|
|
81
|
+
locale: Environment_1.dbCollation || "pt"
|
|
82
|
+
})];
|
|
83
|
+
case 1:
|
|
84
|
+
items = _a.sent();
|
|
85
|
+
return [2 /*return*/, this.result(model, items)];
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
}); };
|
|
89
|
+
this.searchText = params.searchText || "";
|
|
90
|
+
this.order = params.order || "asc";
|
|
91
|
+
this.orderBy = params.orderBy || "_id";
|
|
92
|
+
this.select = params.select;
|
|
93
|
+
this.populate = params.populate;
|
|
94
|
+
this.page = params.page || undefined;
|
|
95
|
+
this.limit = params.limit || 25;
|
|
96
|
+
this.buildPopulate();
|
|
97
|
+
}
|
|
98
|
+
SearchFlow.prototype.buildPopulate = function () {
|
|
99
|
+
if ((0, Utils_1.isEmpty)(this.populate)) {
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
var propertiesArray = this.populate.toString().split(',');
|
|
103
|
+
var populates = [];
|
|
104
|
+
for (var _i = 0, propertiesArray_1 = propertiesArray; _i < propertiesArray_1.length; _i++) {
|
|
105
|
+
var property = propertiesArray_1[_i];
|
|
106
|
+
var _a = property.split('.'), first = _a[0], rest = _a.slice(1);
|
|
107
|
+
var nested = rest.join('.');
|
|
108
|
+
populates.push(this.buildPath(first, nested));
|
|
109
|
+
}
|
|
110
|
+
this.populate = populates;
|
|
111
|
+
};
|
|
112
|
+
SearchFlow.prototype.buildPath = function (target, nested) {
|
|
113
|
+
if (nested === void 0) { nested = ""; }
|
|
114
|
+
var populate = {};
|
|
115
|
+
populate.path = target;
|
|
116
|
+
if ((0, Utils_1.isNotEmpty)(nested)) {
|
|
117
|
+
var _a = nested.split('.'), first = _a[0], rest = _a.slice(1);
|
|
118
|
+
var nested2 = rest.join('.');
|
|
119
|
+
populate.populate = this.buildPath(first, nested2);
|
|
120
|
+
}
|
|
121
|
+
return populate;
|
|
122
|
+
};
|
|
123
|
+
SearchFlow.prototype.buildOrdenation = function () {
|
|
124
|
+
var order = {};
|
|
125
|
+
this.orderBy = this.orderBy || "_id";
|
|
126
|
+
order[this.orderBy] = this.order === "desc" ? -1 : 1;
|
|
127
|
+
return order;
|
|
128
|
+
};
|
|
129
|
+
SearchFlow.prototype.diacriticSensitiveRegex = function (string) {
|
|
130
|
+
if (string === void 0) { string = ""; }
|
|
131
|
+
return string
|
|
132
|
+
.replace(/[a|á|à|ä|â|A|Á|Â|Ã|Ä]/g, '[a,á,à,ä,â,A,Á,Â,Ã,Ä]')
|
|
133
|
+
.replace(/[e|é|ë|è|E|É|Ë|È]/g, '[e,é,ë,è,E,É,Ë,È]')
|
|
134
|
+
.replace(/[i|í|ï|ì|I|Í|Ï|Ì]/g, '[i,í,ï,ì,I,Í,Ï,Ì]')
|
|
135
|
+
.replace(/[o|ó|ö|ò|õ|O|Ó|Ö|Ô|Õ]/g, '[o,ó,ö,ò,õ,O,Ó,Ö,Ô,Õ]')
|
|
136
|
+
.replace(/[u|ü|ú|ù|U|Ú|Ü|Ù]/g, '[u,ü,ú,ù,U,Ú,Ü,Ù]')
|
|
137
|
+
.replace(/[ç|Ç|c|C]/g, '[c,C,ç,Ç]');
|
|
138
|
+
};
|
|
139
|
+
SearchFlow.prototype.search = function (model) {
|
|
140
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
141
|
+
return __generator(this, function (_a) {
|
|
142
|
+
switch (_a.label) {
|
|
143
|
+
case 0:
|
|
144
|
+
if (!this.isPageable()) return [3 /*break*/, 2];
|
|
145
|
+
return [4 /*yield*/, this.searchPageable(model)];
|
|
146
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
147
|
+
case 2: return [4 /*yield*/, this.searchNoPageable(model)];
|
|
148
|
+
case 3: return [2 /*return*/, _a.sent()];
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
};
|
|
153
|
+
SearchFlow.prototype.searchNoPageable = function (model) {
|
|
154
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
155
|
+
var sort, items;
|
|
156
|
+
return __generator(this, function (_a) {
|
|
157
|
+
switch (_a.label) {
|
|
158
|
+
case 0:
|
|
159
|
+
sort = this.buildOrdenation();
|
|
160
|
+
return [4 /*yield*/, model
|
|
161
|
+
.find(this.filters, this.select)
|
|
162
|
+
.populate(this.populate)
|
|
163
|
+
.sort(sort)
|
|
164
|
+
.collation({
|
|
165
|
+
locale: Environment_1.dbCollation || "pt"
|
|
166
|
+
})];
|
|
167
|
+
case 1:
|
|
168
|
+
items = _a.sent();
|
|
169
|
+
return [2 /*return*/, this.result(model, items)];
|
|
170
|
+
}
|
|
171
|
+
});
|
|
172
|
+
});
|
|
173
|
+
};
|
|
174
|
+
SearchFlow.prototype.result = function (model, items) {
|
|
175
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
176
|
+
var total, paging, searchResponse;
|
|
177
|
+
return __generator(this, function (_a) {
|
|
178
|
+
switch (_a.label) {
|
|
179
|
+
case 0: return [4 /*yield*/, this.count(model)];
|
|
180
|
+
case 1:
|
|
181
|
+
total = _a.sent();
|
|
182
|
+
paging = {};
|
|
183
|
+
paging.total = total;
|
|
184
|
+
paging.page = this.page;
|
|
185
|
+
paging.limit = this.limit;
|
|
186
|
+
searchResponse = {};
|
|
187
|
+
searchResponse.items = items;
|
|
188
|
+
searchResponse.paging = paging;
|
|
189
|
+
return [2 /*return*/, searchResponse];
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
});
|
|
193
|
+
};
|
|
194
|
+
SearchFlow.prototype.count = function (model) {
|
|
195
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
196
|
+
return __generator(this, function (_a) {
|
|
197
|
+
switch (_a.label) {
|
|
198
|
+
case 0: return [4 /*yield*/, model.countDocuments(this.filters).exec()];
|
|
199
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
200
|
+
}
|
|
201
|
+
});
|
|
202
|
+
});
|
|
203
|
+
};
|
|
204
|
+
SearchFlow.prototype.findOne = function (repository, model, params) {
|
|
205
|
+
if (params === void 0) { params = {}; }
|
|
206
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
207
|
+
return __generator(this, function (_a) {
|
|
208
|
+
switch (_a.label) {
|
|
209
|
+
case 0: return [4 /*yield*/, repository.findOne(model)
|
|
210
|
+
.sort(params.sort)
|
|
211
|
+
.select(params.select)
|
|
212
|
+
.populate(params.populate)];
|
|
213
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
214
|
+
}
|
|
215
|
+
});
|
|
216
|
+
});
|
|
217
|
+
};
|
|
218
|
+
SearchFlow.prototype.sumBy = function (model, _sum, _by) {
|
|
219
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
220
|
+
var ret;
|
|
221
|
+
return __generator(this, function (_a) {
|
|
222
|
+
switch (_a.label) {
|
|
223
|
+
case 0: return [4 /*yield*/, model.aggregate([
|
|
224
|
+
{
|
|
225
|
+
'$match': this.filters
|
|
226
|
+
},
|
|
227
|
+
{
|
|
228
|
+
$group: {
|
|
229
|
+
_id: _by,
|
|
230
|
+
totalValue: { "$sum": _sum },
|
|
231
|
+
count: { "$sum": 1 }
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
])];
|
|
235
|
+
case 1:
|
|
236
|
+
ret = _a.sent();
|
|
237
|
+
return [2 /*return*/, ret];
|
|
238
|
+
}
|
|
239
|
+
});
|
|
240
|
+
});
|
|
241
|
+
};
|
|
242
|
+
SearchFlow.prototype.buildDefaultFilters = function (objectSearch) {
|
|
243
|
+
var filters = { $and: [] };
|
|
244
|
+
Object.entries(objectSearch.model).forEach(function (_a) {
|
|
245
|
+
var key = _a[0], value = _a[1];
|
|
246
|
+
if ((0, Utils_1.isNotEmpty)(value)) {
|
|
247
|
+
var condition = {};
|
|
248
|
+
if (['order', 'orderBy', 'properties', 'populate', 'page', 'limit', 'model', 'select', 'searchText', 'isPageable', 'searchPageable'].includes(key)) {
|
|
249
|
+
return;
|
|
250
|
+
}
|
|
251
|
+
if (key.endsWith('DateRange') || key.endsWith('DateTimeRange')) {
|
|
252
|
+
var fieldName = key.replace('Range', '');
|
|
253
|
+
var values = value;
|
|
254
|
+
if ((0, Utils_1.isNotEmpty)(values[0])) {
|
|
255
|
+
var momentValue = (0, moment_1.default)(values[0]);
|
|
256
|
+
condition[fieldName] = __assign(__assign({}, condition[fieldName]), { $gte: momentValue.toDate().setHours(0, 0, 0) });
|
|
257
|
+
}
|
|
258
|
+
if ((0, Utils_1.isNotEmpty)(values[1])) {
|
|
259
|
+
var momentValue = (0, moment_1.default)(values[1]);
|
|
260
|
+
condition[fieldName] = __assign(__assign({}, condition[fieldName]), { $lte: momentValue.toDate().setHours(23, 59, 59) });
|
|
261
|
+
}
|
|
262
|
+
filters.$and.push(condition);
|
|
263
|
+
}
|
|
264
|
+
else if (key.endsWith('Like')) {
|
|
265
|
+
var fieldName = key.replace('Like', '');
|
|
266
|
+
condition[fieldName] = { $regex: value, $options: 'i' };
|
|
267
|
+
filters.$and.push(condition);
|
|
268
|
+
}
|
|
269
|
+
else {
|
|
270
|
+
if (!Array.isArray(value)) {
|
|
271
|
+
condition[key] = value;
|
|
272
|
+
}
|
|
273
|
+
else {
|
|
274
|
+
condition[key] = { $in: value };
|
|
275
|
+
}
|
|
276
|
+
filters.$and.push(condition);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
});
|
|
280
|
+
// if (isNotEmpty(objectSearch.model)) {
|
|
281
|
+
// this.addFilterModel(objectSearch.model, filters)
|
|
282
|
+
// }
|
|
283
|
+
if (filters.$and.length === 0)
|
|
284
|
+
delete filters['$and'];
|
|
285
|
+
return filters;
|
|
286
|
+
};
|
|
287
|
+
SearchFlow.prototype.addFilterModel = function (model, filters) {
|
|
288
|
+
Object.entries(model).forEach(function (_a) {
|
|
289
|
+
var key = _a[0], value = _a[1];
|
|
290
|
+
if (key.endsWith('DateRange')) {
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
if (key.endsWith('Like')) {
|
|
294
|
+
return;
|
|
295
|
+
}
|
|
296
|
+
if (['order', 'orderBy', 'properties', 'populate', 'page', 'limit', 'model', 'select', 'searchText', 'isPageable', 'searchPageable'].includes(key)) {
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
299
|
+
if ((0, Utils_1.isNotEmpty)(value)) {
|
|
300
|
+
var condition = {};
|
|
301
|
+
condition[key] = value;
|
|
302
|
+
filters.$and.push(condition);
|
|
303
|
+
}
|
|
304
|
+
});
|
|
305
|
+
};
|
|
306
|
+
return SearchFlow;
|
|
307
|
+
}());
|
|
308
|
+
exports.default = SearchFlow;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import CrudFlow from "./flow/CrudFlow";
|
|
2
|
-
import SearchFlow from "./flow/SearchFlow";
|
|
3
|
-
import { Pagination, SearchResponse } from "./types/SearchResponse";
|
|
4
|
-
export { CrudFlow, SearchFlow, SearchResponse, Pagination };
|
|
1
|
+
import CrudFlow from "./flow/CrudFlow";
|
|
2
|
+
import SearchFlow from "./flow/SearchFlow";
|
|
3
|
+
import { Pagination, SearchResponse } from "./types/SearchResponse";
|
|
4
|
+
export { CrudFlow, SearchFlow, SearchResponse, Pagination };
|
package/dist/index.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.SearchFlow = exports.CrudFlow = void 0;
|
|
7
|
-
var CrudFlow_1 = __importDefault(require("./flow/CrudFlow"));
|
|
8
|
-
exports.CrudFlow = CrudFlow_1.default;
|
|
9
|
-
var SearchFlow_1 = __importDefault(require("./flow/SearchFlow"));
|
|
10
|
-
exports.SearchFlow = SearchFlow_1.default;
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.SearchFlow = exports.CrudFlow = void 0;
|
|
7
|
+
var CrudFlow_1 = __importDefault(require("./flow/CrudFlow"));
|
|
8
|
+
exports.CrudFlow = CrudFlow_1.default;
|
|
9
|
+
var SearchFlow_1 = __importDefault(require("./flow/SearchFlow"));
|
|
10
|
+
exports.SearchFlow = SearchFlow_1.default;
|