@cocreate/crud-server 1.13.7 → 1.14.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,16 @@
1
+ # [1.14.0](https://github.com/CoCreate-app/CoCreate-crud-server/compare/v1.13.7...v1.14.0) (2022-11-25)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * querying an array of documents requires the _id to be wrapped in ObjectId ([d988da8](https://github.com/CoCreate-app/CoCreate-crud-server/commit/d988da872248cf68bbeaf4d47bec6d8875ddda23))
7
+ * replaced @cocreate/filter with @cocreate/utils fo search, query and sort functions ([f91fb78](https://github.com/CoCreate-app/CoCreate-crud-server/commit/f91fb78dea770b4afc72dd5ad2842e4745d659f8))
8
+
9
+
10
+ ### Features
11
+
12
+ * run dotNotationToObject on createDocument ([133cc30](https://github.com/CoCreate-app/CoCreate-crud-server/commit/133cc302771ef139642e47f31c0f308a69c07869))
13
+
1
14
  ## [1.13.7](https://github.com/CoCreate-app/CoCreate-crud-server/compare/v1.13.6...v1.13.7) (2022-11-24)
2
15
 
3
16
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/crud-server",
3
- "version": "1.13.7",
3
+ "version": "1.14.0",
4
4
  "description": "CoCreate-crud-server",
5
5
  "keywords": [
6
6
  "cocreate-crud",
@@ -40,7 +40,8 @@
40
40
  },
41
41
  "homepage": "https://cocreate.app/docs/CoCreate-crud-server",
42
42
  "dependencies": {
43
- "@cocreate/docs": "^1.4.3",
43
+ "@cocreate/docs": "^1.4.4",
44
+ "@cocreate/utils": "^1.12.0",
44
45
  "csvtojson": "^2.0.10",
45
46
  "json-2-csv": "^3.10.3",
46
47
  "mongodb": "^4.4.0"
package/src/index.js CHANGED
@@ -1,7 +1,6 @@
1
1
  'use strict';
2
2
 
3
- const {replaceArray} = require("./utils.crud.js")
4
- const {searchData, sortData} = require("@cocreate/filter")
3
+ const {searchData, sortData} = require("@cocreate/utils");
5
4
  const mongodb = require('./mongodb/mongodb');
6
5
 
7
6
  class CoCreateCrudServer {
@@ -1,7 +1,5 @@
1
- // const {mongoClient} = require("./db")
2
1
  const {MongoClient, ObjectId} = require('mongodb');
3
- const {replaceArray} = require("../utils.crud.js")
4
- const {searchData, sortData} = require("@cocreate/filter")
2
+ const {dotNotationToObject, searchData, sortData} = require('@cocreate/utils')
5
3
 
6
4
  function mongoClient(dbUrl) {
7
5
  try {
@@ -318,6 +316,8 @@ function document(action, data){
318
316
 
319
317
 
320
318
  if (action == 'createDocument') {
319
+ data[type][i] = dotNotationToObject(data[type][i])
320
+
321
321
  if (!data[type][i]._id)
322
322
  data[type][i]._id = ObjectId()
323
323
  else
@@ -326,7 +326,7 @@ function document(action, data){
326
326
  }
327
327
  if (action == 'readDocument') {
328
328
  if (data[type][i]._id)
329
- _ids.push(data[type][i]._id)
329
+ _ids.push(ObjectId(data[type][i]._id))
330
330
  }
331
331
  if (action =='updateDocument') {
332
332
  if (data[type][i]._id)
@@ -340,7 +340,7 @@ function document(action, data){
340
340
  }
341
341
  if (action =='deleteDocument') {
342
342
  if (data[type][i]._id) {
343
- _ids.push(data[type][i]._id)
343
+ _ids.push(ObjectId(data[type][i]._id))
344
344
  documents.push({_id: data[type][i]._id, db: 'mongodb', database, collection})
345
345
  }
346
346
  }
@@ -448,7 +448,7 @@ function document(action, data){
448
448
  documents.push({_id: doc._id, db: 'mongodb', database, collection})
449
449
  else
450
450
  doc['modified'] = {on: data.timeStamp, by: data.user || data.clientId}
451
-
451
+
452
452
  _ids.push(doc._id)
453
453
  }
454
454
  update_ids.push({updateType: 'filter'})
@@ -480,11 +480,10 @@ function document(action, data){
480
480
  update['$set']['organization_id'] = data.organization_id
481
481
 
482
482
  collectionObj.updateMany(query, update, {
483
- upsert: false,
483
+ upsert: data.upsert,
484
484
  projection
485
485
  }).then((result) => {
486
-
487
-
486
+
488
487
  }).catch((error) => {
489
488
  errorLog.push(error)
490
489
  console.log(action, 'error', error);
@@ -557,7 +556,7 @@ function document(action, data){
557
556
  function createUpdate(data, type) {
558
557
  let update = {}, projection = {};
559
558
  if (data[type][0]) {
560
- update['$set'] = valueTypes(data[type][0])
559
+ update['$set'] = data[type][0]
561
560
  // update['$set']['organization_id'] = data['organization_id'];
562
561
  if (update['$set']['_id'])
563
562
  delete update['$set']['_id']
@@ -709,35 +708,6 @@ function createQuery(filters) {
709
708
  return query;
710
709
  }
711
710
 
712
- function valueTypes(data) {
713
- let object = {}
714
- if ( typeof data === 'object' ) {
715
- // update['$set'] = {}
716
- for (const [key, value] of Object.entries(data)) {
717
- let val;
718
- let valueType = typeof value;
719
- switch(valueType) {
720
- case 'string':
721
- val = value
722
- break;
723
- case 'number':
724
- val = Number(value)
725
- break;
726
- case 'object':
727
- if (Array.isArray(value))
728
- val = new Array(...value)
729
- else
730
- val = new Object(value)
731
- break;
732
- default:
733
- val = value
734
- }
735
- object[key] = val
736
- }
737
- return object;
738
- }
739
- }
740
-
741
711
  function errorHandler(data, error, database, collection){
742
712
  if (typeof error == 'object')
743
713
  error['db'] = 'mongodb'
@@ -754,7 +724,24 @@ function errorHandler(data, error, database, collection){
754
724
  data.error = [error]
755
725
  }
756
726
 
757
-
727
+ function replaceArray(data) {
728
+ let keys = Object.keys(data);
729
+ let objectData = {};
730
+
731
+ keys.forEach((k) => {
732
+ let nk = k
733
+ if (/\[([0-9]*)\]/g.test(k)) {
734
+ nk = nk.replace(/\[/g, '.');
735
+ if (nk.endsWith(']'))
736
+ nk = nk.slice(0, -1)
737
+ nk = nk.replace(/\]./g, '.');
738
+ nk = nk.replace(/\]/g, '.');
739
+ }
740
+ objectData[nk] = data[k];
741
+ });
742
+
743
+ return objectData;
744
+ }
758
745
 
759
746
 
760
747
  module.exports = {
package/src/utils.crud.js DELETED
@@ -1,22 +0,0 @@
1
- function replaceArray(data) {
2
- let keys = Object.keys(data);
3
- let objectData = {};
4
-
5
- keys.forEach((k) => {
6
- let nk = k
7
- if (/\[([0-9]*)\]/g.test(k)) {
8
- nk = nk.replace(/\[/g, '.');
9
- if (nk.endsWith(']'))
10
- nk = nk.slice(0, -1)
11
- nk = nk.replace(/\]./g, '.');
12
- nk = nk.replace(/\]/g, '.');
13
- }
14
- objectData[nk] = data[k];
15
- });
16
-
17
- return objectData;
18
- }
19
-
20
- module.exports = {
21
- replaceArray
22
- };