c2-mongoose 2.1.320 → 2.1.321
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/searcher/SearcherV2Flow.d.ts +10 -0
- package/dist/flow/searcher/SearcherV2Flow.js +100 -0
- package/dist/flow/searcher/item/BuildConditionFlowItem.d.ts +6 -0
- package/dist/flow/searcher/item/BuildConditionFlowItem.js +130 -0
- package/dist/flow/searcher/item/BuildPaginationDataFlowItem.d.ts +5 -0
- package/dist/flow/searcher/item/BuildPaginationDataFlowItem.js +35 -0
- package/dist/flow/searcher/item/BuildRegexFlowItem.d.ts +5 -0
- package/dist/flow/searcher/item/BuildRegexFlowItem.js +24 -0
- package/dist/flow/searcher/item/ExtractFieldsOfTypeFlowItem.d.ts +6 -0
- package/dist/flow/searcher/item/ExtractFieldsOfTypeFlowItem.js +32 -0
- package/dist/flow/searcher/item/GetTypeOfFieldInModelFlowItem.d.ts +6 -0
- package/dist/flow/searcher/item/GetTypeOfFieldInModelFlowItem.js +12 -0
- package/dist/flow/searcher/item/GroupFlowItem.d.ts +6 -0
- package/dist/flow/searcher/item/GroupFlowItem.js +80 -0
- package/dist/flow/searcher/item/IsValidObjectIdFlowItem.d.ts +5 -0
- package/dist/flow/searcher/item/IsValidObjectIdFlowItem.js +12 -0
- package/dist/flow/searcher/item/MatchFlowItem.d.ts +6 -0
- package/dist/flow/searcher/item/MatchFlowItem.js +132 -0
- package/dist/flow/searcher/item/NormalizeValueFlowItem.d.ts +5 -0
- package/dist/flow/searcher/item/NormalizeValueFlowItem.js +25 -0
- package/dist/flow/searcher/item/PopulateFlowItem.d.ts +6 -0
- package/dist/flow/searcher/item/PopulateFlowItem.js +101 -0
- package/dist/flow/searcher/item/ProjectFlowItem.d.ts +5 -0
- package/dist/flow/searcher/item/ProjectFlowItem.js +42 -0
- package/dist/flow/searcher/item/SortFlowItem.d.ts +5 -0
- package/dist/flow/searcher/item/SortFlowItem.js +13 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.js +3 -1
- package/package.json +1 -1
- package/src/flow/searcher/SearcherV2Flow.ts +65 -0
- package/src/flow/searcher/item/BuildConditionFlowItem.ts +126 -0
- package/src/flow/searcher/item/BuildPaginationDataFlowItem.ts +31 -0
- package/src/flow/searcher/item/BuildRegexFlowItem.ts +20 -0
- package/src/flow/searcher/item/ExtractFieldsOfTypeFlowItem.ts +20 -0
- package/src/flow/searcher/item/GetTypeOfFieldInModelFlowItem.ts +10 -0
- package/src/flow/searcher/item/GroupFlowItem.ts +86 -0
- package/src/flow/searcher/item/IsValidObjectIdFlowItem.ts +8 -0
- package/src/flow/searcher/item/MatchFlowItem.ts +113 -0
- package/src/flow/searcher/item/NormalizeValueFlowItem.ts +22 -0
- package/src/flow/searcher/item/PopulateFlowItem.ts +99 -0
- package/src/flow/searcher/item/ProjectFlowItem.ts +34 -0
- package/src/flow/searcher/item/SortFlowItem.ts +9 -0
- package/src/index.ts +2 -1
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import mongoose from "mongoose";
|
|
2
|
+
export interface IParams {
|
|
3
|
+
[key: string]: any;
|
|
4
|
+
mainFilter: any;
|
|
5
|
+
}
|
|
6
|
+
declare class SearcherV2Flow {
|
|
7
|
+
exec(params: IParams, model: mongoose.Model<any>): Promise<any>;
|
|
8
|
+
}
|
|
9
|
+
declare const _default: SearcherV2Flow;
|
|
10
|
+
export default _default;
|
|
@@ -0,0 +1,100 @@
|
|
|
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
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
39
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
40
|
+
if (ar || !(i in from)) {
|
|
41
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
42
|
+
ar[i] = from[i];
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
46
|
+
};
|
|
47
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
48
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
49
|
+
};
|
|
50
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
51
|
+
var GroupFlowItem_1 = __importDefault(require("./item/GroupFlowItem"));
|
|
52
|
+
var PopulateFlowItem_1 = __importDefault(require("./item/PopulateFlowItem"));
|
|
53
|
+
var SortFlowItem_1 = __importDefault(require("./item/SortFlowItem"));
|
|
54
|
+
var ProjectFlowItem_1 = __importDefault(require("./item/ProjectFlowItem"));
|
|
55
|
+
var MatchFlowItem_1 = __importDefault(require("./item/MatchFlowItem"));
|
|
56
|
+
var BuildPaginationDataFlowItem_1 = __importDefault(require("./item/BuildPaginationDataFlowItem"));
|
|
57
|
+
var SearcherV2Flow = /** @class */ (function () {
|
|
58
|
+
function SearcherV2Flow() {
|
|
59
|
+
}
|
|
60
|
+
SearcherV2Flow.prototype.exec = function (params, model) {
|
|
61
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
62
|
+
var facet, pipelineFacetPaging, pipelineFacetItems, result, ret;
|
|
63
|
+
return __generator(this, function (_a) {
|
|
64
|
+
switch (_a.label) {
|
|
65
|
+
case 0:
|
|
66
|
+
facet = {};
|
|
67
|
+
pipelineFacetPaging = [];
|
|
68
|
+
pipelineFacetItems = __spreadArray(__spreadArray(__spreadArray(__spreadArray(__spreadArray([], GroupFlowItem_1.default.exec(params.groupBy, params.sumFields, params.groupDetailed, model), true), PopulateFlowItem_1.default.exec(params.populate, model), true), MatchFlowItem_1.default.exec(params, model), true), SortFlowItem_1.default.exec(params.orderBy, params.orderSense), true), ProjectFlowItem_1.default.exec(params.select), true);
|
|
69
|
+
if (params.pageable) {
|
|
70
|
+
pipelineFacetItems = __spreadArray(__spreadArray([], pipelineFacetItems, true), [
|
|
71
|
+
{ $skip: ((params.page - 1) * params.limit) },
|
|
72
|
+
{ $limit: params.limit }
|
|
73
|
+
], false);
|
|
74
|
+
pipelineFacetPaging = __spreadArray(__spreadArray(__spreadArray(__spreadArray([], GroupFlowItem_1.default.exec(params.groupBy, params.sumFields, params.groupDetailed, model), true), PopulateFlowItem_1.default.exec(params.populate, model), true), MatchFlowItem_1.default.exec(params, model), true), BuildPaginationDataFlowItem_1.default.exec(params.page, params.limit), true);
|
|
75
|
+
facet.paging = pipelineFacetPaging;
|
|
76
|
+
}
|
|
77
|
+
facet.items = pipelineFacetItems;
|
|
78
|
+
return [4 /*yield*/, model.aggregate([
|
|
79
|
+
{
|
|
80
|
+
$match: MatchFlowItem_1.default.exec(params.mainFilter, model)
|
|
81
|
+
},
|
|
82
|
+
{
|
|
83
|
+
$facet: facet
|
|
84
|
+
}
|
|
85
|
+
])];
|
|
86
|
+
case 1:
|
|
87
|
+
result = _a.sent();
|
|
88
|
+
ret = {};
|
|
89
|
+
ret.items = result[0].items;
|
|
90
|
+
if (params.pageable) {
|
|
91
|
+
ret.paging = result[0].paging[0];
|
|
92
|
+
}
|
|
93
|
+
return [2 /*return*/, ret];
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
};
|
|
98
|
+
return SearcherV2Flow;
|
|
99
|
+
}());
|
|
100
|
+
exports.default = new SearcherV2Flow;
|
|
@@ -0,0 +1,130 @@
|
|
|
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 __importDefault = (this && this.__importDefault) || function (mod) {
|
|
14
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
var moment_1 = __importDefault(require("moment"));
|
|
18
|
+
var mongoose_1 = require("mongoose");
|
|
19
|
+
var BuildRegexFlowItem_1 = __importDefault(require("./BuildRegexFlowItem"));
|
|
20
|
+
var IsValidObjectIdFlowItem_1 = __importDefault(require("./IsValidObjectIdFlowItem"));
|
|
21
|
+
var BuildConditionFlowItem = /** @class */ (function () {
|
|
22
|
+
function BuildConditionFlowItem() {
|
|
23
|
+
}
|
|
24
|
+
BuildConditionFlowItem.prototype.exec = function (key, value, model) {
|
|
25
|
+
var _a;
|
|
26
|
+
var _b, _c;
|
|
27
|
+
var condition = {};
|
|
28
|
+
if (key.endsWith('DateRange')) {
|
|
29
|
+
var fieldName = key.replace('Range', '');
|
|
30
|
+
var values = value;
|
|
31
|
+
if (values[0]) {
|
|
32
|
+
var momentValue = (0, moment_1.default)(values[0]);
|
|
33
|
+
momentValue.hour(0);
|
|
34
|
+
momentValue.minute(0);
|
|
35
|
+
momentValue.second(0);
|
|
36
|
+
condition[fieldName] = __assign(__assign({}, condition[fieldName]), { $gte: momentValue.tz('America/Sao_Paulo', false).toDate() });
|
|
37
|
+
}
|
|
38
|
+
if (values[1]) {
|
|
39
|
+
var momentValue = (0, moment_1.default)(values[1]);
|
|
40
|
+
momentValue.hour(23);
|
|
41
|
+
momentValue.minute(59);
|
|
42
|
+
momentValue.second(59);
|
|
43
|
+
condition[fieldName] = __assign(__assign({}, condition[fieldName]), { $lte: momentValue.tz('America/Sao_Paulo', false).toDate() });
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
else if (key.endsWith('DateTimeRange')) {
|
|
47
|
+
var fieldName = key.replace('Range', '');
|
|
48
|
+
var values = value;
|
|
49
|
+
if (values[0]) {
|
|
50
|
+
var momentValue = (0, moment_1.default)(values[0]);
|
|
51
|
+
condition[fieldName] = __assign(__assign({}, condition[fieldName]), { $gte: momentValue.tz('America/Sao_Paulo', false).toDate() });
|
|
52
|
+
}
|
|
53
|
+
if (values[1]) {
|
|
54
|
+
var momentValue = (0, moment_1.default)(values[1]);
|
|
55
|
+
condition[fieldName] = __assign(__assign({}, condition[fieldName]), { $lte: momentValue.tz('America/Sao_Paulo', false).toDate() });
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
else if (key.endsWith('Range')) {
|
|
59
|
+
var fieldName = key.replace('Range', '');
|
|
60
|
+
var values = value;
|
|
61
|
+
if (values[0]) {
|
|
62
|
+
condition[fieldName] = __assign(__assign({}, condition[fieldName]), { $gte: Number(values[0]) });
|
|
63
|
+
}
|
|
64
|
+
if (values[1]) {
|
|
65
|
+
condition[fieldName] = __assign(__assign({}, condition[fieldName]), { $lte: Number(values[1]) });
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
else if (key.endsWith('Like')) {
|
|
69
|
+
var fieldName = key.replace('Like', '');
|
|
70
|
+
condition[fieldName] = BuildRegexFlowItem_1.default.exec(value); //{ $regex: value as any, $options: 'i' }
|
|
71
|
+
}
|
|
72
|
+
else if (key.endsWith("AtLeastOneExists")) {
|
|
73
|
+
key = key.replace("AtLeastOneExists", "");
|
|
74
|
+
var fields = key.split('.');
|
|
75
|
+
if (!fields)
|
|
76
|
+
return condition;
|
|
77
|
+
condition[fields[0]] = { $elemMatch: (_a = {}, _a[fields[1]] = { $exists: value }, _a) };
|
|
78
|
+
}
|
|
79
|
+
else if (key.endsWith('Exists')) {
|
|
80
|
+
var fieldName = key.replace('Exists', '');
|
|
81
|
+
condition[fieldName] = { $exists: value };
|
|
82
|
+
}
|
|
83
|
+
else if (key.endsWith("IsEmpty")) {
|
|
84
|
+
key = key.replace("IsEmpty", "");
|
|
85
|
+
condition[key] = value ? { $size: 0 } : { $not: { $size: 0 } };
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
if (((_c = (_b = model === null || model === void 0 ? void 0 : model.schema) === null || _b === void 0 ? void 0 : _b.paths[key]) === null || _c === void 0 ? void 0 : _c.instance) === 'Array') {
|
|
89
|
+
if (!Array.isArray(value)) {
|
|
90
|
+
value = [value];
|
|
91
|
+
}
|
|
92
|
+
value === null || value === void 0 ? void 0 : value.forEach(function (v) {
|
|
93
|
+
if (typeof v === 'string' && IsValidObjectIdFlowItem_1.default.exec(v)) {
|
|
94
|
+
v = new mongoose_1.Types.ObjectId(v);
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
if (key.endsWith("NotIn")) {
|
|
98
|
+
key = key.replace("NotIn", "");
|
|
99
|
+
condition[key] = { $nin: value };
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
condition[key] = { $in: value };
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
else if (Array.isArray(value)) {
|
|
106
|
+
var arr = [];
|
|
107
|
+
for (var _i = 0, value_1 = value; _i < value_1.length; _i++) {
|
|
108
|
+
var val = value_1[_i];
|
|
109
|
+
if (typeof val === 'string' && IsValidObjectIdFlowItem_1.default.exec(val)) {
|
|
110
|
+
val = new mongoose_1.Types.ObjectId(val);
|
|
111
|
+
}
|
|
112
|
+
arr.push(val);
|
|
113
|
+
}
|
|
114
|
+
if (key.endsWith("NotIn")) {
|
|
115
|
+
key = key.replace("NotIn", "");
|
|
116
|
+
condition[key] = { $nin: arr };
|
|
117
|
+
}
|
|
118
|
+
else {
|
|
119
|
+
condition[key] = { $in: arr };
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
condition[key] = value;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return condition;
|
|
127
|
+
};
|
|
128
|
+
return BuildConditionFlowItem;
|
|
129
|
+
}());
|
|
130
|
+
exports.default = new BuildConditionFlowItem;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
var BuildPaginationDataFlowItem = /** @class */ (function () {
|
|
4
|
+
function BuildPaginationDataFlowItem() {
|
|
5
|
+
}
|
|
6
|
+
BuildPaginationDataFlowItem.prototype.exec = function (page, limit) {
|
|
7
|
+
return [
|
|
8
|
+
{ $count: "total" },
|
|
9
|
+
{
|
|
10
|
+
$addFields: {
|
|
11
|
+
page: page,
|
|
12
|
+
limit: limit,
|
|
13
|
+
total: "$total",
|
|
14
|
+
totalPages: {
|
|
15
|
+
$cond: {
|
|
16
|
+
if: { $eq: ['$total', 0] },
|
|
17
|
+
then: 0,
|
|
18
|
+
else: { $ceil: { $divide: ['$total', limit] } }
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
startIndex: { $subtract: [{ $multiply: [page, limit] }, limit - 1] },
|
|
22
|
+
endIndex: {
|
|
23
|
+
$cond: {
|
|
24
|
+
if: { $gt: [{ $multiply: [page, limit] }, "$total"] },
|
|
25
|
+
then: "$total",
|
|
26
|
+
else: { $multiply: [page, limit] }
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
];
|
|
32
|
+
};
|
|
33
|
+
return BuildPaginationDataFlowItem;
|
|
34
|
+
}());
|
|
35
|
+
exports.default = new BuildPaginationDataFlowItem;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
var BuildRegexFlowItem = /** @class */ (function () {
|
|
4
|
+
function BuildRegexFlowItem() {
|
|
5
|
+
}
|
|
6
|
+
BuildRegexFlowItem.prototype.exec = function (searchText) {
|
|
7
|
+
var regexExpression = searchText
|
|
8
|
+
.replace(/[+?^${}()|[\]\\]/g, '\\$&') //Tratar os caracteres que podem ser chaves, escape os demais q podem ser regex
|
|
9
|
+
.replace(/^"/, '^').replace(/"$/, '$') //Replace aspas duplas para busca exata
|
|
10
|
+
.replace(/^%22/, '^').replace(/%22$/, '$') //Replace aspas duplas para busca exata
|
|
11
|
+
.replace(/%20/, ' ') //Replace espaço em branco para nao quebrar o proximo replace
|
|
12
|
+
.replace(/\*/g, '.*?') //Replace % para o padrão regex (% é muito utilizado como o .*? e é mais simples para o usuario digitar)
|
|
13
|
+
.replace(/%/g, '.*?') //Replace % para o padrão regex (% é muito utilizado como o .*? e é mais simples para o usuario digitar)
|
|
14
|
+
.replace(/[ç|Ç|c|C]/g, '[c,C,ç,Ç]')
|
|
15
|
+
.replace(/[a|á|à|ä|â|A|Á|Â|Ã|Ä]/g, '[a,á,à,ä,â,A,Á,Â,Ã,Ä]')
|
|
16
|
+
.replace(/[e|é|ë|è|E|É|Ë|È]/g, '[e,é,ë,è,E,É,Ë,È]')
|
|
17
|
+
.replace(/[i|í|ï|ì|I|Í|Ï|Ì]/g, '[i,í,ï,ì,I,Í,Ï,Ì]')
|
|
18
|
+
.replace(/[o|ó|ö|ò|õ|O|Ó|Ö|Ô|Õ]/g, '[o,ó,ö,ò,õ,O,Ó,Ö,Ô,Õ]')
|
|
19
|
+
.replace(/[u|ü|ú|ù|U|Ú|Ü|Ù]/g, '[u,ü,ú,ù,U,Ú,Ü,Ù]');
|
|
20
|
+
return new RegExp("".concat(regexExpression), 'i');
|
|
21
|
+
};
|
|
22
|
+
return BuildRegexFlowItem;
|
|
23
|
+
}());
|
|
24
|
+
exports.default = new BuildRegexFlowItem;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
3
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
4
|
+
if (ar || !(i in from)) {
|
|
5
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
6
|
+
ar[i] = from[i];
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
var ExtractFieldsOfTypeFlowItem = /** @class */ (function () {
|
|
13
|
+
function ExtractFieldsOfTypeFlowItem() {
|
|
14
|
+
}
|
|
15
|
+
ExtractFieldsOfTypeFlowItem.prototype.exec = function (schema, typing, parent) {
|
|
16
|
+
var _this = this;
|
|
17
|
+
if (typing === void 0) { typing = 'String'; }
|
|
18
|
+
if (parent === void 0) { parent = ''; }
|
|
19
|
+
var fieldsTyped = [];
|
|
20
|
+
Object.keys(schema.paths).forEach(function (field) {
|
|
21
|
+
if (schema.paths[field].instance === typing) {
|
|
22
|
+
fieldsTyped.push("".concat(parent !== '' ? "".concat(parent, ".") : '').concat(field));
|
|
23
|
+
}
|
|
24
|
+
else if (schema.paths[field].instance === 'Embedded') {
|
|
25
|
+
fieldsTyped = __spreadArray(__spreadArray([], fieldsTyped, true), _this.exec(schema.paths[field].schema, typing, field), true);
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
return fieldsTyped;
|
|
29
|
+
};
|
|
30
|
+
return ExtractFieldsOfTypeFlowItem;
|
|
31
|
+
}());
|
|
32
|
+
exports.default = new ExtractFieldsOfTypeFlowItem;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
var GetTypeOfFieldInModelFlowItem = /** @class */ (function () {
|
|
4
|
+
function GetTypeOfFieldInModelFlowItem() {
|
|
5
|
+
}
|
|
6
|
+
GetTypeOfFieldInModelFlowItem.prototype.exec = function (fieldToGroup, modelRoot) {
|
|
7
|
+
var fieldDefinition = modelRoot.schema.path(fieldToGroup);
|
|
8
|
+
return fieldDefinition.instance;
|
|
9
|
+
};
|
|
10
|
+
return GetTypeOfFieldInModelFlowItem;
|
|
11
|
+
}());
|
|
12
|
+
exports.default = new GetTypeOfFieldInModelFlowItem;
|
|
@@ -0,0 +1,80 @@
|
|
|
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
|
+
var GetTypeOfFieldInModelFlowItem_1 = __importDefault(require("./GetTypeOfFieldInModelFlowItem"));
|
|
7
|
+
var GroupFlowItem = /** @class */ (function () {
|
|
8
|
+
function GroupFlowItem() {
|
|
9
|
+
}
|
|
10
|
+
GroupFlowItem.prototype.exec = function (fieldToGroup, sumFields, detailed, modelRoot) {
|
|
11
|
+
var _a;
|
|
12
|
+
if (!fieldToGroup)
|
|
13
|
+
return [];
|
|
14
|
+
var sumField = sumFields ? sumFields[0] : undefined;
|
|
15
|
+
var groupsClauses = [];
|
|
16
|
+
var first = fieldToGroup.split('.')[0];
|
|
17
|
+
var typeOfFieldToGroup = GetTypeOfFieldInModelFlowItem_1.default.exec(first, modelRoot);
|
|
18
|
+
if (typeOfFieldToGroup === "Array") {
|
|
19
|
+
groupsClauses.push({
|
|
20
|
+
$unwind: {
|
|
21
|
+
path: "$".concat(first),
|
|
22
|
+
preserveNullAndEmptyArrays: true,
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
var groupField = "$".concat(fieldToGroup);
|
|
27
|
+
// Check if the field ends with "Date" and add day precision
|
|
28
|
+
if (fieldToGroup.endsWith("Date") || fieldToGroup.endsWith("DateTime")) {
|
|
29
|
+
groupField = {
|
|
30
|
+
$dateToString: {
|
|
31
|
+
format: "%Y-%m-%d",
|
|
32
|
+
date: "$".concat(fieldToGroup)
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
var groupClause = {
|
|
37
|
+
$group: {
|
|
38
|
+
_id: groupField,
|
|
39
|
+
count: { $sum: 1 }
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
if (detailed) {
|
|
43
|
+
groupClause.$group.docs = { $push: "$$ROOT" };
|
|
44
|
+
}
|
|
45
|
+
if (sumField) {
|
|
46
|
+
var tempFieldName = sumField.replace(/\./g, '_');
|
|
47
|
+
var firstSumField = sumField.split('.')[0];
|
|
48
|
+
var typeOfFieldToSum = GetTypeOfFieldInModelFlowItem_1.default.exec(firstSumField, modelRoot);
|
|
49
|
+
if (typeOfFieldToSum === "Array") {
|
|
50
|
+
groupClause.$group[tempFieldName] = { $sum: { $sum: { '$ifNull': ["$".concat(sumField), 0] } } };
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
groupClause.$group[tempFieldName] = { $sum: { $ifNull: ["$".concat(sumField), 0] } };
|
|
54
|
+
}
|
|
55
|
+
groupsClauses.push(groupClause);
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
groupsClauses.push(groupClause);
|
|
59
|
+
}
|
|
60
|
+
var projectClause = {
|
|
61
|
+
$project: (_a = {
|
|
62
|
+
_id: 1
|
|
63
|
+
},
|
|
64
|
+
_a[fieldToGroup] = "$_id",
|
|
65
|
+
_a.count = 1,
|
|
66
|
+
_a)
|
|
67
|
+
};
|
|
68
|
+
if (detailed) {
|
|
69
|
+
projectClause.$project.docs = 1;
|
|
70
|
+
}
|
|
71
|
+
if (sumField) {
|
|
72
|
+
var tempFieldName = sumField.replace(/\./g, '_');
|
|
73
|
+
projectClause.$project["".concat(sumField, "Total")] = "$".concat(tempFieldName);
|
|
74
|
+
}
|
|
75
|
+
groupsClauses.push(projectClause);
|
|
76
|
+
return groupsClauses;
|
|
77
|
+
};
|
|
78
|
+
return GroupFlowItem;
|
|
79
|
+
}());
|
|
80
|
+
exports.default = new GroupFlowItem;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
var IsValidObjectIdFlowItem = /** @class */ (function () {
|
|
4
|
+
function IsValidObjectIdFlowItem() {
|
|
5
|
+
}
|
|
6
|
+
IsValidObjectIdFlowItem.prototype.exec = function (value) {
|
|
7
|
+
var objectIdPattern = /^[0-9a-fA-F]{24}$/;
|
|
8
|
+
return objectIdPattern.test(value);
|
|
9
|
+
};
|
|
10
|
+
return IsValidObjectIdFlowItem;
|
|
11
|
+
}());
|
|
12
|
+
exports.default = new IsValidObjectIdFlowItem;
|
|
@@ -0,0 +1,132 @@
|
|
|
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 __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
14
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
15
|
+
if (ar || !(i in from)) {
|
|
16
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
17
|
+
ar[i] = from[i];
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
21
|
+
};
|
|
22
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
23
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
var mongoose_1 = require("mongoose");
|
|
27
|
+
var ExtractFieldsOfTypeFlowItem_1 = __importDefault(require("./ExtractFieldsOfTypeFlowItem"));
|
|
28
|
+
var IsValidObjectIdFlowItem_1 = __importDefault(require("./IsValidObjectIdFlowItem"));
|
|
29
|
+
var BuildConditionFlowItem_1 = __importDefault(require("./BuildConditionFlowItem"));
|
|
30
|
+
var BuildRegexFlowItem_1 = __importDefault(require("./BuildRegexFlowItem"));
|
|
31
|
+
var NormalizeValueFlowItem_1 = __importDefault(require("./NormalizeValueFlowItem"));
|
|
32
|
+
var MatchFlowItem = /** @class */ (function () {
|
|
33
|
+
function MatchFlowItem() {
|
|
34
|
+
}
|
|
35
|
+
MatchFlowItem.prototype.exec = function (filters, model) {
|
|
36
|
+
// Recuperar todos os campos do tipo String
|
|
37
|
+
var camposString = !filters.searchTextFields ? ExtractFieldsOfTypeFlowItem_1.default.exec(model.schema, 'String') : filters.searchTextFields;
|
|
38
|
+
var andQueriesParam = {};
|
|
39
|
+
Object.entries(filters).forEach(function (_a) {
|
|
40
|
+
var key = _a[0], value = _a[1];
|
|
41
|
+
if (value !== undefined || value === null) {
|
|
42
|
+
if ([
|
|
43
|
+
'mainFilters',
|
|
44
|
+
'or',
|
|
45
|
+
'filters',
|
|
46
|
+
'withTotalizers',
|
|
47
|
+
'onlyMetadata',
|
|
48
|
+
'projection',
|
|
49
|
+
'pageable',
|
|
50
|
+
'orderSense',
|
|
51
|
+
'orderBy',
|
|
52
|
+
'groupBy',
|
|
53
|
+
'groupDetailed',
|
|
54
|
+
'sumFields',
|
|
55
|
+
'countFields',
|
|
56
|
+
'properties',
|
|
57
|
+
'populate',
|
|
58
|
+
'page',
|
|
59
|
+
'limit',
|
|
60
|
+
'model',
|
|
61
|
+
'select',
|
|
62
|
+
'searchText',
|
|
63
|
+
'searchTextFields',
|
|
64
|
+
'sort',
|
|
65
|
+
'isPageable',
|
|
66
|
+
'searchPageable'
|
|
67
|
+
].includes(key)) {
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
if (typeof value === 'string' && IsValidObjectIdFlowItem_1.default.exec(value) && key !== 'owner' && key !== 'identifier') {
|
|
71
|
+
value = new mongoose_1.Types.ObjectId(value);
|
|
72
|
+
}
|
|
73
|
+
if (value === 'true') {
|
|
74
|
+
value = true;
|
|
75
|
+
}
|
|
76
|
+
if (value === 'false') {
|
|
77
|
+
value = false;
|
|
78
|
+
}
|
|
79
|
+
var condition = BuildConditionFlowItem_1.default.exec(key, value, model);
|
|
80
|
+
andQueriesParam = __assign(__assign({}, andQueriesParam), condition);
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
if (filters.searchText && camposString) {
|
|
84
|
+
var regex = BuildRegexFlowItem_1.default.exec(filters.searchText);
|
|
85
|
+
var conditions = { $or: [] };
|
|
86
|
+
for (var _i = 0, camposString_1 = camposString; _i < camposString_1.length; _i++) {
|
|
87
|
+
var fieldString = camposString_1[_i];
|
|
88
|
+
var conditionAux = {};
|
|
89
|
+
conditionAux[fieldString] = { $regex: regex };
|
|
90
|
+
conditions.$or.push(conditionAux);
|
|
91
|
+
}
|
|
92
|
+
andQueriesParam = __assign(__assign({}, andQueriesParam), conditions);
|
|
93
|
+
}
|
|
94
|
+
var orPayload = [];
|
|
95
|
+
if (filters === null || filters === void 0 ? void 0 : filters.or) {
|
|
96
|
+
var _loop_1 = function (clauseOr) {
|
|
97
|
+
var and = void 0;
|
|
98
|
+
var conditionsAnd;
|
|
99
|
+
Object.entries(clauseOr).forEach(function (_a) {
|
|
100
|
+
var key = _a[0], value = _a[1];
|
|
101
|
+
if (!value)
|
|
102
|
+
return;
|
|
103
|
+
value = NormalizeValueFlowItem_1.default.exec(key, value);
|
|
104
|
+
var condition = BuildConditionFlowItem_1.default.exec(key, value, model);
|
|
105
|
+
if (!conditionsAnd) {
|
|
106
|
+
conditionsAnd = {};
|
|
107
|
+
}
|
|
108
|
+
conditionsAnd = __assign(__assign({}, conditionsAnd), condition);
|
|
109
|
+
});
|
|
110
|
+
if (conditionsAnd) {
|
|
111
|
+
and = { $and: [conditionsAnd] };
|
|
112
|
+
}
|
|
113
|
+
if (and) {
|
|
114
|
+
orPayload.push(and);
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
for (var _a = 0, _b = filters === null || filters === void 0 ? void 0 : filters.or; _a < _b.length; _a++) {
|
|
118
|
+
var clauseOr = _b[_a];
|
|
119
|
+
_loop_1(clauseOr);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
var filtersFinal = __assign({}, andQueriesParam);
|
|
123
|
+
if (orPayload.length > 0) {
|
|
124
|
+
if (!filtersFinal.$or)
|
|
125
|
+
filtersFinal.$or = [];
|
|
126
|
+
filtersFinal.$or = __spreadArray(__spreadArray([], filtersFinal.$or, true), orPayload, true);
|
|
127
|
+
}
|
|
128
|
+
return filtersFinal;
|
|
129
|
+
};
|
|
130
|
+
return MatchFlowItem;
|
|
131
|
+
}());
|
|
132
|
+
exports.default = new MatchFlowItem;
|