c2-mongoose 2.1.208 → 2.1.210
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/SearchFlow.d.ts +1 -1
- package/dist/flow/SearchFlow.js +3 -2
- package/dist/flow/item/BuildSelectSingleFlowItem.d.ts +7 -0
- package/dist/flow/item/BuildSelectSingleFlowItem.js +47 -0
- package/package.json +1 -1
- package/src/flow/SearchFlow.ts +14 -13
- package/src/flow/item/BuildSelectSingleFlowItem.ts +48 -0
package/dist/flow/SearchFlow.js
CHANGED
|
@@ -54,6 +54,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
54
54
|
var mongoose_1 = require("mongoose");
|
|
55
55
|
var Utils_1 = require("../utils/Utils");
|
|
56
56
|
var moment_timezone_1 = __importDefault(require("moment-timezone"));
|
|
57
|
+
var BuildSelectSingleFlowItem_1 = __importDefault(require("./item/BuildSelectSingleFlowItem"));
|
|
57
58
|
var SearchFlow = /** @class */ (function () {
|
|
58
59
|
function SearchFlow(params) {
|
|
59
60
|
// Incluir as propriedades adicionais de 'params' como propriedades da classe
|
|
@@ -179,7 +180,7 @@ var SearchFlow = /** @class */ (function () {
|
|
|
179
180
|
}
|
|
180
181
|
stagesItems.push({ $match: this.filters });
|
|
181
182
|
if ((0, Utils_1.isNotEmpty)(this.projection)) {
|
|
182
|
-
stagesItems.push({ $project: this.
|
|
183
|
+
stagesItems.push({ $project: BuildSelectSingleFlowItem_1.default.build(model, this.select) });
|
|
183
184
|
}
|
|
184
185
|
stagesItems.push({ $sort: this.sort });
|
|
185
186
|
stagesMetadata = [];
|
|
@@ -242,7 +243,7 @@ var SearchFlow = /** @class */ (function () {
|
|
|
242
243
|
}
|
|
243
244
|
stagesItems.push({ $match: this.filters });
|
|
244
245
|
if ((0, Utils_1.isNotEmpty)(this.projection)) {
|
|
245
|
-
stagesItems.push({ $project: this.
|
|
246
|
+
stagesItems.push({ $project: BuildSelectSingleFlowItem_1.default.build(model, this.select) });
|
|
246
247
|
}
|
|
247
248
|
stagesItems.push({ $sort: this.sort });
|
|
248
249
|
stagesItems.push({ $skip: ((this.page - 1) * this.limit) || 0 });
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import mongoose from "mongoose";
|
|
2
|
+
declare class BuildSelectSingleFlowItem {
|
|
3
|
+
build(model: mongoose.Model<any>, selects: string[]): any;
|
|
4
|
+
buildProjectionNested(model: mongoose.Model<any>, projectCurrent: any, field: string, include: boolean): any;
|
|
5
|
+
}
|
|
6
|
+
declare const _default: BuildSelectSingleFlowItem;
|
|
7
|
+
export default _default;
|
|
@@ -0,0 +1,47 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
+
var Utils_1 = require("../../utils/Utils");
|
|
15
|
+
var BuildSelectSingleFlowItem = /** @class */ (function () {
|
|
16
|
+
function BuildSelectSingleFlowItem() {
|
|
17
|
+
}
|
|
18
|
+
BuildSelectSingleFlowItem.prototype.build = function (model, selects) {
|
|
19
|
+
var projection = {};
|
|
20
|
+
for (var _i = 0, selects_1 = selects; _i < selects_1.length; _i++) {
|
|
21
|
+
var field = selects_1[_i];
|
|
22
|
+
var include = field.startsWith("-") ? false : true;
|
|
23
|
+
field = field.replace("-", "");
|
|
24
|
+
projection = __assign(__assign({}, projection), this.buildProjectionNested(model, projection, field, include));
|
|
25
|
+
}
|
|
26
|
+
return projection;
|
|
27
|
+
};
|
|
28
|
+
BuildSelectSingleFlowItem.prototype.buildProjectionNested = function (model, projectCurrent, field, include) {
|
|
29
|
+
var _a = field.split('.'), first = _a[0], rest = _a.slice(1);
|
|
30
|
+
if ((0, Utils_1.isEmpty)(first) && (0, Utils_1.isEmpty)(rest)) {
|
|
31
|
+
var campoInfo = model.schema.path(first);
|
|
32
|
+
// Verifique se o tipo do campo é mongoose.Types.ObjectId
|
|
33
|
+
if (campoInfo.instance === "ObjectID") {
|
|
34
|
+
console.log('O campo é um Types.ObjectId');
|
|
35
|
+
return undefined;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
if (rest.length === 0) {
|
|
39
|
+
projectCurrent[first] = include ? 1 : 0;
|
|
40
|
+
return projectCurrent;
|
|
41
|
+
}
|
|
42
|
+
projectCurrent[first] = __assign(__assign({}, projectCurrent[first]), this.buildProjectionNested(model, {}, rest.join("."), include));
|
|
43
|
+
return projectCurrent;
|
|
44
|
+
};
|
|
45
|
+
return BuildSelectSingleFlowItem;
|
|
46
|
+
}());
|
|
47
|
+
exports.default = new BuildSelectSingleFlowItem;
|
package/package.json
CHANGED
package/src/flow/SearchFlow.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
// import moment from "moment"
|
|
2
|
-
import mongoose, { ClientSession, PopulateOption, PopulateOptions, Types } from "mongoose"
|
|
2
|
+
import mongoose, { ClientSession, PopulateOption, PopulateOptions, Types, model } from "mongoose"
|
|
3
3
|
import { SearchOptions, SearchResponse } from "../types/SearchResponse"
|
|
4
4
|
import { isEmpty, isNotEmpty } from "../utils/Utils"
|
|
5
5
|
import moment from "moment-timezone"
|
|
6
|
+
import BuildSelectSingleFlowItem from "./item/BuildSelectSingleFlowItem"
|
|
6
7
|
|
|
7
8
|
interface IPopulate {
|
|
8
9
|
path: string,
|
|
@@ -16,7 +17,7 @@ abstract class SearchFlow {
|
|
|
16
17
|
orderBy: string
|
|
17
18
|
page: number
|
|
18
19
|
limit: number
|
|
19
|
-
select: string
|
|
20
|
+
select: string[]
|
|
20
21
|
populate: any
|
|
21
22
|
filters: any
|
|
22
23
|
sort: any
|
|
@@ -168,7 +169,7 @@ abstract class SearchFlow {
|
|
|
168
169
|
stagesItems.push({ $match: this.filters })
|
|
169
170
|
|
|
170
171
|
if (isNotEmpty(this.projection)) {
|
|
171
|
-
stagesItems.push({ $project: this.
|
|
172
|
+
stagesItems.push({ $project: BuildSelectSingleFlowItem.build(model, this.select) })
|
|
172
173
|
}
|
|
173
174
|
|
|
174
175
|
stagesItems.push({ $sort: this.sort })
|
|
@@ -203,7 +204,7 @@ abstract class SearchFlow {
|
|
|
203
204
|
|
|
204
205
|
if (isNotEmpty(this.populate) && Array.isArray(this.populate)) {
|
|
205
206
|
for (var populate of this.populate) {
|
|
206
|
-
result[0].items = await model.populate(result[0].items, { populate, select: this.projection[populate.path] } as PopulateOptions
|
|
207
|
+
result[0].items = await model.populate(result[0].items, { populate, select: this.projection[populate.path] } as PopulateOptions)
|
|
207
208
|
}
|
|
208
209
|
}
|
|
209
210
|
|
|
@@ -225,7 +226,7 @@ abstract class SearchFlow {
|
|
|
225
226
|
stagesItems.push({ $match: this.filters })
|
|
226
227
|
|
|
227
228
|
if (isNotEmpty(this.projection)) {
|
|
228
|
-
stagesItems.push({ $project: this.
|
|
229
|
+
stagesItems.push({ $project: BuildSelectSingleFlowItem.build(model, this.select) })
|
|
229
230
|
}
|
|
230
231
|
|
|
231
232
|
stagesItems.push({ $sort: this.sort })
|
|
@@ -244,10 +245,10 @@ abstract class SearchFlow {
|
|
|
244
245
|
stagesPaging.push({ $match: this.filters })
|
|
245
246
|
stagesPaging.push({ $count: "total" })
|
|
246
247
|
stagesPaging.push(
|
|
247
|
-
{
|
|
248
|
-
$addFields: {
|
|
249
|
-
page: this.page || 1,
|
|
250
|
-
limit: this.limit,
|
|
248
|
+
{
|
|
249
|
+
$addFields: {
|
|
250
|
+
page: this.page || 1,
|
|
251
|
+
limit: this.limit,
|
|
251
252
|
totalPages: {
|
|
252
253
|
$cond: {
|
|
253
254
|
if: { $eq: ['$total', 0] },
|
|
@@ -255,9 +256,9 @@ abstract class SearchFlow {
|
|
|
255
256
|
else: { $ceil: { $divide: ['$total', this.limit] } }
|
|
256
257
|
}
|
|
257
258
|
},
|
|
258
|
-
startIndex: { $subtract: [
|
|
259
|
+
startIndex: { $subtract: [{ $multiply: [this.page, this.limit] }, this.limit - 1] },
|
|
259
260
|
endIndex: { $multiply: [this.page, this.limit] }
|
|
260
|
-
}
|
|
261
|
+
}
|
|
261
262
|
})
|
|
262
263
|
|
|
263
264
|
let stagesMetadata: any[] = []
|
|
@@ -293,7 +294,7 @@ abstract class SearchFlow {
|
|
|
293
294
|
|
|
294
295
|
if (isNotEmpty(this.populate) && Array.isArray(this.populate)) {
|
|
295
296
|
for (var populate of this.populate) {
|
|
296
|
-
result[0].items = await model.populate(result[0].items, { populate, select: this.projection[populate.path] } as PopulateOptions
|
|
297
|
+
result[0].items = await model.populate(result[0].items, { populate, select: this.projection[populate.path] } as PopulateOptions)
|
|
297
298
|
}
|
|
298
299
|
}
|
|
299
300
|
|
|
@@ -487,7 +488,7 @@ abstract class SearchFlow {
|
|
|
487
488
|
condition[fieldName] = { $exists: value as boolean }
|
|
488
489
|
filters.$and.push(condition)
|
|
489
490
|
}
|
|
490
|
-
|
|
491
|
+
|
|
491
492
|
else {
|
|
492
493
|
if (!Array.isArray(value)) {
|
|
493
494
|
condition[key] = value
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import mongoose from "mongoose"
|
|
2
|
+
import { isEmpty } from "../../utils/Utils"
|
|
3
|
+
|
|
4
|
+
class BuildSelectSingleFlowItem {
|
|
5
|
+
|
|
6
|
+
build(model: mongoose.Model<any>, selects: string[]) {
|
|
7
|
+
|
|
8
|
+
let projection: any = {}
|
|
9
|
+
for (var field of selects) {
|
|
10
|
+
let include = field.startsWith("-") ? false : true
|
|
11
|
+
field = field.replace("-", "")
|
|
12
|
+
projection = {
|
|
13
|
+
...projection,
|
|
14
|
+
...this.buildProjectionNested(model, projection, field, include)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
}
|
|
18
|
+
return projection;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
buildProjectionNested(model: mongoose.Model<any>, projectCurrent: any, field: string, include: boolean) {
|
|
22
|
+
let [first, ...rest] = field.split('.')
|
|
23
|
+
|
|
24
|
+
if (isEmpty(first) && isEmpty(rest)){
|
|
25
|
+
|
|
26
|
+
const campoInfo = model.schema.path(first);
|
|
27
|
+
|
|
28
|
+
// Verifique se o tipo do campo é mongoose.Types.ObjectId
|
|
29
|
+
if (campoInfo.instance === "ObjectID") {
|
|
30
|
+
console.log('O campo é um Types.ObjectId');
|
|
31
|
+
return undefined
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (rest.length === 0) {
|
|
36
|
+
projectCurrent[first] = include ? 1 : 0
|
|
37
|
+
return projectCurrent
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
projectCurrent[first] = {
|
|
41
|
+
...projectCurrent[first],
|
|
42
|
+
...this.buildProjectionNested(model, {}, rest.join("."), include)
|
|
43
|
+
}
|
|
44
|
+
return projectCurrent
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export default new BuildSelectSingleFlowItem
|