c2-mongoose 2.0.5 → 2.0.6
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 +6 -6
- package/dist/flow/CrudFlow.js +69 -67
- package/dist/flow/SearchFlow.d.ts +9 -9
- package/dist/flow/SearchFlow.js +134 -124
- package/package.json +1 -1
- package/src/flow/CrudFlow.ts +6 -6
- package/src/flow/SearchFlow.ts +11 -11
package/dist/flow/CrudFlow.d.ts
CHANGED
|
@@ -3,11 +3,11 @@ import SearchFlow from "./SearchFlow";
|
|
|
3
3
|
declare class CrudFlow<D> extends SearchFlow {
|
|
4
4
|
private repository;
|
|
5
5
|
constructor(repository: mongoose.Model<any>, model?: any);
|
|
6
|
-
create
|
|
7
|
-
delete
|
|
8
|
-
update
|
|
9
|
-
find
|
|
10
|
-
getOne
|
|
11
|
-
get
|
|
6
|
+
create(data: any, session?: any): Promise<any[]>;
|
|
7
|
+
delete(id: string, session?: any): Promise<void>;
|
|
8
|
+
update(id: string, data: D, session?: any): Promise<D>;
|
|
9
|
+
find(search: SearchFlow): Promise<SearchResponse<D>>;
|
|
10
|
+
getOne(model: D, params?: any): Promise<D>;
|
|
11
|
+
get(id: string, pop?: string, sel?: string): Promise<D>;
|
|
12
12
|
}
|
|
13
13
|
export default CrudFlow;
|
package/dist/flow/CrudFlow.js
CHANGED
|
@@ -60,84 +60,86 @@ var CrudFlow = /** @class */ (function (_super) {
|
|
|
60
60
|
function CrudFlow(repository, model) {
|
|
61
61
|
if (model === void 0) { model = {}; }
|
|
62
62
|
var _this = _super.call(this, model) || this;
|
|
63
|
-
_this.
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
63
|
+
_this.repository = repository;
|
|
64
|
+
return _this;
|
|
65
|
+
}
|
|
66
|
+
CrudFlow.prototype.create = function (data, session) {
|
|
67
|
+
if (session === void 0) { session = undefined; }
|
|
68
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
69
|
+
return __generator(this, function (_a) {
|
|
70
|
+
switch (_a.label) {
|
|
71
|
+
case 0: return [4 /*yield*/, this.repository.create([data], { session: session })];
|
|
72
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
73
|
+
}
|
|
72
74
|
});
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
}
|
|
75
|
+
});
|
|
76
|
+
};
|
|
77
|
+
CrudFlow.prototype.delete = function (id, session) {
|
|
78
|
+
if (session === void 0) { session = undefined; }
|
|
79
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
80
|
+
return __generator(this, function (_a) {
|
|
81
|
+
switch (_a.label) {
|
|
82
|
+
case 0: return [4 /*yield*/, this.repository.findByIdAndRemove(id, { session: session })];
|
|
83
|
+
case 1:
|
|
84
|
+
_a.sent();
|
|
85
|
+
return [2 /*return*/];
|
|
86
|
+
}
|
|
85
87
|
});
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
}
|
|
88
|
+
});
|
|
89
|
+
};
|
|
90
|
+
CrudFlow.prototype.update = function (id, data, session) {
|
|
91
|
+
if (session === void 0) { session = undefined; }
|
|
92
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
93
|
+
var dataAfter;
|
|
94
|
+
return __generator(this, function (_a) {
|
|
95
|
+
switch (_a.label) {
|
|
96
|
+
case 0: return [4 /*yield*/, this.repository.findByIdAndUpdate(id, { $set: data }, { returnDocument: 'after', session: session })];
|
|
97
|
+
case 1:
|
|
98
|
+
dataAfter = _a.sent();
|
|
99
|
+
return [2 /*return*/, dataAfter];
|
|
100
|
+
}
|
|
99
101
|
});
|
|
100
|
-
};
|
|
101
|
-
|
|
102
|
+
});
|
|
103
|
+
};
|
|
104
|
+
CrudFlow.prototype.find = function (search) {
|
|
105
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
102
106
|
return __generator(this, function (_a) {
|
|
103
107
|
switch (_a.label) {
|
|
104
108
|
case 0: return [4 /*yield*/, search.search(this.repository)];
|
|
105
109
|
case 1: return [2 /*return*/, _a.sent()];
|
|
106
110
|
}
|
|
107
111
|
});
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
}
|
|
112
|
+
});
|
|
113
|
+
};
|
|
114
|
+
CrudFlow.prototype.getOne = function (model, params) {
|
|
115
|
+
if (params === void 0) { params = {}; }
|
|
116
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
117
|
+
var data;
|
|
118
|
+
return __generator(this, function (_a) {
|
|
119
|
+
switch (_a.label) {
|
|
120
|
+
case 0: return [4 /*yield*/, _super.prototype.findOne.call(this, this.repository, model, params)];
|
|
121
|
+
case 1:
|
|
122
|
+
data = _a.sent();
|
|
123
|
+
return [2 /*return*/, data];
|
|
124
|
+
}
|
|
121
125
|
});
|
|
122
|
-
};
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
}
|
|
126
|
+
});
|
|
127
|
+
};
|
|
128
|
+
CrudFlow.prototype.get = function (id, pop, sel) {
|
|
129
|
+
if (pop === void 0) { pop = ""; }
|
|
130
|
+
if (sel === void 0) { sel = ""; }
|
|
131
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
132
|
+
var data;
|
|
133
|
+
return __generator(this, function (_a) {
|
|
134
|
+
switch (_a.label) {
|
|
135
|
+
case 0: return [4 /*yield*/, this.repository.findById(id).populate(pop).select(sel)];
|
|
136
|
+
case 1:
|
|
137
|
+
data = _a.sent();
|
|
138
|
+
return [2 /*return*/, data];
|
|
139
|
+
}
|
|
136
140
|
});
|
|
137
|
-
};
|
|
138
|
-
|
|
139
|
-
return _this;
|
|
140
|
-
}
|
|
141
|
+
});
|
|
142
|
+
};
|
|
141
143
|
return CrudFlow;
|
|
142
144
|
}(SearchFlow_1.default));
|
|
143
145
|
exports.default = CrudFlow;
|
|
@@ -9,19 +9,19 @@ declare abstract class SearchFlow {
|
|
|
9
9
|
populate: string | [] | any;
|
|
10
10
|
filters: any;
|
|
11
11
|
constructor(params: any);
|
|
12
|
-
buildPopulate
|
|
13
|
-
buildPath
|
|
14
|
-
buildOrdenation
|
|
12
|
+
buildPopulate(): any;
|
|
13
|
+
buildPath(target: string, nested?: string): any;
|
|
14
|
+
buildOrdenation(): any;
|
|
15
15
|
private isPageable;
|
|
16
|
-
diacriticSensitiveRegex
|
|
17
|
-
search
|
|
16
|
+
diacriticSensitiveRegex(string?: string): string;
|
|
17
|
+
search(model: mongoose.Model<any>): Promise<SearchResponse<any>>;
|
|
18
18
|
private searchPageable;
|
|
19
19
|
private searchNoPageable;
|
|
20
20
|
private result;
|
|
21
|
-
count
|
|
21
|
+
count(model: mongoose.Model<any>): Promise<number>;
|
|
22
22
|
findOne(repository: mongoose.Model<any>, model: any, params?: any): Promise<any>;
|
|
23
|
-
sumBy
|
|
24
|
-
buildDefaultFilters
|
|
25
|
-
addFilterModel
|
|
23
|
+
sumBy(model: mongoose.Model<any>, _sum: any, _by: any): Promise<any[]>;
|
|
24
|
+
buildDefaultFilters(objectSearch: any): any;
|
|
25
|
+
addFilterModel(model: any, filters: any): void;
|
|
26
26
|
}
|
|
27
27
|
export default SearchFlow;
|
package/dist/flow/SearchFlow.js
CHANGED
|
@@ -52,37 +52,6 @@ var Utils_1 = require("../utils/Utils");
|
|
|
52
52
|
var SearchFlow = /** @class */ (function () {
|
|
53
53
|
function SearchFlow(params) {
|
|
54
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
55
|
this.isPageable = function () {
|
|
87
56
|
if ((0, Utils_1.isEmpty)(_this.page)) {
|
|
88
57
|
_this.page = 1;
|
|
@@ -92,28 +61,6 @@ var SearchFlow = /** @class */ (function () {
|
|
|
92
61
|
}
|
|
93
62
|
return false;
|
|
94
63
|
};
|
|
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
64
|
this.searchPageable = function (model) { return __awaiter(_this, void 0, void 0, function () {
|
|
118
65
|
var sort, items;
|
|
119
66
|
return __generator(this, function (_a) {
|
|
@@ -135,7 +82,72 @@ var SearchFlow = /** @class */ (function () {
|
|
|
135
82
|
}
|
|
136
83
|
});
|
|
137
84
|
}); };
|
|
138
|
-
this.
|
|
85
|
+
this.searchText = params.searchText || "";
|
|
86
|
+
this.order = params.order || "asc";
|
|
87
|
+
this.orderBy = params.orderBy || "_id";
|
|
88
|
+
this.select = params.select;
|
|
89
|
+
this.populate = params.populate;
|
|
90
|
+
this.page = params.page || undefined;
|
|
91
|
+
this.limit = params.limit || 25;
|
|
92
|
+
this.buildPopulate();
|
|
93
|
+
}
|
|
94
|
+
SearchFlow.prototype.buildPopulate = function () {
|
|
95
|
+
if ((0, Utils_1.isEmpty)(this.populate)) {
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
var propertiesArray = this.populate.split(',');
|
|
99
|
+
var populates = [];
|
|
100
|
+
for (var _i = 0, propertiesArray_1 = propertiesArray; _i < propertiesArray_1.length; _i++) {
|
|
101
|
+
var property = propertiesArray_1[_i];
|
|
102
|
+
var _a = property.split('.'), first = _a[0], rest = _a.slice(1);
|
|
103
|
+
var nested = rest.join('.');
|
|
104
|
+
populates.push(this.buildPath(first, nested));
|
|
105
|
+
}
|
|
106
|
+
this.populate = populates;
|
|
107
|
+
};
|
|
108
|
+
SearchFlow.prototype.buildPath = function (target, nested) {
|
|
109
|
+
if (nested === void 0) { nested = ""; }
|
|
110
|
+
var populate = {};
|
|
111
|
+
populate.path = target;
|
|
112
|
+
if ((0, Utils_1.isNotEmpty)(nested)) {
|
|
113
|
+
var _a = nested.split('.'), first = _a[0], rest = _a.slice(1);
|
|
114
|
+
var nested2 = rest.join('.');
|
|
115
|
+
populate.populate = this.buildPath(first, nested2);
|
|
116
|
+
}
|
|
117
|
+
return populate;
|
|
118
|
+
};
|
|
119
|
+
SearchFlow.prototype.buildOrdenation = function () {
|
|
120
|
+
var order = {};
|
|
121
|
+
this.orderBy = this.orderBy || "_id";
|
|
122
|
+
order[this.orderBy] = this.order === "desc" ? -1 : 1;
|
|
123
|
+
return order;
|
|
124
|
+
};
|
|
125
|
+
SearchFlow.prototype.diacriticSensitiveRegex = function (string) {
|
|
126
|
+
if (string === void 0) { string = ""; }
|
|
127
|
+
return string
|
|
128
|
+
.replace(/[a|á|à|ä|â|A|Á|Â|Ã|Ä]/g, '[a,á,à,ä,â,A,Á,Â,Ã,Ä]')
|
|
129
|
+
.replace(/[e|é|ë|è|E|É|Ë|È]/g, '[e,é,ë,è,E,É,Ë,È]')
|
|
130
|
+
.replace(/[i|í|ï|ì|I|Í|Ï|Ì]/g, '[i,í,ï,ì,I,Í,Ï,Ì]')
|
|
131
|
+
.replace(/[o|ó|ö|ò|õ|O|Ó|Ö|Ô|Õ]/g, '[o,ó,ö,ò,õ,O,Ó,Ö,Ô,Õ]')
|
|
132
|
+
.replace(/[u|ü|ú|ù|U|Ú|Ü|Ù]/g, '[u,ü,ú,ù,U,Ú,Ü,Ù]')
|
|
133
|
+
.replace(/[ç|Ç|c|C]/g, '[c,C,ç,Ç]');
|
|
134
|
+
};
|
|
135
|
+
SearchFlow.prototype.search = function (model) {
|
|
136
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
137
|
+
return __generator(this, function (_a) {
|
|
138
|
+
switch (_a.label) {
|
|
139
|
+
case 0:
|
|
140
|
+
if (!this.isPageable()) return [3 /*break*/, 2];
|
|
141
|
+
return [4 /*yield*/, this.searchPageable(model)];
|
|
142
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
143
|
+
case 2: return [4 /*yield*/, this.searchNoPageable(model)];
|
|
144
|
+
case 3: return [2 /*return*/, _a.sent()];
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
});
|
|
148
|
+
};
|
|
149
|
+
SearchFlow.prototype.searchNoPageable = function (model) {
|
|
150
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
139
151
|
var sort, items;
|
|
140
152
|
return __generator(this, function (_a) {
|
|
141
153
|
switch (_a.label) {
|
|
@@ -153,8 +165,10 @@ var SearchFlow = /** @class */ (function () {
|
|
|
153
165
|
return [2 /*return*/, this.result(model, items)];
|
|
154
166
|
}
|
|
155
167
|
});
|
|
156
|
-
});
|
|
157
|
-
|
|
168
|
+
});
|
|
169
|
+
};
|
|
170
|
+
SearchFlow.prototype.result = function (model, items) {
|
|
171
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
158
172
|
var total, paging, searchResponse;
|
|
159
173
|
return __generator(this, function (_a) {
|
|
160
174
|
switch (_a.label) {
|
|
@@ -171,16 +185,34 @@ var SearchFlow = /** @class */ (function () {
|
|
|
171
185
|
return [2 /*return*/, searchResponse];
|
|
172
186
|
}
|
|
173
187
|
});
|
|
174
|
-
});
|
|
175
|
-
|
|
188
|
+
});
|
|
189
|
+
};
|
|
190
|
+
SearchFlow.prototype.count = function (model) {
|
|
191
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
176
192
|
return __generator(this, function (_a) {
|
|
177
193
|
switch (_a.label) {
|
|
178
194
|
case 0: return [4 /*yield*/, model.countDocuments(this.filters).exec()];
|
|
179
195
|
case 1: return [2 /*return*/, _a.sent()];
|
|
180
196
|
}
|
|
181
197
|
});
|
|
182
|
-
});
|
|
183
|
-
|
|
198
|
+
});
|
|
199
|
+
};
|
|
200
|
+
SearchFlow.prototype.findOne = function (repository, model, params) {
|
|
201
|
+
if (params === void 0) { params = {}; }
|
|
202
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
203
|
+
return __generator(this, function (_a) {
|
|
204
|
+
switch (_a.label) {
|
|
205
|
+
case 0: return [4 /*yield*/, repository.findOne(model)
|
|
206
|
+
.sort(params.sort)
|
|
207
|
+
.select(params.select)
|
|
208
|
+
.populate(params.populate)];
|
|
209
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
210
|
+
}
|
|
211
|
+
});
|
|
212
|
+
});
|
|
213
|
+
};
|
|
214
|
+
SearchFlow.prototype.sumBy = function (model, _sum, _by) {
|
|
215
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
184
216
|
var ret;
|
|
185
217
|
return __generator(this, function (_a) {
|
|
186
218
|
switch (_a.label) {
|
|
@@ -201,77 +233,55 @@ var SearchFlow = /** @class */ (function () {
|
|
|
201
233
|
return [2 /*return*/, ret];
|
|
202
234
|
}
|
|
203
235
|
});
|
|
204
|
-
});
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
236
|
+
});
|
|
237
|
+
};
|
|
238
|
+
SearchFlow.prototype.buildDefaultFilters = function (objectSearch) {
|
|
239
|
+
var filters = { $and: [] };
|
|
240
|
+
Object.entries(objectSearch).forEach(function (_a) {
|
|
241
|
+
var key = _a[0], value = _a[1];
|
|
242
|
+
if ((0, Utils_1.isNotEmpty)(value)) {
|
|
243
|
+
var condition = {};
|
|
244
|
+
if (['order', 'orderBy', 'properties', 'populate', 'page', 'limit', 'model', 'searchText'].includes(key)) {
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
if (key.endsWith('DateRange')) {
|
|
248
|
+
var keyAux = key.replace('Range', '');
|
|
249
|
+
var values = value;
|
|
250
|
+
if ((0, Utils_1.isNotEmpty)(values[0])) {
|
|
251
|
+
condition[keyAux] = __assign(__assign({}, condition[keyAux]), { $gte: new Date(values[0]) });
|
|
213
252
|
}
|
|
214
|
-
if (
|
|
215
|
-
|
|
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
|
-
}
|
|
253
|
+
if ((0, Utils_1.isNotEmpty)(values[1])) {
|
|
254
|
+
condition[keyAux] = __assign(__assign({}, condition[keyAux]), { $lte: new Date(values[1]) });
|
|
223
255
|
}
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
else {
|
|
229
|
-
condition[key] = { $in: value };
|
|
230
|
-
}
|
|
256
|
+
}
|
|
257
|
+
else {
|
|
258
|
+
if (!Array.isArray(value)) {
|
|
259
|
+
condition[key] = value;
|
|
231
260
|
}
|
|
232
|
-
|
|
233
|
-
|
|
261
|
+
else {
|
|
262
|
+
condition[key] = { $in: value };
|
|
234
263
|
}
|
|
235
264
|
}
|
|
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;
|
|
265
|
+
if ((0, Utils_1.isNotEmpty)(condition)) {
|
|
250
266
|
filters.$and.push(condition);
|
|
251
267
|
}
|
|
252
|
-
}
|
|
253
|
-
};
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
.sort(params.sort)
|
|
270
|
-
.select(params.select)
|
|
271
|
-
.populate(params.populate)];
|
|
272
|
-
case 1: return [2 /*return*/, _a.sent()];
|
|
273
|
-
}
|
|
274
|
-
});
|
|
268
|
+
}
|
|
269
|
+
});
|
|
270
|
+
if ((0, Utils_1.isNotEmpty)(objectSearch.model)) {
|
|
271
|
+
this.addFilterModel(objectSearch.model, filters);
|
|
272
|
+
}
|
|
273
|
+
if (filters.$and.length === 0)
|
|
274
|
+
delete filters['$and'];
|
|
275
|
+
return filters;
|
|
276
|
+
};
|
|
277
|
+
SearchFlow.prototype.addFilterModel = function (model, filters) {
|
|
278
|
+
Object.entries(model).forEach(function (_a) {
|
|
279
|
+
var key = _a[0], value = _a[1];
|
|
280
|
+
if ((0, Utils_1.isNotEmpty)(value)) {
|
|
281
|
+
var condition = {};
|
|
282
|
+
condition[key] = value;
|
|
283
|
+
filters.$and.push(condition);
|
|
284
|
+
}
|
|
275
285
|
});
|
|
276
286
|
};
|
|
277
287
|
return SearchFlow;
|
package/package.json
CHANGED
package/src/flow/CrudFlow.ts
CHANGED
|
@@ -10,29 +10,29 @@ class CrudFlow<D> extends SearchFlow {
|
|
|
10
10
|
this.repository = repository
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
-
public
|
|
13
|
+
public async create(data: any, session: any = undefined) {
|
|
14
14
|
return await this.repository.create([data], { session })
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
-
public
|
|
17
|
+
public async delete(id: string, session: any = undefined) {
|
|
18
18
|
await this.repository.findByIdAndRemove(id, { session })
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
-
public
|
|
21
|
+
public async update(id: string, data: D, session: any = undefined): Promise<D> {
|
|
22
22
|
const dataAfter = await this.repository.findByIdAndUpdate(id, { $set: data as any }, { returnDocument: 'after', session })
|
|
23
23
|
return dataAfter as D
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
-
public
|
|
26
|
+
public async find(search: SearchFlow): Promise<SearchResponse<D>> {
|
|
27
27
|
return await search.search(this.repository)
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
-
public
|
|
30
|
+
public async getOne(model: D, params: any = {}): Promise<D> {
|
|
31
31
|
const data = await super.findOne(this.repository, model, params)
|
|
32
32
|
return data as D
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
-
public
|
|
35
|
+
public async get(id: string, pop = "", sel = ""): Promise<D> {
|
|
36
36
|
const data = await this.repository.findById(id).populate(pop).select(sel)
|
|
37
37
|
return data as D
|
|
38
38
|
}
|
package/src/flow/SearchFlow.ts
CHANGED
|
@@ -23,7 +23,7 @@ abstract class SearchFlow {
|
|
|
23
23
|
this.buildPopulate()
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
-
public buildPopulate
|
|
26
|
+
public buildPopulate(): any {
|
|
27
27
|
if (isEmpty(this.populate)) {
|
|
28
28
|
return
|
|
29
29
|
}
|
|
@@ -38,7 +38,7 @@ abstract class SearchFlow {
|
|
|
38
38
|
this.populate = populates
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
-
public buildPath
|
|
41
|
+
public buildPath(target: string, nested: string = "") {
|
|
42
42
|
var populate = {} as any
|
|
43
43
|
populate.path = target
|
|
44
44
|
if (isNotEmpty(nested)) {
|
|
@@ -49,7 +49,7 @@ abstract class SearchFlow {
|
|
|
49
49
|
return populate
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
-
public buildOrdenation
|
|
52
|
+
public buildOrdenation() {
|
|
53
53
|
let order = {} as any
|
|
54
54
|
this.orderBy = this.orderBy || "_id"
|
|
55
55
|
order[this.orderBy] = this.order === "desc" ? -1 : 1
|
|
@@ -68,7 +68,7 @@ abstract class SearchFlow {
|
|
|
68
68
|
return false
|
|
69
69
|
}
|
|
70
70
|
|
|
71
|
-
public diacriticSensitiveRegex
|
|
71
|
+
public diacriticSensitiveRegex(string: string = ""): string {
|
|
72
72
|
return string
|
|
73
73
|
.replace(/[a|á|à|ä|â|A|Á|Â|Ã|Ä]/g, '[a,á,à,ä,â,A,Á,Â,Ã,Ä]')
|
|
74
74
|
.replace(/[e|é|ë|è|E|É|Ë|È]/g, '[e,é,ë,è,E,É,Ë,È]')
|
|
@@ -78,7 +78,7 @@ abstract class SearchFlow {
|
|
|
78
78
|
.replace(/[ç|Ç|c|C]/g, '[c,C,ç,Ç]')
|
|
79
79
|
}
|
|
80
80
|
|
|
81
|
-
public
|
|
81
|
+
public async search(model: mongoose.Model<any>): Promise<SearchResponse<any>> {
|
|
82
82
|
if (this.isPageable()) {
|
|
83
83
|
return await this.searchPageable(model)
|
|
84
84
|
}
|
|
@@ -101,7 +101,7 @@ abstract class SearchFlow {
|
|
|
101
101
|
return this.result(model, items)
|
|
102
102
|
}
|
|
103
103
|
|
|
104
|
-
private
|
|
104
|
+
private async searchNoPageable(model: mongoose.Model<any>): Promise<SearchResponse<any>> {
|
|
105
105
|
const sort = this.buildOrdenation()
|
|
106
106
|
var items = await model
|
|
107
107
|
.find(this.filters, this.select)
|
|
@@ -114,7 +114,7 @@ abstract class SearchFlow {
|
|
|
114
114
|
return this.result(model, items)
|
|
115
115
|
}
|
|
116
116
|
|
|
117
|
-
private
|
|
117
|
+
private async result(model: mongoose.Model<any>, items: []): Promise<SearchResponse<any>> {
|
|
118
118
|
var total = await this.count(model)
|
|
119
119
|
|
|
120
120
|
var paging: Paging = {}
|
|
@@ -129,7 +129,7 @@ abstract class SearchFlow {
|
|
|
129
129
|
return searchResponse
|
|
130
130
|
}
|
|
131
131
|
|
|
132
|
-
public
|
|
132
|
+
public async count(model: mongoose.Model<any>): Promise<number> {
|
|
133
133
|
return await model.countDocuments(this.filters).exec()
|
|
134
134
|
}
|
|
135
135
|
|
|
@@ -140,7 +140,7 @@ abstract class SearchFlow {
|
|
|
140
140
|
.populate(params.populate)
|
|
141
141
|
}
|
|
142
142
|
|
|
143
|
-
public
|
|
143
|
+
public async sumBy(model: mongoose.Model<any>, _sum: any, _by: any): Promise<any[]> {
|
|
144
144
|
const ret = await model.aggregate(
|
|
145
145
|
[
|
|
146
146
|
{
|
|
@@ -158,7 +158,7 @@ abstract class SearchFlow {
|
|
|
158
158
|
return ret
|
|
159
159
|
}
|
|
160
160
|
|
|
161
|
-
public buildDefaultFilters
|
|
161
|
+
public buildDefaultFilters(objectSearch: any) {
|
|
162
162
|
let filters = { $and: [] } as any
|
|
163
163
|
Object.entries(objectSearch).forEach(([key, value]) => {
|
|
164
164
|
if (isNotEmpty(value)) {
|
|
@@ -208,7 +208,7 @@ abstract class SearchFlow {
|
|
|
208
208
|
return filters
|
|
209
209
|
}
|
|
210
210
|
|
|
211
|
-
public addFilterModel
|
|
211
|
+
public addFilterModel(model: any, filters: any) {
|
|
212
212
|
Object.entries(model).forEach(([key, value]) => {
|
|
213
213
|
if (isNotEmpty(value)) {
|
|
214
214
|
let condition = {} as any
|