@cocreate/mongodb 1.21.1 → 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,10 @@
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
+
1
8
  ## [1.21.1](https://github.com/CoCreate-app/CoCreate-mongodb/compare/v1.21.0...v1.21.1) (2024-12-05)
2
9
 
3
10
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/mongodb",
3
- "version": "1.21.1",
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
@@ -375,8 +375,8 @@ function object(method, data) {
375
375
  options = {};
376
376
 
377
377
  if (data.$filter && data.$filter.key)
378
- projection = data.$filter.key
379
-
378
+ projection = data.$filter.key;
379
+
380
380
  if (method === "update")
381
381
  createUpdate(update, options, data, true);
382
382
 
@@ -493,10 +493,9 @@ function object(method, data) {
493
493
  result.insertedId.toString();
494
494
  // documents.push({ ...data[type][i], ...reference })
495
495
  } else if (method === "read") {
496
- result = await arrayObj.findOne(
497
- query,
498
- {projection}
499
- );
496
+ result = await arrayObj.findOne(query, {
497
+ projection
498
+ });
500
499
  if (result)
501
500
  result._id = result._id.toString();
502
501
 
@@ -548,13 +547,16 @@ function object(method, data) {
548
547
  continue;
549
548
  }
550
549
  } else if (method === "update") {
551
- if (update['$pull'] && update['$unset']) {
550
+ if (
551
+ update["$pull"] &&
552
+ update["$unset"]
553
+ ) {
552
554
  result = await arrayObj.updateOne(
553
555
  query,
554
- {$unset: update['$unset']},
556
+ { $unset: update["$unset"] },
555
557
  options
556
558
  );
557
- delete update['$unset']
559
+ delete update["$unset"];
558
560
  }
559
561
  result = await arrayObj.updateOne(
560
562
  query,
@@ -611,14 +613,14 @@ function object(method, data) {
611
613
  document = "";
612
614
  if (Object.keys(sort).length > 0)
613
615
  cursor = arrayObj
614
- .find(query, {projection})
616
+ .find(query, { projection })
615
617
  .sort(sort)
616
618
  .skip(index)
617
619
  .limit(limit)
618
620
  .allowDiskUse(true);
619
621
  else
620
622
  cursor = arrayObj
621
- .find(query, {projection})
623
+ .find(query, { projection })
622
624
  .sort(sort)
623
625
  .skip(index)
624
626
  .limit(limit);
@@ -718,15 +720,23 @@ function object(method, data) {
718
720
 
719
721
  let result;
720
722
  if (method === "update") {
721
- if (update['$pull'] && update['$unset']) {
722
- result = await arrayObj.updateOne(
723
- {
724
- _id: document._id
725
- },
726
- {$unset: update['$unset']},
727
- options
728
- );
729
- 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"];
730
740
  }
731
741
 
732
742
  if (options.returnNewDocument) {
@@ -915,13 +925,13 @@ function createUpdate(update, options, data, isGlobal) {
915
925
  operator = "$unset";
916
926
  updates[key] = 1;
917
927
  // if (!updates["$pull"]) updates["$pull"] = {};
918
- const pullkey = key.split('.');
919
- pullkey.shift();
920
- pullkey.pop();
928
+ const pullkey = key.split(".");
929
+ pullkey.shift();
930
+ pullkey.pop();
921
931
  // updates["$pull"][pullkey.join('.')] = null;
922
932
  // updates["$pull"][pullkey.join('.')] = { $in: [null] };
923
- if (!update["$pull"]) update["$pull"] = {};
924
- update["$pull"][pullkey.join('.')] = null;
933
+ if (!update["$pull"]) update["$pull"] = {};
934
+ update["$pull"][pullkey.join(".")] = null;
925
935
  } else if (operator === "$pop") {
926
936
  key = arrayKey;
927
937
  updates[key] = index || 1;
@@ -980,41 +990,45 @@ async function createFilter(data, arrayObj) {
980
990
  return value;
981
991
  }
982
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
+
983
1025
  if (data.$filter.query) {
984
1026
  for (let key in data.$filter.query) {
985
1027
  if (Array.isArray(data.$filter.query[key])) {
986
1028
  // Handle $or operator with an array of conditions
987
1029
  query[key] = data.$filter.query[key].map((condition) => {
988
1030
  let newCondition = {};
989
- for (let subKey in condition) {
990
- if (
991
- typeof condition[subKey] === "object" &&
992
- condition[subKey] !== null
993
- ) {
994
- newCondition[subKey] = {};
995
- for (let subCondition in condition[subKey]) {
996
- if (subKey == "_id")
997
- newCondition[subKey][subCondition] =
998
- ObjectId(
999
- condition[subKey][subCondition]
1000
- );
1001
- else
1002
- newCondition[subKey][subCondition] =
1003
- convertIfDate(
1004
- condition[subKey][subCondition]
1005
- );
1006
- }
1007
- } else {
1008
- if (subKey == "_id")
1009
- newCondition[subKey] = ObjectId(
1010
- condition[subKey]
1011
- );
1012
- else
1013
- newCondition[subKey] = convertIfDate(
1014
- condition[subKey]
1015
- );
1016
- }
1017
- }
1031
+ mergeToDotNotation(condition, "", newCondition);
1018
1032
  return newCondition;
1019
1033
  });
1020
1034
  } else if (
@@ -1022,22 +1036,13 @@ async function createFilter(data, arrayObj) {
1022
1036
  data.$filter.query[key] !== null
1023
1037
  ) {
1024
1038
  // Handle general object conditions
1025
- query[key] = {};
1026
- for (let condition in data.$filter.query[key]) {
1027
- if (key == "_id")
1028
- query[key][condition] = ObjectId(
1029
- data.$filter.query[key][condition]
1030
- );
1031
- else
1032
- query[key][condition] = convertIfDate(
1033
- data.$filter.query[key][condition]
1034
- );
1035
- }
1039
+ mergeToDotNotation(data.$filter.query[key], key, query);
1036
1040
  } else {
1037
1041
  // Handle direct values
1038
- if (key == "_id")
1039
- query[key] = ObjectId(data.$filter.query[key]);
1040
- 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]);
1041
1046
  }
1042
1047
  }
1043
1048
  }
@@ -1045,8 +1050,7 @@ async function createFilter(data, arrayObj) {
1045
1050
  if (data.$filter.sort) {
1046
1051
  for (let i = 0; i < data.$filter.sort.length; i++) {
1047
1052
  let direction = data.$filter.sort[i].direction;
1048
- if (direction == "desc" || direction == -1) direction = -1;
1049
- else direction = 1;
1053
+ direction = direction === "desc" || direction === -1 ? -1 : 1;
1050
1054
 
1051
1055
  sort[data.$filter.sort[i].key] = direction;
1052
1056
  }
@@ -1077,7 +1081,10 @@ function parseRegExp(regExpString) {
1077
1081
 
1078
1082
  function createProjection(projection, data) {
1079
1083
  Object.keys(data).forEach((key) => {
1080
- if (!["_id", "organization_id", "isFIlter"].includes(key) && !key.startsWith("$"))
1084
+ if (
1085
+ !["_id", "organization_id", "isFIlter"].includes(key) &&
1086
+ !key.startsWith("$")
1087
+ )
1081
1088
  projection[key.replace(/\[(\d+)\]/g, ".$1")] = 1;
1082
1089
  });
1083
1090