@cocreate/crud-server 1.3.3 → 1.4.1

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,27 @@
1
+ ## [1.4.1](https://github.com/CoCreate-app/CoCreate-crud-server/compare/v1.4.0...v1.4.1) (2022-05-06)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * replace ObjectID with ObjectId due to mongodb depreciation ([93e4294](https://github.com/CoCreate-app/CoCreate-crud-server/commit/93e42944447357ec6a244b72ce968bbd8ff5c93d))
7
+ * update config organization_Id to organization_id ([996611b](https://github.com/CoCreate-app/CoCreate-crud-server/commit/996611baf2b2d3afbf895ef193ae15ba7226e77a))
8
+
9
+ # [1.4.0](https://github.com/CoCreate-app/CoCreate-crud-server/compare/v1.3.4...v1.4.0) (2022-04-01)
10
+
11
+
12
+ ### Features
13
+
14
+ * readCollections to return a list of collections ([2ad15d4](https://github.com/CoCreate-app/CoCreate-crud-server/commit/2ad15d43baf22c6dabbf56582cac182dec8876ad))
15
+
16
+ ## [1.3.4](https://github.com/CoCreate-app/CoCreate-crud-server/compare/v1.3.3...v1.3.4) (2022-03-06)
17
+
18
+
19
+ ### Bug Fixes
20
+
21
+ * broadcast_sender if condition removed and handeled by socket ([c2a0a6d](https://github.com/CoCreate-app/CoCreate-crud-server/commit/c2a0a6d79d331e56160cd9ddaf8a7e521e2270e8))
22
+ * removed param isExact from broadcast as it can be handled by param room ([e709e36](https://github.com/CoCreate-app/CoCreate-crud-server/commit/e709e366493759399f371f2ffc3c630e415f8daa))
23
+ * update param roomInfo to socketInfo ([f50edc9](https://github.com/CoCreate-app/CoCreate-crud-server/commit/f50edc91ee4695f6a652fd8a8c943feac24ba80a))
24
+
1
25
  ## [1.3.3](https://github.com/CoCreate-app/CoCreate-crud-server/compare/v1.3.2...v1.3.3) (2022-03-05)
2
26
 
3
27
 
@@ -1,7 +1,7 @@
1
1
  module.exports = {
2
2
  config: {
3
3
  "apiKey": "2061acef-0451-4545-f754-60cf8160",
4
- "organization_Id": "5ff747727005da1c272740ab",
4
+ "organization_id": "5ff747727005da1c272740ab",
5
5
  "host": "general.cocreate.app"
6
6
  },
7
7
  "sources": [
package/docs/index.html CHANGED
@@ -124,7 +124,7 @@
124
124
  var config = {
125
125
  apiKey: 'c2b08663-06e3-440c-ef6f-13978b42883a',
126
126
  securityKey: 'f26baf68-e3a9-45fc-effe-502e47116265',
127
- organization_Id: '5de0387b12e200ea63204d6c'
127
+ organization_id: '5de0387b12e200ea63204d6c'
128
128
  }
129
129
  </script>
130
130
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/crud-server",
3
- "version": "1.3.3",
3
+ "version": "1.4.1",
4
4
  "description": "CoCreate-crud-server",
5
5
  "keywords": [
6
6
  "cocreate-crud",
package/src/backup.js CHANGED
@@ -13,11 +13,11 @@ class CoCreateBackup {
13
13
 
14
14
  init() {
15
15
  if (this.wsManager) {
16
- // this.wsManager.on('exportDB', (socket, data, roomInfo) => this.exportData(socket, data, roomInfo));
17
- // this.wsManager.on('importDB', (socket, data, roomInfo) => this.importData(socket, data, roomInfo));
18
- // this.wsManager.on('importFile2DB', (socket, data, roomInfo) => this.importData(socket, data, roomInfo));
16
+ // this.wsManager.on('exportDB', (socket, data, socketInfo) => this.exportData(socket, data, socketInfo));
17
+ // this.wsManager.on('importDB', (socket, data, socketInfo) => this.importData(socket, data, socketInfo));
18
+ // this.wsManager.on('importFile2DB', (socket, data, socketInfo) => this.importData(socket, data, socketInfo));
19
19
 
20
- // this.wsManager.on('downloadData', (socket, data, roomInfo) => this.downloadData(socket, data, roomInfo))
20
+ // this.wsManager.on('downloadData', (socket, data, socketInfo) => this.downloadData(socket, data, socketInfo))
21
21
  }
22
22
  }
23
23
 
@@ -28,13 +28,13 @@ class CoCreateBackup {
28
28
  data: JSON data
29
29
  }
30
30
  **/
31
- // async downloadData(socket, data, roomInfo) {
31
+ // async downloadData(socket, data, socketInfo) {
32
32
  // const export_type = data.type || "json";
33
33
 
34
34
  // try {
35
35
  // let binaryData = null;
36
36
  // const result = data.data;
37
- // const orgId = roomInfo ? roomInfo.orgId : "";
37
+ // const orgId = socketInfo ? socketInfo.orgId : "";
38
38
  // if (export_type === 'csv') {
39
39
  // binaryData = await json2csv.json2csvAsync(JSON.parse(JSON.stringify(result)), {
40
40
  // emptyFieldValue: ''
@@ -60,7 +60,7 @@ class CoCreateBackup {
60
60
 
61
61
  })
62
62
  **/
63
- // async exportData(socket, data, roomInfo) {
63
+ // async exportData(socket, data, socketInfo) {
64
64
  // const self = this;
65
65
 
66
66
  // const export_type = data.export_type || "json";
@@ -68,7 +68,7 @@ class CoCreateBackup {
68
68
  // try {
69
69
 
70
70
  // var collection = this.dbClient.db(data['namespace']).collection(data["collection"]);
71
- // const orgId = roomInfo ? roomInfo.orgId : "";
71
+ // const orgId = socketInfo ? socketInfo.orgId : "";
72
72
 
73
73
  // var query = {};
74
74
 
@@ -92,13 +92,13 @@ class CoCreateBackup {
92
92
  // }
93
93
  // }
94
94
 
95
- // async setImportData(socket, data, roomInfo) {
95
+ // async setImportData(socket, data, socketInfo) {
96
96
  // this.importCollection = data['collection']
97
97
  // this.importType = data['import_type'];
98
98
  // this.importDB = data['namespace'];
99
99
  // }
100
100
 
101
- // async importData(socket, data, roomInfo) {
101
+ // async importData(socket, data, socketInfo) {
102
102
  // const importCollection = data['collection']
103
103
  // const importType = data['import_type'];
104
104
  // const importFile = data['file'];
@@ -106,7 +106,7 @@ class CoCreateBackup {
106
106
 
107
107
  // console.log('import:', importCollection, importType, importFile)
108
108
  // const self = this;
109
- // // const orgId = roomInfo ? roomInfo.orgId : "";
109
+ // // const orgId = socketInfo ? socketInfo.orgId : "";
110
110
  // if (!importCollection || !importType) {
111
111
  // return;
112
112
  // }
package/src/crud.js CHANGED
@@ -1,4 +1,4 @@
1
- const {ObjectID} = require("mongodb");
1
+ const {ObjectId} = require("mongodb");
2
2
  const {encodeObject, replaceArray} = require("./utils.crud.js")
3
3
 
4
4
 
@@ -11,15 +11,15 @@ class CoCreateCrud {
11
11
 
12
12
  init() {
13
13
  if (this.wsManager) {
14
- this.wsManager.on('createDocument', (socket, data, roomInfo) => this.createDocument(socket, data, roomInfo));
15
- this.wsManager.on('readDocument', (socket, data, roomInfo) => this.readDocument(socket, data, roomInfo))
16
- this.wsManager.on('updateDocument', (socket, data, roomInfo) => this.updateDocument(socket, data, roomInfo))
17
- this.wsManager.on('deleteDocument', (socket, data, roomInfo) => this.deleteDocument(socket, data, roomInfo))
14
+ this.wsManager.on('createDocument', (socket, data, socketInfo) => this.createDocument(socket, data, socketInfo));
15
+ this.wsManager.on('readDocument', (socket, data, socketInfo) => this.readDocument(socket, data, socketInfo))
16
+ this.wsManager.on('updateDocument', (socket, data, socketInfo) => this.updateDocument(socket, data, socketInfo))
17
+ this.wsManager.on('deleteDocument', (socket, data, socketInfo) => this.deleteDocument(socket, data, socketInfo))
18
18
  }
19
19
  }
20
20
 
21
21
  /** Create Document **/
22
- async createDocument(socket, req_data, roomInfo){
22
+ async createDocument(socket, req_data, socketInfo){
23
23
  const self = this;
24
24
  if(!req_data.data) return;
25
25
 
@@ -30,25 +30,25 @@ class CoCreateCrud {
30
30
 
31
31
  collection.insertOne(insertData, function(error, result) {
32
32
  if(!error && result){
33
- const response = {...req_data, document_id: result.ops[0]._id, data:result.ops[0] }
33
+ const response = {...req_data, document_id: `${result.insertedId}`, data: insertData }
34
34
  // let isFlat = req_data.isFlat == false ? false : true;
35
35
  // const response_data = isFlat ? encodeObject(response) : response;
36
36
  // const response_data = response;
37
- self.broadcast('createDocument', socket, req_data, response, roomInfo)
37
+ self.broadcast('createDocument', socket, response, socketInfo)
38
38
  } else {
39
- self.wsManager.send(socket, 'ServerError', error, null, roomInfo);
39
+ self.wsManager.send(socket, 'ServerError', error, null, socketInfo);
40
40
  }
41
41
  });
42
42
  }catch(error){
43
43
  console.log('createDocument error', error);
44
- self.wsManager.send(socket, 'ServerError', 'error', null, roomInfo);
44
+ self.wsManager.send(socket, 'ServerError', 'error', null, socketInfo);
45
45
  }
46
46
  }
47
47
 
48
48
  /** Read Document **/
49
- async readDocument(socket, req_data, roomInfo) {
49
+ async readDocument(socket, req_data, socketInfo) {
50
50
  if (!req_data['collection'] || req_data['collection'] == 'null' || typeof req_data['collection'] !== 'string') {
51
- this.wsManager.send(socket, 'ServerError', 'error', null, roomInfo);
51
+ this.wsManager.send(socket, 'ServerError', 'error', null, socketInfo);
52
52
  return;
53
53
  }
54
54
  const self = this;
@@ -58,7 +58,7 @@ class CoCreateCrud {
58
58
  const collection = db.collection(req_data["collection"]);
59
59
 
60
60
  const query = {
61
- "_id": new ObjectID(req_data["document_id"])
61
+ "_id": new ObjectId(req_data["document_id"])
62
62
  };
63
63
  if (req_data['organization_id']) {
64
64
  query['organization_id'] = req_data['organization_id'];
@@ -81,27 +81,27 @@ class CoCreateCrud {
81
81
  }
82
82
 
83
83
  let isFlat = req_data.isFlat == true ? true : false;
84
- self.wsManager.send(socket, 'readDocument', { ...req_data, data: isFlat ? encodeObject(tmp) : tmp }, req_data['organization_id'], roomInfo);
84
+ self.wsManager.send(socket, 'readDocument', { ...req_data, data: isFlat ? encodeObject(tmp) : tmp }, req_data['organization_id'], socketInfo);
85
85
  } else {
86
- self.wsManager.send(socket, 'ServerError', error, null, roomInfo);
86
+ self.wsManager.send(socket, 'ServerError', error, null, socketInfo);
87
87
  }
88
88
  });
89
89
  } catch (error) {
90
90
  console.log('readDocument error', error);
91
- self.wsManager.send(socket, 'ServerError', 'error', null, roomInfo);
91
+ self.wsManager.send(socket, 'ServerError', 'error', null, socketInfo);
92
92
  }
93
93
  }
94
94
 
95
95
  /** Update Document **/
96
- async updateDocument(socket, req_data, roomInfo) {
96
+ async updateDocument(socket, req_data, socketInfo) {
97
97
  const self = this;
98
98
  try {
99
99
  const db = this.dbClient.db(req_data['organization_id']);
100
100
  const collection = db.collection(req_data["collection"]);
101
- let objId = new ObjectID();
101
+ let objId = new ObjectId();
102
102
  try {
103
103
  if (req_data["document_id"]) {
104
- objId = new ObjectID(req_data["document_id"]);
104
+ objId = new ObjectId(req_data["document_id"]);
105
105
  }
106
106
  } catch (err) {
107
107
  console.log(err);
@@ -132,10 +132,10 @@ class CoCreateCrud {
132
132
 
133
133
  if(req_data['unset']) response['delete_fields'] = req_data['unset'];
134
134
 
135
- self.broadcast('updateDocument', socket, req_data, response, roomInfo)
135
+ self.broadcast('updateDocument', socket, response, socketInfo)
136
136
  }).catch((error) => {
137
137
  console.log('error', error)
138
- self.wsManager.send(socket, 'ServerError', error, null, roomInfo);
138
+ self.wsManager.send(socket, 'ServerError', error, null, socketInfo);
139
139
  });
140
140
 
141
141
  } catch (error) {
@@ -145,42 +145,32 @@ class CoCreateCrud {
145
145
  }
146
146
 
147
147
  /** Delete Document **/
148
- async deleteDocument(socket, req_data, roomInfo) {
148
+ async deleteDocument(socket, req_data, socketInfo) {
149
149
  const self = this;
150
150
 
151
151
  try {
152
152
  const db = this.dbClient.db(req_data['organization_id']);
153
153
  const collection = db.collection(req_data["collection"]);
154
154
  const query = {
155
- "_id": new ObjectID(req_data["document_id"])
155
+ "_id": new ObjectId(req_data["document_id"])
156
156
  };
157
157
 
158
158
  collection.deleteOne(query, function(error, result) {
159
159
  if (!error) {
160
160
  let response = { ...req_data }
161
- self.broadcast('deleteDocument', socket, req_data, response, roomInfo)
161
+ self.broadcast('deleteDocument', socket, response, socketInfo)
162
162
  } else {
163
- self.wsManager.send(socket, 'ServerError', error, null, roomInfo);
163
+ self.wsManager.send(socket, 'ServerError', error, null, socketInfo);
164
164
  }
165
165
  })
166
166
  } catch (error) {
167
167
  console.log(error);
168
- self.wsManager.send(socket, 'ServerError', 'error', null, roomInfo);
168
+ self.wsManager.send(socket, 'ServerError', 'error', null, socketInfo);
169
169
  }
170
170
  }
171
171
 
172
- broadcast(component, socket, req_data, response, roomInfo) {
173
- if (req_data.broadcast_sender != false) {
174
- this.wsManager.send(socket, component, { ...response, element: req_data['element']}, req_data['organization_id'], roomInfo);
175
- }
176
-
177
- if (req_data.broadcast !== false) {
178
- if (req_data.room) {
179
- this.wsManager.broadcast(socket, req_data.namespace || req_data['organization_id'] , req_data.room, component, response, true, roomInfo);
180
- } else {
181
- this.wsManager.broadcast(socket, req_data.namespace || req_data['organization_id'], null, component, response, true, roomInfo)
182
- }
183
- }
172
+ broadcast(component, socket, response, socketInfo) {
173
+ this.wsManager.broadcast(socket, response.namespace || response['organization_id'], response.room, component, response, socketInfo);
184
174
  process.emit('changed-document', response)
185
175
  }
186
176
  }
package/src/list.js CHANGED
@@ -1,4 +1,4 @@
1
- const {ObjectID} = require("mongodb");
1
+ const {ObjectId} = require("mongodb");
2
2
 
3
3
  class CoCreateList {
4
4
  constructor(wsManager, dbClient) {
@@ -8,8 +8,8 @@ class CoCreateList {
8
8
  }
9
9
 
10
10
  init() {
11
- this.wsManager.on('readDocumentList', (socket, data, roomInfo) => this.readDocumentList(socket, data, roomInfo));
12
- this.wsManager.on('readCollectionList', (socket, data, roomInfo) => this.readCollectionList(socket, data, roomInfo));
11
+ this.wsManager.on('readDocumentList', (socket, data, socketInfo) => this.readDocumentList(socket, data, socketInfo));
12
+ this.wsManager.on('readCollections', (socket, data, socketInfo) => this.readCollections(socket, data, socketInfo));
13
13
  }
14
14
 
15
15
  /**
@@ -40,26 +40,23 @@ class CoCreateList {
40
40
  count: 0 (integer)
41
41
  },
42
42
 
43
- is_collection: true | false,
44
43
  //. case fetch document case
45
44
  export: true | false
46
45
  -------- additional response data -----------
47
46
  data: [] // array
48
47
  }
49
48
  **/
50
- async readDocumentList(socket, req_data, roomInfo) {
49
+ async readDocumentList(socket, req_data, socketInfo) {
51
50
  function sleep(ms) {
52
51
  return new Promise((resolve) => {
53
52
  setTimeout(resolve, ms);
54
53
  });
55
54
  }
56
-
57
- // await sleep(3000)
58
55
 
59
56
  const self = this;
60
57
 
61
58
  if (req_data['is_collection']) {
62
- var result = await this.readCollectionList(socket, req_data, roomInfo);
59
+ var result = await this.readCollections(socket, req_data, socketInfo);
63
60
  return;
64
61
  }
65
62
 
@@ -116,42 +113,33 @@ class CoCreateList {
116
113
 
117
114
  result_data = result;
118
115
  }
119
- self.wsManager.send(socket, 'readDocumentList', { ...req_data, data: result_data, operator: {...operator, total: total}}, req_data['organization_id'], roomInfo);
116
+ self.wsManager.send(socket, 'readDocumentList', { ...req_data, data: result_data, operator: {...operator, total: total}}, req_data['organization_id'], socketInfo);
120
117
  } else {
121
118
  console.log(error)
122
- self.wsManager.send(socket, 'ServerError', error, null, roomInfo);
119
+ self.wsManager.send(socket, 'ServerError', error, null, socketInfo);
123
120
  }
124
121
  })
125
122
  } catch (error) {
126
123
  console.log('readDocumentList error', error);
127
- this.wsManager.send(socket, 'ServerError', 'error', null, roomInfo);
124
+ this.wsManager.send(socket, 'ServerError', 'error', null, socketInfo);
128
125
  }
129
126
  }
130
127
 
131
- async readCollectionList(socket, data, roomInfo) {
128
+ async readCollections(socket, data, socketInfo) {
132
129
  try {
133
- var collections = [];
134
- const db = this.dbClient.db(req_data['organization_id']);
135
- collections = await db.listCollections().toArray().then(infos => {
136
- var result = [];
137
- infos.forEach(function(item) {
138
- let __uuid = item.info.uuid.toString('hex')
139
- result.push({
140
- name: item.name,
141
- _id: __uuid,
142
- id: __uuid,
143
- });
144
- })
145
- return result;
146
- })
147
-
148
- this.wsManager.send(socket, 'readCollectionList', {...data, data: collections }, data['organization_id'], roomInfo);
130
+ const self = this;
131
+ const db = this.dbClient.db(data['organization_id']);
132
+ db.listCollections().toArray(function(error, result) {
133
+ if (!error && result && result.length > 0) {
134
+ self.wsManager.send(socket, 'readCollections', {...data, data: result }, data['organization_id'], socketInfo);
135
+ }
136
+ })
149
137
  } catch(error) {
150
- this.wsManager.send(socket, 'ServerError', 'error', null, roomInfo);
138
+ console.log('readCollections error', error);
139
+ this.wsManager.send(socket, 'ServerError', 'error', null, socketInfo);
151
140
  }
152
141
  }
153
142
 
154
-
155
143
  /**
156
144
  * function that make query from data
157
145
  *
@@ -170,7 +158,7 @@ class CoCreateList {
170
158
  query[key] = {};
171
159
  }
172
160
 
173
- if (item.name == "_id") item.value = item.value.map(v => new ObjectID(v))
161
+ if (item.name == "_id") item.value = item.value.map(v => new ObjectId(v))
174
162
 
175
163
  switch (item.operator) {
176
164
  case '$contain':