@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 +7 -0
- package/package.json +1 -1
- package/src/backup.js +141 -143
- package/src/index.js +45 -45
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
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
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
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,
|
|
6
|
+
constructor(wsManager, databases, db) {
|
|
7
7
|
this.wsManager = wsManager
|
|
8
8
|
this.databases = databases
|
|
9
|
-
this.
|
|
9
|
+
this.db = db
|
|
10
10
|
this.ObjectId = ObjectId
|
|
11
|
-
this.
|
|
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.
|
|
18
|
-
this.wsManager.on('readDatabase', (socket, data) => this.
|
|
19
|
-
this.wsManager.on('updateDatabase', (socket, data) => this.
|
|
20
|
-
this.wsManager.on('deleteDatabase', (socket, data) => this.
|
|
21
|
-
this.wsManager.on('createCollection', (socket, data) => this.
|
|
22
|
-
this.wsManager.on('readCollection', (socket, data) => this.
|
|
23
|
-
this.wsManager.on('updateCollection', (socket, data) => this.
|
|
24
|
-
this.wsManager.on('deleteCollection', (socket, data) => this.
|
|
25
|
-
this.wsManager.on('createDocument', (socket, data) => this.
|
|
26
|
-
this.wsManager.on('readDocument', (socket, data) => this.
|
|
27
|
-
this.wsManager.on('updateDocument', (socket, data) => this.
|
|
28
|
-
this.wsManager.on('deleteDocument', (socket, data) => this.
|
|
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.
|
|
33
|
+
data = await this.crud('', 'databaseStats', data)
|
|
34
34
|
return data
|
|
35
35
|
}
|
|
36
36
|
|
|
37
37
|
async createDatabase(data) {
|
|
38
|
-
data = await this.
|
|
38
|
+
data = await this.crud('', 'createDatabase', data)
|
|
39
39
|
return data
|
|
40
40
|
}
|
|
41
41
|
|
|
42
42
|
async readDatabase(data) {
|
|
43
|
-
data = await this.
|
|
43
|
+
data = await this.crud('', 'readDatabase', data)
|
|
44
44
|
return data
|
|
45
45
|
}
|
|
46
46
|
|
|
47
47
|
async updateDatabase(data) {
|
|
48
|
-
data = await this.
|
|
48
|
+
data = await this.crud('', 'updateDatabase', data)
|
|
49
49
|
return data
|
|
50
50
|
}
|
|
51
51
|
|
|
52
52
|
async deleteDatabase(data) {
|
|
53
|
-
data = await this.
|
|
53
|
+
data = await this.crud('', 'deleteDatabase', data)
|
|
54
54
|
return data
|
|
55
55
|
}
|
|
56
56
|
|
|
57
57
|
async createCollection(data) {
|
|
58
|
-
data = await this.
|
|
58
|
+
data = await this.crud('', 'createCollection', data)
|
|
59
59
|
return data
|
|
60
60
|
}
|
|
61
61
|
|
|
62
62
|
async readCollection(data) {
|
|
63
|
-
data = await this.
|
|
63
|
+
data = await this.crud('', 'readCollection', data)
|
|
64
64
|
return data
|
|
65
65
|
}
|
|
66
66
|
|
|
67
67
|
async updateCollection(data) {
|
|
68
|
-
data = await this.
|
|
68
|
+
data = await this.crud('', 'updateCollection', data)
|
|
69
69
|
return data
|
|
70
70
|
}
|
|
71
71
|
|
|
72
72
|
async deleteCollection(data) {
|
|
73
|
-
data = await this.
|
|
73
|
+
data = await this.crud('', 'deleteCollection', data)
|
|
74
74
|
return data
|
|
75
75
|
}
|
|
76
76
|
|
|
77
77
|
async createDocument(data) {
|
|
78
|
-
data = await this.
|
|
78
|
+
data = await this.crud('', 'createDocument', data)
|
|
79
79
|
return data
|
|
80
80
|
}
|
|
81
81
|
|
|
82
82
|
async readDocument(data) {
|
|
83
|
-
data = await this.
|
|
83
|
+
data = await this.crud('', 'readDocument', data)
|
|
84
84
|
return data
|
|
85
85
|
}
|
|
86
86
|
|
|
87
87
|
async updateDocument(data) {
|
|
88
|
-
data = await this.
|
|
88
|
+
data = await this.crud('', 'updateDocument', data)
|
|
89
89
|
return data
|
|
90
90
|
}
|
|
91
91
|
|
|
92
92
|
async deleteDocument(data) {
|
|
93
|
-
data = await this.
|
|
93
|
+
data = await this.crud('', 'deleteDocument', data)
|
|
94
94
|
return data
|
|
95
95
|
}
|
|
96
96
|
|
|
97
|
-
async
|
|
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.
|
|
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 =
|
|
110
|
-
this.
|
|
109
|
+
dbUrl = this.db
|
|
110
|
+
this.dbUrls.set(data.organization_id, dbUrl)
|
|
111
111
|
} else {
|
|
112
|
-
let organization = await this.
|
|
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.
|
|
122
|
+
this.dbUrls.set(data.organization_id, dbUrl)
|
|
124
123
|
} else {
|
|
125
|
-
this.
|
|
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
|
|
167
|
+
this[action](platformUpdate)
|
|
170
168
|
}
|
|
171
169
|
|
|
172
170
|
}
|
|
173
171
|
|
|
174
172
|
}
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
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.
|
|
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.
|
|
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 {
|