@cocreate/mongodb 1.21.0 → 1.22.0

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.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ # [1.22.0](https://github.com/CoCreate-app/CoCreate-mongodb/compare/v1.21.1...v1.22.0) (2024-12-19)
2
+
3
+
4
+ ### Features
5
+
6
+ * impoved projection handeling,mergeTtoDotNotation function for queries etc ([d18f84e](https://github.com/CoCreate-app/CoCreate-mongodb/commit/d18f84e82a9edced30aa42b13d9442e7c841b6e8))
7
+
8
+ ## [1.21.1](https://github.com/CoCreate-app/CoCreate-mongodb/compare/v1.21.0...v1.21.1) (2024-12-05)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * projection handeling ([ee8a878](https://github.com/CoCreate-app/CoCreate-mongodb/commit/ee8a87856bd2032242b2e0dd5f05b995ee519f23))
14
+
1
15
  # [1.21.0](https://github.com/CoCreate-app/CoCreate-mongodb/compare/v1.20.0...v1.21.0) (2024-11-26)
2
16
 
3
17
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/mongodb",
3
- "version": "1.21.0",
3
+ "version": "1.22.0",
4
4
  "description": "A simple mongodb component in vanilla javascript. Easily configured using HTML5 data-attributes and/or JavaScript API.",
5
5
  "keywords": [
6
6
  "mongodb",
package/src/index.js CHANGED
@@ -374,6 +374,9 @@ function object(method, data) {
374
374
  update = {},
375
375
  options = {};
376
376
 
377
+ if (data.$filter && data.$filter.key)
378
+ projection = data.$filter.key;
379
+
377
380
  if (method === "update")
378
381
  createUpdate(update, options, data, true);
379
382
 
@@ -422,7 +425,7 @@ function object(method, data) {
422
425
  by: data.user_id || data.clientId
423
426
  };
424
427
  } else if (method === "read") {
425
- projection = createProjection(data[type][i]);
428
+ // projection = createProjection(projection, data[type][i]);
426
429
  } else if (method === "update") {
427
430
  if (!data[type][i].modified)
428
431
  data[type][i].modified = {
@@ -490,10 +493,9 @@ function object(method, data) {
490
493
  result.insertedId.toString();
491
494
  // documents.push({ ...data[type][i], ...reference })
492
495
  } else if (method === "read") {
493
- result = await arrayObj.findOne(
494
- query,
496
+ result = await arrayObj.findOne(query, {
495
497
  projection
496
- );
498
+ });
497
499
  if (result)
498
500
  result._id = result._id.toString();
499
501
 
@@ -545,13 +547,16 @@ function object(method, data) {
545
547
  continue;
546
548
  }
547
549
  } else if (method === "update") {
548
- if (update['$pull'] && update['$unset']) {
550
+ if (
551
+ update["$pull"] &&
552
+ update["$unset"]
553
+ ) {
549
554
  result = await arrayObj.updateOne(
550
555
  query,
551
- {$unset: update['$unset']},
556
+ { $unset: update["$unset"] },
552
557
  options
553
558
  );
554
- delete update['$unset']
559
+ delete update["$unset"];
555
560
  }
556
561
  result = await arrayObj.updateOne(
557
562
  query,
@@ -608,14 +613,14 @@ function object(method, data) {
608
613
  document = "";
609
614
  if (Object.keys(sort).length > 0)
610
615
  cursor = arrayObj
611
- .find(query, projection)
616
+ .find(query, { projection })
612
617
  .sort(sort)
613
618
  .skip(index)
614
619
  .limit(limit)
615
620
  .allowDiskUse(true);
616
621
  else
617
622
  cursor = arrayObj
618
- .find(query, projection)
623
+ .find(query, { projection })
619
624
  .sort(sort)
620
625
  .skip(index)
621
626
  .limit(limit);
@@ -715,15 +720,23 @@ function object(method, data) {
715
720
 
716
721
  let result;
717
722
  if (method === "update") {
718
- if (update['$pull'] && update['$unset']) {
719
- result = await arrayObj.updateOne(
720
- {
721
- _id: document._id
722
- },
723
- {$unset: update['$unset']},
724
- options
725
- );
726
- delete update['$unset']
723
+ if (
724
+ update["$pull"] &&
725
+ update["$unset"]
726
+ ) {
727
+ result =
728
+ await arrayObj.updateOne(
729
+ {
730
+ _id: document._id
731
+ },
732
+ {
733
+ $unset: update[
734
+ "$unset"
735
+ ]
736
+ },
737
+ options
738
+ );
739
+ delete update["$unset"];
727
740
  }
728
741
 
729
742
  if (options.returnNewDocument) {
@@ -912,13 +925,13 @@ function createUpdate(update, options, data, isGlobal) {
912
925
  operator = "$unset";
913
926
  updates[key] = 1;
914
927
  // if (!updates["$pull"]) updates["$pull"] = {};
915
- const pullkey = key.split('.');
916
- pullkey.shift();
917
- pullkey.pop();
928
+ const pullkey = key.split(".");
929
+ pullkey.shift();
930
+ pullkey.pop();
918
931
  // updates["$pull"][pullkey.join('.')] = null;
919
932
  // updates["$pull"][pullkey.join('.')] = { $in: [null] };
920
- if (!update["$pull"]) update["$pull"] = {};
921
- update["$pull"][pullkey.join('.')] = null;
933
+ if (!update["$pull"]) update["$pull"] = {};
934
+ update["$pull"][pullkey.join(".")] = null;
922
935
  } else if (operator === "$pop") {
923
936
  key = arrayKey;
924
937
  updates[key] = index || 1;
@@ -977,41 +990,45 @@ async function createFilter(data, arrayObj) {
977
990
  return value;
978
991
  }
979
992
 
993
+ // Recursive function to merge keys into dot notation
994
+ function mergeToDotNotation(obj, parentKey = "", result = {}) {
995
+ for (let key in obj) {
996
+ const isOperator = key.startsWith("$");
997
+ const currentKey = parentKey ? `${parentKey}.${key}` : key;
998
+
999
+ if (
1000
+ obj[key] &&
1001
+ typeof obj[key] === "object" &&
1002
+ !Array.isArray(obj[key])
1003
+ ) {
1004
+ if (isOperator) {
1005
+ // Ensure operators are grouped under their parent key
1006
+ result[parentKey] = result[parentKey] || {};
1007
+ result[parentKey][key] = obj[key];
1008
+ } else {
1009
+ // Recurse into nested objects
1010
+ mergeToDotNotation(obj[key], currentKey, result);
1011
+ }
1012
+ } else {
1013
+ // Assign to result, merging into dot notation if applicable
1014
+ if (isOperator) {
1015
+ result[parentKey] = result[parentKey] || {};
1016
+ result[parentKey][key] = obj[key];
1017
+ } else {
1018
+ result[currentKey] = obj[key];
1019
+ }
1020
+ }
1021
+ }
1022
+ return result;
1023
+ }
1024
+
980
1025
  if (data.$filter.query) {
981
1026
  for (let key in data.$filter.query) {
982
1027
  if (Array.isArray(data.$filter.query[key])) {
983
1028
  // Handle $or operator with an array of conditions
984
1029
  query[key] = data.$filter.query[key].map((condition) => {
985
1030
  let newCondition = {};
986
- for (let subKey in condition) {
987
- if (
988
- typeof condition[subKey] === "object" &&
989
- condition[subKey] !== null
990
- ) {
991
- newCondition[subKey] = {};
992
- for (let subCondition in condition[subKey]) {
993
- if (subKey == "_id")
994
- newCondition[subKey][subCondition] =
995
- ObjectId(
996
- condition[subKey][subCondition]
997
- );
998
- else
999
- newCondition[subKey][subCondition] =
1000
- convertIfDate(
1001
- condition[subKey][subCondition]
1002
- );
1003
- }
1004
- } else {
1005
- if (subKey == "_id")
1006
- newCondition[subKey] = ObjectId(
1007
- condition[subKey]
1008
- );
1009
- else
1010
- newCondition[subKey] = convertIfDate(
1011
- condition[subKey]
1012
- );
1013
- }
1014
- }
1031
+ mergeToDotNotation(condition, "", newCondition);
1015
1032
  return newCondition;
1016
1033
  });
1017
1034
  } else if (
@@ -1019,22 +1036,13 @@ async function createFilter(data, arrayObj) {
1019
1036
  data.$filter.query[key] !== null
1020
1037
  ) {
1021
1038
  // Handle general object conditions
1022
- query[key] = {};
1023
- for (let condition in data.$filter.query[key]) {
1024
- if (key == "_id")
1025
- query[key][condition] = ObjectId(
1026
- data.$filter.query[key][condition]
1027
- );
1028
- else
1029
- query[key][condition] = convertIfDate(
1030
- data.$filter.query[key][condition]
1031
- );
1032
- }
1039
+ mergeToDotNotation(data.$filter.query[key], key, query);
1033
1040
  } else {
1034
1041
  // Handle direct values
1035
- if (key == "_id")
1036
- query[key] = ObjectId(data.$filter.query[key]);
1037
- else query[key] = convertIfDate(data.$filter.query[key]);
1042
+ query[key] =
1043
+ key === "_id"
1044
+ ? ObjectId(data.$filter.query[key])
1045
+ : convertIfDate(data.$filter.query[key]);
1038
1046
  }
1039
1047
  }
1040
1048
  }
@@ -1042,8 +1050,7 @@ async function createFilter(data, arrayObj) {
1042
1050
  if (data.$filter.sort) {
1043
1051
  for (let i = 0; i < data.$filter.sort.length; i++) {
1044
1052
  let direction = data.$filter.sort[i].direction;
1045
- if (direction == "desc" || direction == -1) direction = -1;
1046
- else direction = 1;
1053
+ direction = direction === "desc" || direction === -1 ? -1 : 1;
1047
1054
 
1048
1055
  sort[data.$filter.sort[i].key] = direction;
1049
1056
  }
@@ -1072,11 +1079,12 @@ function parseRegExp(regExpString) {
1072
1079
  };
1073
1080
  }
1074
1081
 
1075
- function createProjection(data) {
1076
- let projection = {};
1077
-
1082
+ function createProjection(projection, data) {
1078
1083
  Object.keys(data).forEach((key) => {
1079
- if (!["_id", "organization_id"].includes(key) && !key.startsWith("$"))
1084
+ if (
1085
+ !["_id", "organization_id", "isFIlter"].includes(key) &&
1086
+ !key.startsWith("$")
1087
+ )
1080
1088
  projection[key.replace(/\[(\d+)\]/g, ".$1")] = 1;
1081
1089
  });
1082
1090