c2-mongoose 2.1.372 → 2.1.374

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