c2-mongoose 2.0.1 → 2.0.3
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 +143 -0
- package/dist/flow/SearchFlow.d.ts +27 -0
- package/dist/flow/SearchFlow.js +277 -0
- package/dist/index.d.ts +3 -2
- package/dist/index.js +5 -2
- package/package.json +4 -3
- package/src/flow/CrudFlow.ts +41 -0
- package/src/{model/Search.ts → flow/SearchFlow.ts} +7 -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> extends SearchFlow {
|
|
4
|
+
private repository;
|
|
5
|
+
constructor(repository: mongoose.Model<any>, model?: any);
|
|
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, sort?: undefined) => Promise<D>;
|
|
11
|
+
get: (id: string, pop?: string, sel?: string) => Promise<D>;
|
|
12
|
+
}
|
|
13
|
+
export default CrudFlow;
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __extends = (this && this.__extends) || (function () {
|
|
3
|
+
var extendStatics = function (d, b) {
|
|
4
|
+
extendStatics = Object.setPrototypeOf ||
|
|
5
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
+
return extendStatics(d, b);
|
|
8
|
+
};
|
|
9
|
+
return function (d, b) {
|
|
10
|
+
if (typeof b !== "function" && b !== null)
|
|
11
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
+
extendStatics(d, b);
|
|
13
|
+
function __() { this.constructor = d; }
|
|
14
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
+
};
|
|
16
|
+
})();
|
|
17
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
18
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
19
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
20
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
21
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
22
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
23
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
24
|
+
});
|
|
25
|
+
};
|
|
26
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
27
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
28
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
29
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
30
|
+
function step(op) {
|
|
31
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
32
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
33
|
+
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;
|
|
34
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
35
|
+
switch (op[0]) {
|
|
36
|
+
case 0: case 1: t = op; break;
|
|
37
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
38
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
39
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
40
|
+
default:
|
|
41
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
42
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
43
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
44
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
45
|
+
if (t[2]) _.ops.pop();
|
|
46
|
+
_.trys.pop(); continue;
|
|
47
|
+
}
|
|
48
|
+
op = body.call(thisArg, _);
|
|
49
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
50
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
54
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
55
|
+
};
|
|
56
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
57
|
+
var SearchFlow_1 = __importDefault(require("./SearchFlow"));
|
|
58
|
+
var CrudFlow = /** @class */ (function (_super) {
|
|
59
|
+
__extends(CrudFlow, _super);
|
|
60
|
+
function CrudFlow(repository, model) {
|
|
61
|
+
if (model === void 0) { model = {}; }
|
|
62
|
+
var _this = _super.call(this, model) || this;
|
|
63
|
+
_this.create = function (data, session) {
|
|
64
|
+
if (session === void 0) { session = undefined; }
|
|
65
|
+
return __awaiter(_this, void 0, void 0, function () {
|
|
66
|
+
return __generator(this, function (_a) {
|
|
67
|
+
switch (_a.label) {
|
|
68
|
+
case 0: return [4 /*yield*/, this.repository.create([data], { session: session })];
|
|
69
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
};
|
|
74
|
+
_this.delete = function (id, session) {
|
|
75
|
+
if (session === void 0) { session = undefined; }
|
|
76
|
+
return __awaiter(_this, void 0, void 0, function () {
|
|
77
|
+
return __generator(this, function (_a) {
|
|
78
|
+
switch (_a.label) {
|
|
79
|
+
case 0: return [4 /*yield*/, this.repository.findByIdAndRemove(id, { session: session })];
|
|
80
|
+
case 1:
|
|
81
|
+
_a.sent();
|
|
82
|
+
return [2 /*return*/];
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
};
|
|
87
|
+
_this.update = function (id, data, session) {
|
|
88
|
+
if (session === void 0) { session = undefined; }
|
|
89
|
+
return __awaiter(_this, void 0, void 0, function () {
|
|
90
|
+
var dataAfter;
|
|
91
|
+
return __generator(this, function (_a) {
|
|
92
|
+
switch (_a.label) {
|
|
93
|
+
case 0: return [4 /*yield*/, this.repository.findByIdAndUpdate(id, { $set: data }, { returnDocument: 'after', session: session })];
|
|
94
|
+
case 1:
|
|
95
|
+
dataAfter = _a.sent();
|
|
96
|
+
return [2 /*return*/, dataAfter];
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
};
|
|
101
|
+
_this.find = function (search) { return __awaiter(_this, void 0, void 0, function () {
|
|
102
|
+
return __generator(this, function (_a) {
|
|
103
|
+
switch (_a.label) {
|
|
104
|
+
case 0: return [4 /*yield*/, search.search(this.repository)];
|
|
105
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
}); };
|
|
109
|
+
_this.getOne = function (model, sort) {
|
|
110
|
+
if (sort === void 0) { sort = undefined; }
|
|
111
|
+
return __awaiter(_this, void 0, void 0, function () {
|
|
112
|
+
var data;
|
|
113
|
+
return __generator(this, function (_a) {
|
|
114
|
+
switch (_a.label) {
|
|
115
|
+
case 0: return [4 /*yield*/, _super.prototype.findOne.call(this, this.repository, model, sort)];
|
|
116
|
+
case 1:
|
|
117
|
+
data = _a.sent();
|
|
118
|
+
return [2 /*return*/, data];
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
};
|
|
123
|
+
_this.get = function (id, pop, sel) {
|
|
124
|
+
if (pop === void 0) { pop = ""; }
|
|
125
|
+
if (sel === void 0) { sel = ""; }
|
|
126
|
+
return __awaiter(_this, void 0, void 0, function () {
|
|
127
|
+
var data;
|
|
128
|
+
return __generator(this, function (_a) {
|
|
129
|
+
switch (_a.label) {
|
|
130
|
+
case 0: return [4 /*yield*/, this.repository.findById(id).populate(pop).select(sel)];
|
|
131
|
+
case 1:
|
|
132
|
+
data = _a.sent();
|
|
133
|
+
return [2 /*return*/, data];
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
});
|
|
137
|
+
};
|
|
138
|
+
_this.repository = repository;
|
|
139
|
+
return _this;
|
|
140
|
+
}
|
|
141
|
+
return CrudFlow;
|
|
142
|
+
}(SearchFlow_1.default));
|
|
143
|
+
exports.default = CrudFlow;
|
|
@@ -0,0 +1,27 @@
|
|
|
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
|
+
findOne(repository: mongoose.Model<any>, model: any, sort?: undefined): Promise<any>;
|
|
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
|
+
}
|
|
27
|
+
export default SearchFlow;
|
|
@@ -0,0 +1,277 @@
|
|
|
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
|
+
SearchFlow.prototype.findOne = function (repository, model, sort) {
|
|
264
|
+
if (sort === void 0) { sort = undefined; }
|
|
265
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
266
|
+
return __generator(this, function (_a) {
|
|
267
|
+
switch (_a.label) {
|
|
268
|
+
case 0: return [4 /*yield*/, repository.findOne(model)
|
|
269
|
+
.sort(sort)];
|
|
270
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
271
|
+
}
|
|
272
|
+
});
|
|
273
|
+
});
|
|
274
|
+
};
|
|
275
|
+
return SearchFlow;
|
|
276
|
+
}());
|
|
277
|
+
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
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "c2-mongoose",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.3",
|
|
4
4
|
"description": "Lib to make any search in database mongoose and use as basic crud",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
7
|
"scripts": {
|
|
8
|
-
"
|
|
9
|
-
"
|
|
8
|
+
"watch": "tsc -w --skipLibCheck",
|
|
9
|
+
"build": "tsc --skipLibCheck",
|
|
10
|
+
"preversion": "tsc --skipLibCheck",
|
|
10
11
|
"postversion": "git push && git push --tags && npm publish",
|
|
11
12
|
"publish-patch": "npm version patch",
|
|
12
13
|
"publish-minor": "npm version minor",
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import mongoose from "mongoose"
|
|
2
|
+
import SearchFlow from "./SearchFlow"
|
|
3
|
+
|
|
4
|
+
class CrudFlow<D> extends SearchFlow {
|
|
5
|
+
|
|
6
|
+
private repository: mongoose.Model<any>
|
|
7
|
+
|
|
8
|
+
constructor(repository: mongoose.Model<any>, model: any = {}) {
|
|
9
|
+
super(model as D)
|
|
10
|
+
this.repository = repository
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
public create = async (data: any, session: any = undefined) => {
|
|
14
|
+
return await this.repository.create([data], { session })
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
public delete = async (id: string, session: any = undefined) => {
|
|
18
|
+
await this.repository.findByIdAndRemove(id, { session })
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
public update = async (id: string, data: D, session: any = undefined): Promise<D> => {
|
|
22
|
+
const dataAfter = await this.repository.findByIdAndUpdate(id, { $set: data as any }, { returnDocument: 'after', session })
|
|
23
|
+
return dataAfter as D
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
public find = async (search: SearchFlow): Promise<SearchResponse<D>> => {
|
|
27
|
+
return await search.search(this.repository)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
public getOne = async (model: D, sort = undefined): Promise<D> => {
|
|
31
|
+
const data = await super.findOne(this.repository, model, sort)
|
|
32
|
+
return data as D
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
public get = async (id: string, pop = "", sel = ""): Promise<D> => {
|
|
36
|
+
const data = await this.repository.findById(id).populate(pop).select(sel)
|
|
37
|
+
return data as D
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
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
|
|
@@ -133,6 +133,11 @@ abstract class Search {
|
|
|
133
133
|
return await model.countDocuments(this.filters).exec()
|
|
134
134
|
}
|
|
135
135
|
|
|
136
|
+
public async findOne(repository: mongoose.Model<any>, model: any, sort = undefined) {
|
|
137
|
+
return await repository.findOne(model)
|
|
138
|
+
.sort(sort)
|
|
139
|
+
}
|
|
140
|
+
|
|
136
141
|
public sumBy = async (model: mongoose.Model<any>, _sum: any, _by: any): Promise<any[]> => {
|
|
137
142
|
const ret = await model.aggregate(
|
|
138
143
|
[
|
|
@@ -212,4 +217,4 @@ abstract class Search {
|
|
|
212
217
|
}
|
|
213
218
|
}
|
|
214
219
|
|
|
215
|
-
export default
|
|
220
|
+
export default SearchFlow
|
package/src/index.ts
CHANGED