c2-mongoose 2.1.364 → 2.1.366

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,6 @@
1
+ import mongoose from "mongoose";
2
+ declare class LookupArrayFlowItem {
3
+ execute(previousPath: string, remainingPath: string[], populateClauses: any[], model: mongoose.Model<any>): any[];
4
+ }
5
+ declare const _default: LookupArrayFlowItem;
6
+ export default _default;
@@ -0,0 +1,78 @@
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
+ 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); })) {
41
+ populateClauseLookup.push({
42
+ $lookup: {
43
+ from: collectionName,
44
+ localField: "".concat(previousPath, ".").concat(currentField),
45
+ foreignField: "_id",
46
+ as: "".concat(previousPath, ".").concat(currentField)
47
+ }
48
+ });
49
+ populateClauseLookup.push({
50
+ $unwind: {
51
+ path: "$".concat(previousPath, ".").concat(currentField),
52
+ preserveNullAndEmptyArrays: true
53
+ }
54
+ });
55
+ populateClauses = __spreadArray(__spreadArray([], populateClauses, true), populateClauseLookup, true);
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)] = { $push: "$".concat(previousPath) }, _b)]
71
+ }
72
+ }
73
+ });
74
+ return populateClauses;
75
+ };
76
+ return LookupArrayFlowItem;
77
+ }());
78
+ exports.default = new LookupArrayFlowItem;
@@ -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 LookupArrayFlowItem_1 = __importDefault(require("./LookupArrayFlowItem"));
16
17
  var PopulateFlowItem = /** @class */ (function () {
17
18
  function PopulateFlowItem() {
18
19
  }
@@ -27,6 +28,10 @@ var PopulateFlowItem = /** @class */ (function () {
27
28
  if (!fieldDefinition)
28
29
  return;
29
30
  var typeFirst = fieldDefinition.instance;
31
+ if (typeFirst === 'Array') {
32
+ LookupArrayFlowItem_1.default.execute(first, rest, populateClauses, modelRoot);
33
+ return;
34
+ }
30
35
  while ((typeFirst === 'Array' || typeFirst === 'Embedded') && (rest === null || rest === void 0 ? void 0 : rest.length)) {
31
36
  first = [first, rest[0]].join(".");
32
37
  rest.splice(0, 1);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "c2-mongoose",
3
- "version": "2.1.364",
3
+ "version": "2.1.366",
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",
@@ -0,0 +1,74 @@
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
+ const populateClauseLookup = []
8
+ populateClauseLookup.push({
9
+ $unwind: `$${previousPath}`
10
+ });
11
+
12
+
13
+ //init code here
14
+ let [currentField, ...others] = remainingPath
15
+
16
+ if (!currentField) {
17
+ return []
18
+ }
19
+
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
+ }
24
+
25
+
26
+ let typeFirst = currentFieldDefinition.instance
27
+ if (typeFirst === 'Array' && !others?.length) return []
28
+
29
+ let modelName = currentFieldDefinition.options.ref;
30
+ let refModel = (global as any).connectionMongoose.model(modelName);
31
+ let collectionName = refModel.collection.name;
32
+
33
+
34
+ if (!populateClauses.some((populateClause: any) => populateClause?.$lookup?.localField === `${previousPath}.${currentField}`)) {
35
+ 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}`
41
+ }
42
+ })
43
+
44
+ populateClauseLookup.push({
45
+ $unwind: {
46
+ path: `$${previousPath}.${currentField}`,
47
+ preserveNullAndEmptyArrays: true
48
+ }
49
+ })
50
+
51
+ populateClauses = [...populateClauses, ...populateClauseLookup]
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}`]: { $push: `$${previousPath}` } }]
66
+ }
67
+ }
68
+ });
69
+
70
+ return populateClauses
71
+ }
72
+ }
73
+
74
+ export default new LookupArrayFlowItem
@@ -1,5 +1,6 @@
1
1
  import mongoose from "mongoose";
2
2
  import GetFieldDefinitionFlowItem from "./GetFieldDefinitionFlowItem"
3
+ import LookupArrayFlowItem from "./LookupArrayFlowItem";
3
4
 
4
5
  class PopulateFlowItem {
5
6
  execute(populates: string = "", modelRoot: mongoose.Model<any>): any[] {
@@ -16,6 +17,11 @@ class PopulateFlowItem {
16
17
 
17
18
  let typeFirst = fieldDefinition.instance
18
19
 
20
+ if (typeFirst === 'Array') {
21
+ LookupArrayFlowItem.execute(first, rest, populateClauses, modelRoot)
22
+ return
23
+ }
24
+
19
25
  while ((typeFirst === 'Array' || typeFirst === 'Embedded') && rest?.length) {
20
26
  first = [first, rest[0]].join(".")
21
27
  rest.splice(0, 1)