c2-mongoose 2.1.370 → 2.1.372
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.
|
@@ -0,0 +1,120 @@
|
|
|
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
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
var GetFieldDefinitionFlowItem_1 = __importDefault(require("./GetFieldDefinitionFlowItem"));
|
|
16
|
+
var LookupArrayFlowItem = /** @class */ (function () {
|
|
17
|
+
function LookupArrayFlowItem() {
|
|
18
|
+
}
|
|
19
|
+
LookupArrayFlowItem.prototype.execute = function (previousPath, remainingPath, populateClauses, model) {
|
|
20
|
+
var _a, _b;
|
|
21
|
+
var populateClauseLookup = [];
|
|
22
|
+
try {
|
|
23
|
+
populateClauseLookup.push({
|
|
24
|
+
$unwind: "$".concat(previousPath)
|
|
25
|
+
});
|
|
26
|
+
//init code here
|
|
27
|
+
var currentField_1 = remainingPath[0], othersFields = remainingPath.slice(1);
|
|
28
|
+
if (!currentField_1) {
|
|
29
|
+
return [];
|
|
30
|
+
}
|
|
31
|
+
var currentFieldDefinition = GetFieldDefinitionFlowItem_1.default.execute(model, "".concat(previousPath, ".").concat(currentField_1));
|
|
32
|
+
if (!currentFieldDefinition || !currentFieldDefinition.options || !currentFieldDefinition.options.ref) {
|
|
33
|
+
throw new Error("[1]No reference found for populate field: ".concat([previousPath, currentField_1]));
|
|
34
|
+
}
|
|
35
|
+
var currentType = currentFieldDefinition.instance;
|
|
36
|
+
if (currentType === 'Array' && !(othersFields === null || othersFields === void 0 ? void 0 : othersFields.length))
|
|
37
|
+
return [];
|
|
38
|
+
var modelName = currentFieldDefinition.options.ref;
|
|
39
|
+
var currentModel = global.connectionMongoose.model(modelName);
|
|
40
|
+
var collectionName = currentModel.collection.name;
|
|
41
|
+
if (!populateClauses.some(function (populateClause) { var _a; return ((_a = populateClause === null || populateClause === void 0 ? void 0 : populateClause.$lookup) === null || _a === void 0 ? void 0 : _a.localField) === "".concat(previousPath, ".").concat(currentField_1); })) {
|
|
42
|
+
populateClauseLookup.push({
|
|
43
|
+
$lookup: {
|
|
44
|
+
from: collectionName,
|
|
45
|
+
localField: "".concat(previousPath, ".").concat(currentField_1),
|
|
46
|
+
foreignField: "_id",
|
|
47
|
+
as: "".concat(previousPath, ".").concat(currentField_1)
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
populateClauseLookup.push({
|
|
51
|
+
$unwind: {
|
|
52
|
+
path: "$".concat(previousPath, ".").concat(currentField_1),
|
|
53
|
+
preserveNullAndEmptyArrays: true
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
//end code here
|
|
58
|
+
populateClauseLookup.push({
|
|
59
|
+
$group: (_a = {
|
|
60
|
+
_id: "$_id",
|
|
61
|
+
doc: { $first: "$$ROOT" }
|
|
62
|
+
},
|
|
63
|
+
_a["".concat(previousPath)] = { $push: "$".concat(previousPath) } // agrupa apenas o array
|
|
64
|
+
,
|
|
65
|
+
_a)
|
|
66
|
+
});
|
|
67
|
+
populateClauseLookup.push({
|
|
68
|
+
$replaceRoot: {
|
|
69
|
+
newRoot: {
|
|
70
|
+
$mergeObjects: ["$doc", (_b = {}, _b["".concat(previousPath)] = "$".concat(previousPath), _b)]
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
var _loop_1 = function () {
|
|
75
|
+
var _c;
|
|
76
|
+
var nextField = othersFields[0], rest = othersFields.slice(1);
|
|
77
|
+
var nextDefinition = GetFieldDefinitionFlowItem_1.default.execute(currentModel, nextField);
|
|
78
|
+
if (!nextDefinition || !nextDefinition.options || !nextDefinition.options.ref) {
|
|
79
|
+
throw new Error("[1]No reference found for populate field: ".concat([previousPath, currentField_1]));
|
|
80
|
+
}
|
|
81
|
+
var nextType = nextDefinition.instance;
|
|
82
|
+
if (nextType === 'Array' && !(othersFields === null || othersFields === void 0 ? void 0 : othersFields.length))
|
|
83
|
+
return "continue";
|
|
84
|
+
var nextModelName = nextDefinition.options.ref;
|
|
85
|
+
var nextRefModel = global.connectionMongoose.model(nextModelName);
|
|
86
|
+
var collectionName_1 = nextRefModel.collection.name;
|
|
87
|
+
if (!__spreadArray(__spreadArray([], populateClauses, true), populateClauseLookup, true).some(function (populateClause) { var _a; return ((_a = populateClause === null || populateClause === void 0 ? void 0 : populateClause.$lookup) === null || _a === void 0 ? void 0 : _a.localField) === nextField; })) {
|
|
88
|
+
var populateClauseLookup_1 = [
|
|
89
|
+
{
|
|
90
|
+
$lookup: {
|
|
91
|
+
from: collectionName_1,
|
|
92
|
+
localField: nextField,
|
|
93
|
+
foreignField: "_id",
|
|
94
|
+
as: nextField
|
|
95
|
+
}
|
|
96
|
+
},
|
|
97
|
+
];
|
|
98
|
+
if (nextType !== 'Array') {
|
|
99
|
+
populateClauseLookup_1.push({
|
|
100
|
+
$addFields: (_c = {},
|
|
101
|
+
_c["".concat(nextField)] = { $ifNull: [{ $arrayElemAt: ["$".concat(nextField), 0] }, undefined] },
|
|
102
|
+
_c)
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
while (othersFields === null || othersFields === void 0 ? void 0 : othersFields.length) {
|
|
108
|
+
_loop_1();
|
|
109
|
+
}
|
|
110
|
+
populateClauseLookup = __spreadArray(__spreadArray([], populateClauseLookup, true), this.execute("".concat(previousPath, ".").concat(currentField_1), othersFields, __spreadArray(__spreadArray([], populateClauses, true), populateClauseLookup, true), currentModel), true);
|
|
111
|
+
return __spreadArray([], populateClauseLookup, true);
|
|
112
|
+
}
|
|
113
|
+
catch (error) {
|
|
114
|
+
console.error(error.message);
|
|
115
|
+
return [];
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
return LookupArrayFlowItem;
|
|
119
|
+
}());
|
|
120
|
+
exports.default = new LookupArrayFlowItem;
|
package/package.json
CHANGED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import mongoose from "mongoose";
|
|
2
|
+
import GetFieldDefinitionFlowItem from "./GetFieldDefinitionFlowItem";
|
|
3
|
+
|
|
4
|
+
class LookupArrayFlowItem {
|
|
5
|
+
execute(previousPath: string, remainingPath: string[], populateClauses: any[], model: mongoose.Model<any>): any[] {
|
|
6
|
+
|
|
7
|
+
let populateClauseLookup = []
|
|
8
|
+
|
|
9
|
+
try {
|
|
10
|
+
populateClauseLookup.push({
|
|
11
|
+
$unwind: `$${previousPath}`
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
//init code here
|
|
16
|
+
let [currentField, ...othersFields] = remainingPath
|
|
17
|
+
|
|
18
|
+
if (!currentField) {
|
|
19
|
+
return []
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
let currentFieldDefinition = GetFieldDefinitionFlowItem.execute(model, `${previousPath}.${currentField}`);
|
|
23
|
+
if (!currentFieldDefinition || !currentFieldDefinition.options || !currentFieldDefinition.options.ref) {
|
|
24
|
+
throw new Error(`[1]No reference found for populate field: ${[previousPath, currentField]}`);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
let currentType = currentFieldDefinition.instance
|
|
29
|
+
if (currentType === 'Array' && !othersFields?.length) return []
|
|
30
|
+
|
|
31
|
+
let modelName = currentFieldDefinition.options.ref;
|
|
32
|
+
let currentModel = (global as any).connectionMongoose.model(modelName);
|
|
33
|
+
let collectionName = currentModel.collection.name;
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
if (!populateClauses.some((populateClause: any) => populateClause?.$lookup?.localField === `${previousPath}.${currentField}`)) {
|
|
37
|
+
populateClauseLookup.push({
|
|
38
|
+
$lookup: {
|
|
39
|
+
from: collectionName, // The collection where user data is stored
|
|
40
|
+
localField: `${previousPath}.${currentField}`,
|
|
41
|
+
foreignField: "_id",
|
|
42
|
+
as: `${previousPath}.${currentField}`
|
|
43
|
+
}
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
populateClauseLookup.push({
|
|
47
|
+
$unwind: {
|
|
48
|
+
path: `$${previousPath}.${currentField}`,
|
|
49
|
+
preserveNullAndEmptyArrays: true
|
|
50
|
+
}
|
|
51
|
+
})
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
//end code here
|
|
55
|
+
populateClauseLookup.push({
|
|
56
|
+
$group: {
|
|
57
|
+
_id: "$_id",
|
|
58
|
+
doc: { $first: "$$ROOT" }, // pega o documento inteiro
|
|
59
|
+
[`${previousPath}`]: { $push: `$${previousPath}` } // agrupa apenas o array
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
populateClauseLookup.push({
|
|
63
|
+
$replaceRoot: {
|
|
64
|
+
newRoot: {
|
|
65
|
+
$mergeObjects: ["$doc", { [`${previousPath}`]: `$${previousPath}` }]
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
while (othersFields?.length) {
|
|
72
|
+
|
|
73
|
+
let [nextField, ...rest] = othersFields
|
|
74
|
+
let nextDefinition = GetFieldDefinitionFlowItem.execute(currentModel, nextField);
|
|
75
|
+
|
|
76
|
+
if (!nextDefinition || !nextDefinition.options || !nextDefinition.options.ref) {
|
|
77
|
+
throw new Error(`[1]No reference found for populate field: ${[previousPath, currentField]}`);
|
|
78
|
+
}
|
|
79
|
+
let nextType = nextDefinition.instance
|
|
80
|
+
if (nextType === 'Array' && !othersFields?.length) continue;
|
|
81
|
+
|
|
82
|
+
let nextModelName = nextDefinition.options.ref;
|
|
83
|
+
let nextRefModel = (global as any).connectionMongoose.model(nextModelName);
|
|
84
|
+
let collectionName = nextRefModel.collection.name;
|
|
85
|
+
|
|
86
|
+
if (![...populateClauses, ...populateClauseLookup].some((populateClause: any) => populateClause?.$lookup?.localField === nextField)) {
|
|
87
|
+
const populateClauseLookup: any[] = [
|
|
88
|
+
{
|
|
89
|
+
$lookup: {
|
|
90
|
+
from: collectionName, // The collection where user data is stored
|
|
91
|
+
localField: nextField,
|
|
92
|
+
foreignField: "_id",
|
|
93
|
+
as: nextField
|
|
94
|
+
}
|
|
95
|
+
},
|
|
96
|
+
]
|
|
97
|
+
if (nextType !== 'Array') {
|
|
98
|
+
populateClauseLookup.push(
|
|
99
|
+
{
|
|
100
|
+
$addFields: {
|
|
101
|
+
[`${nextField}`]: { $ifNull: [{ $arrayElemAt: [`$${nextField}`, 0] }, undefined] },
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
)
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
populateClauseLookup = [...populateClauseLookup, ...this.execute(`${previousPath}.${currentField}`, othersFields, [...populateClauses, ...populateClauseLookup], currentModel)]
|
|
110
|
+
|
|
111
|
+
return [...populateClauseLookup]
|
|
112
|
+
} catch (error: any) {
|
|
113
|
+
console.error(error.message)
|
|
114
|
+
return []
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export default new LookupArrayFlowItem
|