c2-mongoose 2.1.366 → 2.1.368

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