c2-mongoose 2.1.13 → 2.1.17

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