c2-mongoose 2.1.365 → 2.1.367
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.
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import mongoose from "mongoose";
|
|
2
2
|
declare class LookupArrayFlowItem {
|
|
3
|
-
execute(previousPath: string, remainingPath: string[], populateClauses: any[], model: mongoose.Model<any>):
|
|
3
|
+
execute(previousPath: string, remainingPath: string[], populateClauses: any[], model: mongoose.Model<any>): never[] | undefined;
|
|
4
4
|
}
|
|
5
5
|
declare const _default: LookupArrayFlowItem;
|
|
6
6
|
export default _default;
|
|
@@ -19,56 +19,63 @@ var LookupArrayFlowItem = /** @class */ (function () {
|
|
|
19
19
|
LookupArrayFlowItem.prototype.execute = function (previousPath, remainingPath, populateClauses, model) {
|
|
20
20
|
var _a, _b;
|
|
21
21
|
var populateClauseLookup = [];
|
|
22
|
-
|
|
23
|
-
$unwind: "$".concat(previousPath)
|
|
24
|
-
});
|
|
25
|
-
//init code here
|
|
26
|
-
var currentField = remainingPath[0], others = remainingPath.slice(1);
|
|
27
|
-
var currentFieldDefinition = GetFieldDefinitionFlowItem_1.default.execute(model, currentField);
|
|
28
|
-
if (!currentFieldDefinition || !currentFieldDefinition.options || !currentFieldDefinition.options.ref) {
|
|
29
|
-
throw new Error("[1]No reference found for populate field: ".concat([previousPath, currentField]));
|
|
30
|
-
}
|
|
31
|
-
var typeFirst = currentFieldDefinition.instance;
|
|
32
|
-
if (typeFirst === 'Array' && !(others === null || others === void 0 ? void 0 : others.length))
|
|
33
|
-
return [];
|
|
34
|
-
var modelName = currentFieldDefinition.options.ref;
|
|
35
|
-
var refModel = global.connectionMongoose.model(modelName);
|
|
36
|
-
var collectionName = refModel.collection.name;
|
|
37
|
-
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); })) {
|
|
22
|
+
try {
|
|
38
23
|
populateClauseLookup.push({
|
|
39
|
-
$
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
24
|
+
$unwind: "$".concat(previousPath)
|
|
25
|
+
});
|
|
26
|
+
//init code here
|
|
27
|
+
var currentField_1 = remainingPath[0], others = 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 typeFirst = currentFieldDefinition.instance;
|
|
36
|
+
if (typeFirst === 'Array' && !(others === null || others === void 0 ? void 0 : others.length))
|
|
37
|
+
return [];
|
|
38
|
+
var modelName = currentFieldDefinition.options.ref;
|
|
39
|
+
var refModel = global.connectionMongoose.model(modelName);
|
|
40
|
+
var collectionName = refModel.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)
|
|
45
66
|
});
|
|
46
67
|
populateClauseLookup.push({
|
|
47
|
-
$
|
|
48
|
-
|
|
49
|
-
|
|
68
|
+
$replaceRoot: {
|
|
69
|
+
newRoot: {
|
|
70
|
+
$mergeObjects: ["$doc", (_b = {}, _b["".concat(previousPath)] = { $push: "$".concat(previousPath) }, _b)]
|
|
71
|
+
}
|
|
50
72
|
}
|
|
51
73
|
});
|
|
52
74
|
populateClauses = __spreadArray(__spreadArray([], populateClauses, true), populateClauseLookup, true);
|
|
53
75
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
_id: "$_id",
|
|
58
|
-
doc: { $first: "$$ROOT" }
|
|
59
|
-
},
|
|
60
|
-
_a["".concat(previousPath)] = { $push: "$".concat(previousPath) } // agrupa apenas o array
|
|
61
|
-
,
|
|
62
|
-
_a)
|
|
63
|
-
});
|
|
64
|
-
populateClauseLookup.push({
|
|
65
|
-
$replaceRoot: {
|
|
66
|
-
newRoot: {
|
|
67
|
-
$mergeObjects: ["$doc", (_b = {}, _b["".concat(previousPath)] = { $push: "$".concat(previousPath) }, _b)]
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
});
|
|
71
|
-
return populateClauses;
|
|
76
|
+
catch (error) {
|
|
77
|
+
console.error(error.message);
|
|
78
|
+
}
|
|
72
79
|
};
|
|
73
80
|
return LookupArrayFlowItem;
|
|
74
81
|
}());
|
package/package.json
CHANGED
|
@@ -2,68 +2,75 @@ import mongoose from "mongoose";
|
|
|
2
2
|
import GetFieldDefinitionFlowItem from "./GetFieldDefinitionFlowItem";
|
|
3
3
|
|
|
4
4
|
class LookupArrayFlowItem {
|
|
5
|
-
execute(previousPath: string, remainingPath: string[], populateClauses: any[], model: mongoose.Model<any>)
|
|
5
|
+
execute(previousPath: string, remainingPath: string[], populateClauses: any[], model: mongoose.Model<any>) {
|
|
6
6
|
|
|
7
7
|
const populateClauseLookup = []
|
|
8
|
-
populateClauseLookup.push({
|
|
9
|
-
$unwind: `$${previousPath}`
|
|
10
|
-
});
|
|
11
8
|
|
|
9
|
+
try {
|
|
10
|
+
populateClauseLookup.push({
|
|
11
|
+
$unwind: `$${previousPath}`
|
|
12
|
+
});
|
|
12
13
|
|
|
13
|
-
//init code here
|
|
14
|
-
let [currentField, ...others] = remainingPath
|
|
15
14
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
15
|
+
//init code here
|
|
16
|
+
let [currentField, ...others] = 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
|
+
}
|
|
20
26
|
|
|
21
27
|
|
|
22
|
-
|
|
23
|
-
|
|
28
|
+
let typeFirst = currentFieldDefinition.instance
|
|
29
|
+
if (typeFirst === 'Array' && !others?.length) return []
|
|
24
30
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
31
|
+
let modelName = currentFieldDefinition.options.ref;
|
|
32
|
+
let refModel = (global as any).connectionMongoose.model(modelName);
|
|
33
|
+
let collectionName = refModel.collection.name;
|
|
28
34
|
|
|
29
35
|
|
|
30
|
-
|
|
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
|
|
31
55
|
populateClauseLookup.push({
|
|
32
|
-
$
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
as: `${previousPath}.${currentField}`
|
|
56
|
+
$group: {
|
|
57
|
+
_id: "$_id",
|
|
58
|
+
doc: { $first: "$$ROOT" }, // pega o documento inteiro
|
|
59
|
+
[`${previousPath}`]: { $push: `$${previousPath}` } // agrupa apenas o array
|
|
37
60
|
}
|
|
38
|
-
})
|
|
39
|
-
|
|
61
|
+
});
|
|
40
62
|
populateClauseLookup.push({
|
|
41
|
-
$
|
|
42
|
-
|
|
43
|
-
|
|
63
|
+
$replaceRoot: {
|
|
64
|
+
newRoot: {
|
|
65
|
+
$mergeObjects: ["$doc", { [`${previousPath}`]: { $push: `$${previousPath}` } }]
|
|
66
|
+
}
|
|
44
67
|
}
|
|
45
|
-
})
|
|
68
|
+
});
|
|
46
69
|
|
|
47
70
|
populateClauses = [...populateClauses, ...populateClauseLookup]
|
|
71
|
+
} catch (error: any) {
|
|
72
|
+
console.error(error.message)
|
|
48
73
|
}
|
|
49
|
-
|
|
50
|
-
//end code here
|
|
51
|
-
populateClauseLookup.push({
|
|
52
|
-
$group: {
|
|
53
|
-
_id: "$_id",
|
|
54
|
-
doc: { $first: "$$ROOT" }, // pega o documento inteiro
|
|
55
|
-
[`${previousPath}`]: { $push: `$${previousPath}` } // agrupa apenas o array
|
|
56
|
-
}
|
|
57
|
-
});
|
|
58
|
-
populateClauseLookup.push({
|
|
59
|
-
$replaceRoot: {
|
|
60
|
-
newRoot: {
|
|
61
|
-
$mergeObjects: ["$doc", { [`${previousPath}`]: { $push: `$${previousPath}` } }]
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
return populateClauses
|
|
67
74
|
}
|
|
68
75
|
}
|
|
69
76
|
|