@cocreate/mongodb 1.16.1 → 1.17.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,15 @@
1
+ # [1.17.0](https://github.com/CoCreate-app/CoCreate-mongodb/compare/v1.16.1...v1.17.0) (2024-01-17)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * update to support new query system ([7555076](https://github.com/CoCreate-app/CoCreate-mongodb/commit/7555076cf718f68d56547988879e910770a45c5e))
7
+
8
+
9
+ ### Features
10
+
11
+ * parseRegex expresion function to seperate pattern from options ([9401b07](https://github.com/CoCreate-app/CoCreate-mongodb/commit/9401b07b459f3a809e2ef0496a5df08adb5840b1))
12
+
1
13
  ## [1.16.1](https://github.com/CoCreate-app/CoCreate-mongodb/compare/v1.16.0...v1.16.1) (2024-01-08)
2
14
 
3
15
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/mongodb",
3
- "version": "1.16.1",
3
+ "version": "1.17.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
@@ -1,5 +1,5 @@
1
1
  const { MongoClient, ObjectId } = require('mongodb');
2
- const { dotNotationToObject, queryData, searchData, sortData, isValidDate } = require('@cocreate/utils')
2
+ const { dotNotationToObject, query, searchData, sortData, isValidDate } = require('@cocreate/utils')
3
3
  const clients = new Map()
4
4
  const organizations = {}
5
5
 
@@ -617,7 +617,7 @@ async function createFilter(data, arrayObj) {
617
617
 
618
618
  if (data.$filter) {
619
619
  if (data.$filter.query)
620
- query = createQuery(data.$filter.query);
620
+ query = data.$filter.query
621
621
 
622
622
  if (data.$filter.sort) {
623
623
  for (let i = 0; i < data.$filter.sort.length; i++) {
@@ -648,116 +648,12 @@ async function createFilter(data, arrayObj) {
648
648
  return { query, sort, index, limit, count }
649
649
  }
650
650
 
651
-
652
- // TODO: improve mongodb query to cover many cases
653
- function createQuery(queries) {
654
- let query = new Object();
655
-
656
- for (let item of queries) {
657
-
658
- if (!item.key)
659
- continue
660
-
661
- if (item.key == "_id") {
662
- if (item.value)
663
- try {
664
- item.value = ObjectId(item.value)
665
- } catch (error) {
666
- continue
667
- }
668
- else
669
- continue
670
- }
671
-
672
- if (isValidDate(item.value))
673
- item.value = new Date(item.value)
674
-
675
- let key = item.key;
676
- if (!query[key]) {
677
- query[key] = {};
678
- }
679
-
680
- switch (item.operator) {
681
- case '$includes':
682
- case 'includes':
683
- query[key]['$regex'] = item.value;
684
- break;
685
-
686
- case '$range':
687
- if (item.value[0] !== null && item.value[1] !== null) {
688
- query[key] = { $gte: item.value[0], $lte: item.value[1] };
689
- } else if (item.value[0] !== null) {
690
- query[key] = { $gte: item.value[0] };
691
- } else if (item.value[1] !== null) {
692
- query[key] = { $lte: item.value[1] };
693
- }
694
- break;
695
-
696
- case 'equals':
697
- query[$eq][item.operator] = item.value;
698
- case '$eq':
699
- case '$ne':
700
- case '$lt':
701
- case '$lte':
702
- case '$gt':
703
- case '$gte':
704
- case '$regex':
705
- query[key][item.operator] = item.value;
706
- break;
707
- case '$in':
708
- if (!Array.isArray(item.value))
709
- query[key][item.operator] = [item.value]
710
- else
711
- query[key] = { $in: item.value }
712
- break;
713
-
714
- case '$nin':
715
- if (!Array.isArray(item.value))
716
- query[key][item.operator] = [item.value]
717
- else
718
- query[key] = { $nin: item.value }
719
- break;
720
- case '$elemMatch':
721
- query[key] = {
722
- "$elemMatch": {
723
- name: {
724
- $in: item.value
725
- }
726
- }
727
- }
728
- break;
729
- case '$or':
730
- if (!query[item.operator])
731
- query[item.operator] = [{ [key]: item.value }];
732
- else
733
- query[item.operator].push({ [key]: item.value })
734
- delete query[key]
735
- break;
736
- case '$geoWithin':
737
- try {
738
- let value = JSON.parse(item.value);
739
- if (item.type) {
740
- query[key]['$geoWithin'] = {
741
- [item.type]: value
742
- }
743
- }
744
- } catch (e) {
745
- console.log('geowithin error');
746
- }
747
- break;
748
- case 'dotNotation':
749
- // TODO: handle CoCreate query using dotNotation, objects and utils.dotNotionToObject to perform deep merge
750
- break;
751
- }
752
- }
753
-
754
- //. global search
755
- //. we have to set indexes in text fields ex: db.chart.createIndex({ "$**": "text" })
756
- // if (data['searchKey']) {
757
- // query["$text"] = {$search: "\"Ni\""};
758
- // }
759
-
760
- return query;
651
+ function parseRegExp(regExpString) {
652
+ let matches = regExpString.match(/\/(.*)\/(.*)/);
653
+ return {
654
+ pattern: matches[1],
655
+ options: matches[2]
656
+ };
761
657
  }
762
658
 
763
659
  function createProjection(data) {