c2-mongoose 2.1.381 → 2.1.382
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.
|
@@ -3,7 +3,7 @@ export interface IForeingData {
|
|
|
3
3
|
parentFieldOnChild: string;
|
|
4
4
|
}
|
|
5
5
|
declare class OneToManyFlowItem {
|
|
6
|
-
execute(foreings: IForeingData[]): any[];
|
|
6
|
+
execute(foreings: IForeingData[], populate?: string): any[];
|
|
7
7
|
}
|
|
8
8
|
declare const _default: OneToManyFlowItem;
|
|
9
9
|
export default _default;
|
|
@@ -8,25 +8,37 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
|
8
8
|
}
|
|
9
9
|
return to.concat(ar || Array.prototype.slice.call(from));
|
|
10
10
|
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
11
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
15
|
var lodash_1 = require("lodash");
|
|
16
|
+
var PopulateFlowItem_1 = __importDefault(require("./PopulateFlowItem"));
|
|
17
|
+
var mongoose_1 = __importDefault(require("mongoose"));
|
|
13
18
|
var OneToManyFlowItem = /** @class */ (function () {
|
|
14
19
|
function OneToManyFlowItem() {
|
|
15
20
|
}
|
|
16
|
-
OneToManyFlowItem.prototype.execute = function (foreings) {
|
|
21
|
+
OneToManyFlowItem.prototype.execute = function (foreings, populate) {
|
|
22
|
+
if (populate === void 0) { populate = ""; }
|
|
17
23
|
var collection = [];
|
|
18
24
|
foreings === null || foreings === void 0 ? void 0 : foreings.forEach(function (foreing) {
|
|
25
|
+
var labeled = (0, lodash_1.camelCase)(foreing.collectionChild);
|
|
19
26
|
var populateClauseLookup = [
|
|
20
27
|
{
|
|
21
28
|
$lookup: {
|
|
22
29
|
from: foreing.collectionChild,
|
|
23
30
|
localField: "_id",
|
|
24
31
|
foreignField: foreing.parentFieldOnChild,
|
|
25
|
-
as:
|
|
32
|
+
as: labeled
|
|
26
33
|
}
|
|
27
34
|
},
|
|
28
35
|
];
|
|
29
36
|
collection = __spreadArray(__spreadArray([], collection, true), populateClauseLookup, true);
|
|
37
|
+
var model = mongoose_1.default.connection.models[Object.keys(mongoose_1.default.connection.models).find(function (key) { return mongoose_1.default.connection.models[key].collection.name === foreing.collectionChild; })];
|
|
38
|
+
var populates = populate.split(" ").filter(function (p) { return p.startsWith(labeled); }).join(" ");
|
|
39
|
+
if (populates && model) {
|
|
40
|
+
collection = __spreadArray(__spreadArray([], collection, true), PopulateFlowItem_1.default.execute(populates, model), true);
|
|
41
|
+
}
|
|
30
42
|
});
|
|
31
43
|
return collection;
|
|
32
44
|
};
|
package/package.json
CHANGED
|
@@ -1,25 +1,41 @@
|
|
|
1
1
|
import { camelCase } from "lodash"
|
|
2
|
+
import PopulateFlowItem from "./PopulateFlowItem";
|
|
3
|
+
import mongoose from "mongoose";
|
|
2
4
|
export interface IForeingData {
|
|
3
5
|
collectionChild: string;
|
|
4
6
|
parentFieldOnChild: string;
|
|
5
7
|
}
|
|
6
8
|
|
|
7
9
|
class OneToManyFlowItem {
|
|
8
|
-
execute(foreings: IForeingData[]): any[] {
|
|
10
|
+
execute(foreings: IForeingData[], populate: string = ""): any[] {
|
|
9
11
|
let collection: any[] = []
|
|
10
12
|
foreings?.forEach((foreing: IForeingData) => {
|
|
13
|
+
|
|
14
|
+
const labeled = camelCase(foreing.collectionChild)
|
|
11
15
|
const populateClauseLookup: any[] = [
|
|
12
16
|
{
|
|
13
17
|
$lookup: {
|
|
14
18
|
from: foreing.collectionChild, // The collection where user data is stored
|
|
15
19
|
localField: "_id",
|
|
16
20
|
foreignField: foreing.parentFieldOnChild,
|
|
17
|
-
as:
|
|
21
|
+
as: labeled
|
|
18
22
|
}
|
|
19
23
|
},
|
|
20
24
|
]
|
|
21
25
|
|
|
22
26
|
collection = [...collection, ...populateClauseLookup]
|
|
27
|
+
|
|
28
|
+
const model = mongoose.connection.models[
|
|
29
|
+
Object.keys(mongoose.connection.models).find(
|
|
30
|
+
key => mongoose.connection.models[key].collection.name === foreing.collectionChild
|
|
31
|
+
)!
|
|
32
|
+
];
|
|
33
|
+
|
|
34
|
+
const populates = populate.split(" ").filter((p: string) => p.startsWith(labeled)).join(" ")
|
|
35
|
+
|
|
36
|
+
if (populates && model) {
|
|
37
|
+
collection = [...collection, ...PopulateFlowItem.execute(populates, model)]
|
|
38
|
+
}
|
|
23
39
|
})
|
|
24
40
|
|
|
25
41
|
return collection
|