@cocreate/crud-server 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-crud-server/compare/v1.21.1...v1.22.0) (2023-05-24)
2
+
3
+
4
+ ### Features
5
+
6
+ * Supports multiple databases to be used independently or as a mesh. ([32e0897](https://github.com/CoCreate-app/CoCreate-crud-server/commit/32e08973f4b8cb1f5bf031417adc683e9d00ce9e))
7
+
1
8
  ## [1.21.1](https://github.com/CoCreate-app/CoCreate-crud-server/compare/v1.21.0...v1.21.1) (2023-05-20)
2
9
 
3
10
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/crud-server",
3
- "version": "1.21.1",
3
+ "version": "1.22.0",
4
4
  "description": "CoCreate-crud-server",
5
5
  "keywords": [
6
6
  "cocreate-crud",
package/src/backup.js CHANGED
@@ -2,149 +2,147 @@ const json2csv = require("json-2-csv")
2
2
  const csvtojson = require("csvtojson");
3
3
 
4
4
  class CoCreateBackup {
5
- constructor(wsManager, dbClient) {
6
- this.wsManager = wsManager
7
- this.dbClient = dbClient
8
- this.importCollection = '';
9
- this.importType = '';
10
- this.importDB = '';
11
- this.init();
12
- }
13
-
14
- init() {
15
- if (this.wsManager) {
16
- // this.wsManager.on('exportDB', (socket, data) => this.exportData(socket, data));
17
- // this.wsManager.on('importDB', (socket, data) => this.importData(socket, data));
18
- // this.wsManager.on('importFile2DB', (socket, data) => this.importData(socket, data));
19
-
20
- // this.wsManager.on('downloadData', (socket, data) => this.downloadData(socket, data))
21
- }
22
- }
23
-
24
- /**
25
- * data: {
26
- collection: '',
27
- type: 'csv/json'
28
- data: JSON data
29
- }
30
- **/
31
- // async downloadData(socket, data) {
32
- // const export_type = data.type || "json";
33
-
34
- // try {
35
- // let binaryData = null;
36
- // const result = data.data;
37
- // const organization_id = socket.config.organization_id ;
38
- // if (export_type === 'csv') {
39
- // binaryData = await json2csv.json2csvAsync(JSON.parse(JSON.stringify(result)), {
40
- // emptyFieldValue: ''
41
- // });
42
- // } else {
43
- // binaryData = Buffer.from(JSON.stringify(result));
44
- // }
45
-
46
- // this.wsManager.send(socket, 'downloadFileInfo', {file_name: `backup_${data['collection']}.${export_type}`, binaryData, export_type}, organization_id);
47
-
48
- // this.wsManager.sendBinary(socket, binaryData, organization_id);
49
-
50
- // } catch (error) {
51
- // console.log('export error', error);
52
- // }
53
- // }
54
-
55
- /**
56
- CoCreateSocket.exportData({
57
- collection: '',
58
- db: '',
59
- export_type: json/csv,
60
-
61
- })
62
- **/
63
- // async exportData(socket, data) {
64
- // const self = this;
65
-
66
- // const export_type = data.export_type || "json";
67
-
68
- // try {
69
-
70
- // var collection = this.dbClient.db(data['namespace']).collection(data["collection"]);
71
- // const organization_id = socket.config.organization_id
72
-
73
- // var query = {};
74
-
75
- // collection.find(query).toArray(async function(error, result) {
76
- // if (!error) {
77
- // let binaryData = null;
78
- // self.wsManager.send(socket, 'downloadFileInfo', {file_name: `backup_${data['collection']}.${export_type}`}, organization_id);
79
- // if (export_type === 'csv') {
80
- // binaryData = await json2csv.json2csvAsync(JSON.parse(JSON.stringify(result)), {
81
- // emptyFieldValue: ''
82
- // });
83
- // } else {
84
- // binaryData = Buffer.from(JSON.stringify(result));
85
- // }
86
-
87
- // self.wsManager.sendBinary(socket, binaryData, organization_id);
88
- // }
89
- // });
90
- // } catch (error) {
91
- // console.log('export error', error);
92
- // }
93
- // }
94
-
95
- // async setImportData(socket, data) {
96
- // this.importCollection = data['collection']
97
- // this.importType = data['import_type'];
98
- // this.importDB = data['namespace'];
99
- // }
100
-
101
- // async importData(socket, data) {
102
- // const importCollection = data['collection']
103
- // const importType = data['import_type'];
104
- // const importFile = data['file'];
105
- // // const importDB = data['namespace'];
106
-
107
- // console.log('import:', importCollection, importType, importFile)
108
- // const self = this;
109
- // // const organization_id = socket.config.organization_id
110
- // if (!importCollection || !importType) {
111
- // return;
112
- // }
113
- // try {
114
- // console.log('data: ', data)
115
- // // return;
116
- // let jsonData = null;
117
- // if (this.importType === 'csv') {
118
- // jsonData = await csvtojson({ignoreEmpty: true}).fromString(data.toString())
119
- // }
120
- // else {
121
- // jsonData = JSON.parse(importFile.toString());
122
- // }
123
- // // TODO: validate json / if json is object error happens
124
- // jsonData.map((item) => delete item._id);
125
- // console.log('json: ', jsonData)
126
- // var collection = this.dbClient.db(organization_id).collection(importCollection);
127
- // // console.log(this.importCollection)
128
- // collection.insertMany(jsonData, function(err, result) {
129
- // if (!err) {
130
- // self.wsManager.send(socket, 'importedFile2DB', {
131
- // 'database': organization_id,
132
- // 'collection': importCollection,
133
- // 'import_type': importType,
134
- // 'data': result
135
- // }, organization_id)
136
- // }
137
- // })
138
-
139
- // // this.importCollection = '';
140
- // // this.importType = '';
141
-
142
- // } catch (error) {
143
- // console.log('import db error', error)
144
- // }
145
-
146
- // }
147
-
5
+ constructor(wsManager, dbClient) {
6
+ this.wsManager = wsManager
7
+ this.dbClient = dbClient
8
+ this.importCollection = '';
9
+ this.importType = '';
10
+ this.importDB = '';
11
+ this.init();
12
+ }
13
+
14
+ init() {
15
+ if (this.wsManager) {
16
+ // this.wsManager.on('exportDB', (socket, data) => this.exportData(socket, data));
17
+ // this.wsManager.on('importDB', (socket, data) => this.importData(socket, data));
18
+ // this.wsManager.on('downloadData', (socket, data) => this.downloadData(socket, data))
19
+ }
20
+ }
21
+
22
+ /**
23
+ * data: {
24
+ collection: '',
25
+ type: 'csv/json'
26
+ data: JSON data
27
+ }
28
+ **/
29
+ // async downloadData(socket, data) {
30
+ // const export_type = data.type || "json";
31
+
32
+ // try {
33
+ // let binaryData = null;
34
+ // const result = data.data;
35
+ // const organization_id = socket.config.organization_id ;
36
+ // if (export_type === 'csv') {
37
+ // binaryData = await json2csv.json2csvAsync(JSON.parse(JSON.stringify(result)), {
38
+ // emptyFieldValue: ''
39
+ // });
40
+ // } else {
41
+ // binaryData = Buffer.from(JSON.stringify(result));
42
+ // }
43
+
44
+ // this.wsManager.send(socket, 'downloadFileInfo', {file_name: `backup_${data['collection']}.${export_type}`, binaryData, export_type}, organization_id);
45
+
46
+ // this.wsManager.sendBinary(socket, binaryData, organization_id);
47
+
48
+ // } catch (error) {
49
+ // console.log('export error', error);
50
+ // }
51
+ // }
52
+
53
+ /**
54
+ CoCreateSocket.exportData({
55
+ collection: '',
56
+ db: '',
57
+ export_type: json/csv,
58
+
59
+ })
60
+ **/
61
+ // async exportData(socket, data) {
62
+ // const self = this;
63
+
64
+ // const export_type = data.export_type || "json";
65
+
66
+ // try {
67
+
68
+ // var collection = this.dbClient.db(data['namespace']).collection(data["collection"]);
69
+ // const organization_id = socket.config.organization_id
70
+
71
+ // var query = {};
72
+
73
+ // collection.find(query).toArray(async function(error, result) {
74
+ // if (!error) {
75
+ // let binaryData = null;
76
+ // self.wsManager.send(socket, 'downloadFileInfo', {file_name: `backup_${data['collection']}.${export_type}`}, organization_id);
77
+ // if (export_type === 'csv') {
78
+ // binaryData = await json2csv.json2csvAsync(JSON.parse(JSON.stringify(result)), {
79
+ // emptyFieldValue: ''
80
+ // });
81
+ // } else {
82
+ // binaryData = Buffer.from(JSON.stringify(result));
83
+ // }
84
+
85
+ // self.wsManager.sendBinary(socket, binaryData, organization_id);
86
+ // }
87
+ // });
88
+ // } catch (error) {
89
+ // console.log('export error', error);
90
+ // }
91
+ // }
92
+
93
+ // async setImportData(socket, data) {
94
+ // this.importCollection = data['collection']
95
+ // this.importType = data['import_type'];
96
+ // this.importDB = data['namespace'];
97
+ // }
98
+
99
+ // async importData(socket, data) {
100
+ // const importCollection = data['collection']
101
+ // const importType = data['import_type'];
102
+ // const importFile = data['file'];
103
+ // // const importDB = data['namespace'];
104
+
105
+ // console.log('import:', importCollection, importType, importFile)
106
+ // const self = this;
107
+ // // const organization_id = socket.config.organization_id
108
+ // if (!importCollection || !importType) {
109
+ // return;
110
+ // }
111
+ // try {
112
+ // console.log('data: ', data)
113
+ // // return;
114
+ // let jsonData = null;
115
+ // if (this.importType === 'csv') {
116
+ // jsonData = await csvtojson({ignoreEmpty: true}).fromString(data.toString())
117
+ // }
118
+ // else {
119
+ // jsonData = JSON.parse(importFile.toString());
120
+ // }
121
+ // // TODO: validate json / if json is object error happens
122
+ // jsonData.map((item) => delete item._id);
123
+ // console.log('json: ', jsonData)
124
+ // var collection = this.dbClient.db(organization_id).collection(importCollection);
125
+ // // console.log(this.importCollection)
126
+ // collection.insertMany(jsonData, function(err, result) {
127
+ // if (!err) {
128
+ // self.wsManager.send(socket, 'importedFile2DB', {
129
+ // 'database': organization_id,
130
+ // 'collection': importCollection,
131
+ // 'import_type': importType,
132
+ // 'data': result
133
+ // }, organization_id)
134
+ // }
135
+ // })
136
+
137
+ // // this.importCollection = '';
138
+ // // this.importType = '';
139
+
140
+ // } catch (error) {
141
+ // console.log('import db error', error)
142
+ // }
143
+
144
+ // }
145
+
148
146
  }
149
147
  module.exports = CoCreateBackup;
150
148
 
package/src/index.js CHANGED
@@ -3,114 +3,113 @@
3
3
  const { ObjectId, searchData, sortData } = require("@cocreate/utils");
4
4
 
5
5
  class CoCreateCrudServer {
6
- constructor(wsManager, databases, database) {
6
+ constructor(wsManager, databases, db) {
7
7
  this.wsManager = wsManager
8
8
  this.databases = databases
9
- this.database = database
9
+ this.db = db
10
10
  this.ObjectId = ObjectId
11
- this.databaseUrls = new Map();
11
+ this.dbUrls = new Map();
12
12
  this.init();
13
13
  }
14
14
 
15
15
  init() {
16
16
  if (this.wsManager) {
17
- this.wsManager.on('createDatabase', (socket, data) => this.db(socket, 'createDatabase', data))
18
- this.wsManager.on('readDatabase', (socket, data) => this.db(socket, 'readDatabase', data))
19
- this.wsManager.on('updateDatabase', (socket, data) => this.db(socket, 'updateDatabase', data))
20
- this.wsManager.on('deleteDatabase', (socket, data) => this.db(socket, 'deleteDatabase', data))
21
- this.wsManager.on('createCollection', (socket, data) => this.db(socket, 'createCollection', data))
22
- this.wsManager.on('readCollection', (socket, data) => this.db(socket, 'readCollection', data))
23
- this.wsManager.on('updateCollection', (socket, data) => this.db(socket, 'updateCollection', data))
24
- this.wsManager.on('deleteCollection', (socket, data) => this.db(socket, 'deleteCollection', data))
25
- this.wsManager.on('createDocument', (socket, data) => this.db(socket, 'createDocument', data))
26
- this.wsManager.on('readDocument', (socket, data) => this.db(socket, 'readDocument', data))
27
- this.wsManager.on('updateDocument', (socket, data) => this.db(socket, 'updateDocument', data))
28
- this.wsManager.on('deleteDocument', (socket, data) => this.db(socket, 'deleteDocument', data))
17
+ this.wsManager.on('createDatabase', (socket, data) => this.crud(socket, 'createDatabase', data))
18
+ this.wsManager.on('readDatabase', (socket, data) => this.crud(socket, 'readDatabase', data))
19
+ this.wsManager.on('updateDatabase', (socket, data) => this.crud(socket, 'updateDatabase', data))
20
+ this.wsManager.on('deleteDatabase', (socket, data) => this.crud(socket, 'deleteDatabase', data))
21
+ this.wsManager.on('createCollection', (socket, data) => this.crud(socket, 'createCollection', data))
22
+ this.wsManager.on('readCollection', (socket, data) => this.crud(socket, 'readCollection', data))
23
+ this.wsManager.on('updateCollection', (socket, data) => this.crud(socket, 'updateCollection', data))
24
+ this.wsManager.on('deleteCollection', (socket, data) => this.crud(socket, 'deleteCollection', data))
25
+ this.wsManager.on('createDocument', (socket, data) => this.crud(socket, 'createDocument', data))
26
+ this.wsManager.on('readDocument', (socket, data) => this.crud(socket, 'readDocument', data))
27
+ this.wsManager.on('updateDocument', (socket, data) => this.crud(socket, 'updateDocument', data))
28
+ this.wsManager.on('deleteDocument', (socket, data) => this.crud(socket, 'deleteDocument', data))
29
29
  }
30
30
  }
31
31
 
32
32
  async databaseStats(data) {
33
- data = await this.db('', 'databaseStats', data)
33
+ data = await this.crud('', 'databaseStats', data)
34
34
  return data
35
35
  }
36
36
 
37
37
  async createDatabase(data) {
38
- data = await this.db('', 'createDatabase', data)
38
+ data = await this.crud('', 'createDatabase', data)
39
39
  return data
40
40
  }
41
41
 
42
42
  async readDatabase(data) {
43
- data = await this.db('', 'readDatabase', data)
43
+ data = await this.crud('', 'readDatabase', data)
44
44
  return data
45
45
  }
46
46
 
47
47
  async updateDatabase(data) {
48
- data = await this.db('', 'updateDatabase', data)
48
+ data = await this.crud('', 'updateDatabase', data)
49
49
  return data
50
50
  }
51
51
 
52
52
  async deleteDatabase(data) {
53
- data = await this.db('', 'deleteDatabase', data)
53
+ data = await this.crud('', 'deleteDatabase', data)
54
54
  return data
55
55
  }
56
56
 
57
57
  async createCollection(data) {
58
- data = await this.db('', 'createCollection', data)
58
+ data = await this.crud('', 'createCollection', data)
59
59
  return data
60
60
  }
61
61
 
62
62
  async readCollection(data) {
63
- data = await this.db('', 'readCollection', data)
63
+ data = await this.crud('', 'readCollection', data)
64
64
  return data
65
65
  }
66
66
 
67
67
  async updateCollection(data) {
68
- data = await this.db('', 'updateCollection', data)
68
+ data = await this.crud('', 'updateCollection', data)
69
69
  return data
70
70
  }
71
71
 
72
72
  async deleteCollection(data) {
73
- data = await this.db('', 'deleteCollection', data)
73
+ data = await this.crud('', 'deleteCollection', data)
74
74
  return data
75
75
  }
76
76
 
77
77
  async createDocument(data) {
78
- data = await this.db('', 'createDocument', data)
78
+ data = await this.crud('', 'createDocument', data)
79
79
  return data
80
80
  }
81
81
 
82
82
  async readDocument(data) {
83
- data = await this.db('', 'readDocument', data)
83
+ data = await this.crud('', 'readDocument', data)
84
84
  return data
85
85
  }
86
86
 
87
87
  async updateDocument(data) {
88
- data = await this.db('', 'updateDocument', data)
88
+ data = await this.crud('', 'updateDocument', data)
89
89
  return data
90
90
  }
91
91
 
92
92
  async deleteDocument(data) {
93
- data = await this.db('', 'deleteDocument', data)
93
+ data = await this.crud('', 'deleteDocument', data)
94
94
  return data
95
95
  }
96
96
 
97
- async db(socket, action, data) {
97
+ async crud(socket, action, data) {
98
98
  return new Promise(async (resolve) => {
99
99
  try {
100
100
  if (!data.organization_id)
101
101
  return resolve()
102
102
 
103
- let dbUrl = this.databaseUrls.get(data.organization_id)
103
+ let dbUrl = this.dbUrls.get(data.organization_id)
104
104
  if (dbUrl === false)
105
105
  return resolve({ dbUrl: false, error: 'database url could not be found' })
106
106
 
107
107
  if (!dbUrl) {
108
108
  if (data.organization_id === process.env.organization_id) {
109
- dbUrl = { [this.database.name]: this.database }
110
- this.databaseUrls.set(data.organization_id, dbUrl)
109
+ dbUrl = this.db
110
+ this.dbUrls.set(data.organization_id, dbUrl)
111
111
  } else {
112
- let organization = await this.databases[this.database.name]['readDocument']({
113
- dbUrl: this.database.url[0],
112
+ let organization = await this.readDocument({
114
113
  database: process.env.organization_id,
115
114
  collection: 'organizations',
116
115
  document: [{ _id: data.organization_id }],
@@ -120,9 +119,9 @@ class CoCreateCrudServer {
120
119
  organization = organization.document[0]
121
120
  if (organization && organization.databases) {
122
121
  dbUrl = organization.databases
123
- this.databaseUrls.set(data.organization_id, dbUrl)
122
+ this.dbUrls.set(data.organization_id, dbUrl)
124
123
  } else {
125
- this.databaseUrls.set(data.organization_id, false)
124
+ this.dbUrls.set(data.organization_id, false)
126
125
  if (organization)
127
126
  return resolve({ dbUrl: false, error: 'database url could not be found' })
128
127
  else
@@ -151,7 +150,6 @@ class CoCreateCrudServer {
151
150
 
152
151
  if (syncKeys && syncKeys.length) {
153
152
  let platformUpdate = {
154
- dbUrl: this.database.url,
155
153
  database: process.env.organization_id,
156
154
  collection: data.collection,
157
155
  document: [{}],
@@ -166,26 +164,28 @@ class CoCreateCrudServer {
166
164
  }
167
165
  }
168
166
 
169
- this.databases[this.database.name][action](platformUpdate)
167
+ this[action](platformUpdate)
170
168
  }
171
169
 
172
170
  }
173
171
 
174
172
  }
175
- if (!data.db || !data.db.length)
176
- data.db = ['mongodb']
177
- else if (!Array.isArray(data.db))
173
+
174
+ if (!data.db || !data.db.length) {
175
+ data.db = [Object.keys(dbUrl)[0]]
176
+ } else if (!Array.isArray(data.db))
178
177
  data.db = [data.db]
179
178
 
180
179
  for (let i = 0; i < data.db.length; i++) {
181
180
  if (dbUrl && dbUrl[data.db[i]]) {
182
181
  let db = dbUrl[data.db[i]]
183
182
 
184
- if (db.name && this.databases[db.name]) {
185
-
183
+ if (db.provider && this.databases[db.provider]) {
184
+ if (!Array.isArray(db.url))
185
+ db.url = [db.url]
186
186
  for (let i = 0; i < db.url.length; i++) {
187
187
  data['dbUrl'] = db.url[i]
188
- data = await this.databases[db.name][action](data)
188
+ data = await this.databases[db.provider][action](data)
189
189
  }
190
190
 
191
191
  //TODO: sorting should take place here in order to return sorted values from multiple dbs
@@ -212,7 +212,7 @@ class CoCreateCrudServer {
212
212
  }
213
213
  } catch (error) {
214
214
  if (socket) {
215
- errorHandler(data, error)
215
+ this.errorHandler(data, error)
216
216
  this.wsManager.send(socket, action, data);
217
217
  resolve()
218
218
  } else {