c2-mongoose 2.0.1 → 2.0.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/dist/flow/CrudFlow.d.ts +13 -0
- package/dist/flow/CrudFlow.js +122 -0
- package/dist/flow/SearchFlow.d.ts +26 -0
- package/dist/flow/SearchFlow.js +265 -0
- package/dist/index.d.ts +3 -2
- package/dist/index.js +5 -2
- package/package.json +1 -1
- package/src/flow/CrudFlow.ts +42 -0
- package/src/{model/Search.ts → flow/SearchFlow.ts} +2 -2
- package/src/index.ts +3 -2
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import mongoose from "mongoose";
|
|
2
|
+
import SearchFlow from "./SearchFlow";
|
|
3
|
+
declare class CrudFlow<D> {
|
|
4
|
+
private repository;
|
|
5
|
+
constructor(repository: mongoose.Model<any>);
|
|
6
|
+
create: (data: D, session?: any) => Promise<D>;
|
|
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
|
+
findOne: (model: D, sort?: undefined) => Promise<D>;
|
|
11
|
+
get: (id: string, pop?: string, sel?: string) => Promise<D>;
|
|
12
|
+
}
|
|
13
|
+
export default CrudFlow;
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
+
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;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
var CrudFlow = /** @class */ (function () {
|
|
40
|
+
function CrudFlow(repository) {
|
|
41
|
+
var _this = this;
|
|
42
|
+
this.create = function (data, session) {
|
|
43
|
+
if (session === void 0) { session = undefined; }
|
|
44
|
+
return __awaiter(_this, void 0, void 0, function () {
|
|
45
|
+
return __generator(this, function (_a) {
|
|
46
|
+
switch (_a.label) {
|
|
47
|
+
case 0: return [4 /*yield*/, this.repository.create([data], { session: session })];
|
|
48
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
};
|
|
53
|
+
this.delete = function (id, session) {
|
|
54
|
+
if (session === void 0) { session = undefined; }
|
|
55
|
+
return __awaiter(_this, void 0, void 0, function () {
|
|
56
|
+
return __generator(this, function (_a) {
|
|
57
|
+
switch (_a.label) {
|
|
58
|
+
case 0: return [4 /*yield*/, this.repository.findByIdAndRemove(id, { session: session })];
|
|
59
|
+
case 1:
|
|
60
|
+
_a.sent();
|
|
61
|
+
return [2 /*return*/];
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
};
|
|
66
|
+
this.update = function (id, data, session) {
|
|
67
|
+
if (session === void 0) { session = undefined; }
|
|
68
|
+
return __awaiter(_this, void 0, void 0, function () {
|
|
69
|
+
var dataAfter;
|
|
70
|
+
return __generator(this, function (_a) {
|
|
71
|
+
switch (_a.label) {
|
|
72
|
+
case 0: return [4 /*yield*/, this.repository.findByIdAndUpdate(id, { $set: data }, { returnDocument: 'after', session: session })];
|
|
73
|
+
case 1:
|
|
74
|
+
dataAfter = _a.sent();
|
|
75
|
+
return [2 /*return*/, dataAfter];
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
};
|
|
80
|
+
this.find = function (search) { return __awaiter(_this, void 0, void 0, function () {
|
|
81
|
+
return __generator(this, function (_a) {
|
|
82
|
+
switch (_a.label) {
|
|
83
|
+
case 0: return [4 /*yield*/, search.search(this.repository)];
|
|
84
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
}); };
|
|
88
|
+
this.findOne = function (model, sort) {
|
|
89
|
+
if (sort === void 0) { sort = undefined; }
|
|
90
|
+
return __awaiter(_this, void 0, void 0, function () {
|
|
91
|
+
var data;
|
|
92
|
+
return __generator(this, function (_a) {
|
|
93
|
+
switch (_a.label) {
|
|
94
|
+
case 0: return [4 /*yield*/, this.repository.findOne(model)
|
|
95
|
+
.sort(sort)];
|
|
96
|
+
case 1:
|
|
97
|
+
data = _a.sent();
|
|
98
|
+
return [2 /*return*/, data];
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
};
|
|
103
|
+
this.get = function (id, pop, sel) {
|
|
104
|
+
if (pop === void 0) { pop = ""; }
|
|
105
|
+
if (sel === void 0) { sel = ""; }
|
|
106
|
+
return __awaiter(_this, void 0, void 0, function () {
|
|
107
|
+
var data;
|
|
108
|
+
return __generator(this, function (_a) {
|
|
109
|
+
switch (_a.label) {
|
|
110
|
+
case 0: return [4 /*yield*/, this.repository.findById(id).populate(pop).select(sel)];
|
|
111
|
+
case 1:
|
|
112
|
+
data = _a.sent();
|
|
113
|
+
return [2 /*return*/, data];
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
};
|
|
118
|
+
this.repository = repository;
|
|
119
|
+
}
|
|
120
|
+
return CrudFlow;
|
|
121
|
+
}());
|
|
122
|
+
exports.default = CrudFlow;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import mongoose from "mongoose";
|
|
2
|
+
declare abstract class SearchFlow {
|
|
3
|
+
searchText: string;
|
|
4
|
+
order: string;
|
|
5
|
+
orderBy: string;
|
|
6
|
+
page: number;
|
|
7
|
+
limit: number;
|
|
8
|
+
select: string;
|
|
9
|
+
populate: string | [] | any;
|
|
10
|
+
filters: any;
|
|
11
|
+
constructor(params: any);
|
|
12
|
+
buildPopulate: () => any;
|
|
13
|
+
buildPath: (target: string, nested?: string) => any;
|
|
14
|
+
buildOrdenation: () => any;
|
|
15
|
+
private isPageable;
|
|
16
|
+
diacriticSensitiveRegex: (string?: string) => string;
|
|
17
|
+
search: (model: mongoose.Model<any>) => Promise<SearchResponse<any>>;
|
|
18
|
+
private searchPageable;
|
|
19
|
+
private searchNoPageable;
|
|
20
|
+
private result;
|
|
21
|
+
count: (model: mongoose.Model<any>) => Promise<number>;
|
|
22
|
+
sumBy: (model: mongoose.Model<any>, _sum: any, _by: any) => Promise<any[]>;
|
|
23
|
+
buildDefaultFilters: (objectSearch: any) => any;
|
|
24
|
+
addFilterModel: (model: any, filters: any) => void;
|
|
25
|
+
}
|
|
26
|
+
export default SearchFlow;
|
|
@@ -0,0 +1,265 @@
|
|
|
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 SearchFlow = /** @class */ (function () {
|
|
53
|
+
function SearchFlow(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 SearchFlow;
|
|
264
|
+
}());
|
|
265
|
+
exports.default = SearchFlow;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
1
|
+
import CrudFlow from "./flow/CrudFlow";
|
|
2
|
+
import SearchFlow from "./flow/SearchFlow";
|
|
3
|
+
export { SearchFlow, CrudFlow };
|
package/dist/index.js
CHANGED
|
@@ -3,5 +3,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
exports.CrudFlow = exports.SearchFlow = 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;
|
package/package.json
CHANGED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import mongoose from "mongoose"
|
|
2
|
+
import SearchFlow from "./SearchFlow"
|
|
3
|
+
|
|
4
|
+
class CrudFlow<D> {
|
|
5
|
+
|
|
6
|
+
private repository: mongoose.Model<any>
|
|
7
|
+
|
|
8
|
+
constructor(repository: mongoose.Model<any>) {
|
|
9
|
+
this.repository = repository
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
public create = async (data: D, session: any = undefined): Promise<D> => {
|
|
13
|
+
return await this.repository.create([data], { session }) as D
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
public delete = async (id: string, session: any = undefined) => {
|
|
17
|
+
await this.repository.findByIdAndRemove(id, { session })
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
public update = async (id: string, data: D, session: any = undefined): Promise<D> => {
|
|
21
|
+
const dataAfter = await this.repository.findByIdAndUpdate(id, { $set: data as any }, { returnDocument: 'after', session })
|
|
22
|
+
return dataAfter as D
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
public find = async (search: SearchFlow): Promise<SearchResponse<D>> => {
|
|
26
|
+
return await search.search(this.repository)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
public findOne = async (model: D, sort = undefined): Promise<D> => {
|
|
30
|
+
const data = await this.repository.findOne(model as any)
|
|
31
|
+
.sort(sort)
|
|
32
|
+
|
|
33
|
+
return data as D
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
public get = async (id: string, pop = "", sel = ""): Promise<D> => {
|
|
37
|
+
const data = await this.repository.findById(id).populate(pop).select(sel)
|
|
38
|
+
return data as D
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export default CrudFlow
|
|
@@ -2,7 +2,7 @@ import mongoose from "mongoose"
|
|
|
2
2
|
import { dbCollation } from "../configuration/Environment"
|
|
3
3
|
import { isEmpty, isNotEmpty } from "../utils/Utils"
|
|
4
4
|
|
|
5
|
-
abstract class
|
|
5
|
+
abstract class SearchFlow {
|
|
6
6
|
searchText: string
|
|
7
7
|
order: string
|
|
8
8
|
orderBy: string
|
|
@@ -212,4 +212,4 @@ abstract class Search {
|
|
|
212
212
|
}
|
|
213
213
|
}
|
|
214
214
|
|
|
215
|
-
export default
|
|
215
|
+
export default SearchFlow
|
package/src/index.ts
CHANGED