@cocreate/crud-server 1.20.4 → 1.21.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 +20 -0
- package/docs/index.html +1 -1
- package/package.json +1 -1
- package/src/backup.js +1 -1
- package/src/index.js +226 -216
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,23 @@
|
|
|
1
|
+
# [1.21.0](https://github.com/CoCreate-app/CoCreate-crud-server/compare/v1.20.5...v1.21.0) (2023-05-19)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* wsManager listeners & add async/await consistency ([f8b21be](https://github.com/CoCreate-app/CoCreate-crud-server/commit/f8b21beafa41888ea85c16391880fb1bfce10231))
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Features
|
|
10
|
+
|
|
11
|
+
* Refactor code to ensure data.db is always an Array ([ce76271](https://github.com/CoCreate-app/CoCreate-crud-server/commit/ce76271518a0b7525e6dbbd80d7dc25e8184692d))
|
|
12
|
+
* update data.organization_id if previously transformed to platform_id ([bb95274](https://github.com/CoCreate-app/CoCreate-crud-server/commit/bb9527425ee5cb13c81933a658a6a732ac306407))
|
|
13
|
+
|
|
14
|
+
## [1.20.5](https://github.com/CoCreate-app/CoCreate-crud-server/compare/v1.20.4...v1.20.5) (2023-05-11)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
### Bug Fixes
|
|
18
|
+
|
|
19
|
+
* replace key ([84d197d](https://github.com/CoCreate-app/CoCreate-crud-server/commit/84d197d316beb82a92e73769c7d145b76ea0ac83))
|
|
20
|
+
|
|
1
21
|
## [1.20.4](https://github.com/CoCreate-app/CoCreate-crud-server/compare/v1.20.3...v1.20.4) (2023-05-10)
|
|
2
22
|
|
|
3
23
|
|
package/docs/index.html
CHANGED
package/package.json
CHANGED
package/src/backup.js
CHANGED
|
@@ -120,7 +120,7 @@ class CoCreateBackup {
|
|
|
120
120
|
// else {
|
|
121
121
|
// jsonData = JSON.parse(importFile.toString());
|
|
122
122
|
// }
|
|
123
|
-
// //
|
|
123
|
+
// // TODO: validate json / if json is object error happens
|
|
124
124
|
// jsonData.map((item) => delete item._id);
|
|
125
125
|
// console.log('json: ', jsonData)
|
|
126
126
|
// var collection = this.dbClient.db(organization_id).collection(importCollection);
|
package/src/index.js
CHANGED
|
@@ -1,231 +1,241 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const {ObjectId, searchData, sortData} = require("@cocreate/utils");
|
|
3
|
+
const { ObjectId, searchData, sortData } = require("@cocreate/utils");
|
|
4
4
|
|
|
5
5
|
class CoCreateCrudServer {
|
|
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
|
-
|
|
6
|
+
constructor(wsManager, databases, database) {
|
|
7
|
+
this.wsManager = wsManager
|
|
8
|
+
this.databases = databases
|
|
9
|
+
this.database = database
|
|
10
|
+
this.ObjectId = ObjectId
|
|
11
|
+
this.databaseUrls = new Map();
|
|
12
|
+
this.init();
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
init() {
|
|
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))
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async databaseStats(data) {
|
|
33
|
+
data = await this.db('', 'databaseStats', data)
|
|
34
|
+
return data
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
async createDatabase(data) {
|
|
38
|
+
data = await this.db('', 'createDatabase', data)
|
|
39
|
+
return data
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async readDatabase(data) {
|
|
43
|
+
data = await this.db('', 'readDatabase', data)
|
|
44
|
+
return data
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
async updateDatabase(data) {
|
|
48
|
+
data = await this.db('', 'updateDatabase', data)
|
|
49
|
+
return data
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async deleteDatabase(data) {
|
|
53
|
+
data = await this.db('', 'deleteDatabase', data)
|
|
54
|
+
return data
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async createCollection(data) {
|
|
58
|
+
data = await this.db('', 'createCollection', data)
|
|
59
|
+
return data
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async readCollection(data) {
|
|
63
|
+
data = await this.db('', 'readCollection', data)
|
|
64
|
+
return data
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
async updateCollection(data) {
|
|
68
|
+
data = await this.db('', 'updateCollection', data)
|
|
69
|
+
return data
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
async deleteCollection(data) {
|
|
73
|
+
data = await this.db('', 'deleteCollection', data)
|
|
74
|
+
return data
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async createDocument(data) {
|
|
78
|
+
data = await this.db('', 'createDocument', data)
|
|
79
|
+
return data
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
async readDocument(data) {
|
|
83
|
+
data = await this.db('', 'readDocument', data)
|
|
84
|
+
return data
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
async updateDocument(data) {
|
|
88
|
+
data = await this.db('', 'updateDocument', data)
|
|
89
|
+
return data
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async deleteDocument(data) {
|
|
93
|
+
data = await this.db('', 'deleteDocument', data)
|
|
94
|
+
return data
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
async db(socket, action, data) {
|
|
98
|
+
return new Promise(async (resolve) => {
|
|
99
|
+
try {
|
|
100
|
+
if (!data.organization_id)
|
|
101
|
+
return resolve()
|
|
102
|
+
|
|
103
|
+
let dbUrl = this.databaseUrls.get(data.organization_id)
|
|
104
|
+
if (dbUrl === false)
|
|
105
|
+
return resolve({ dbUrl: false, error: 'database url could not be found' })
|
|
106
|
+
|
|
107
|
+
if (!dbUrl) {
|
|
108
|
+
if (data.organization_id === process.env.organization_id) {
|
|
109
|
+
dbUrl = { [this.database.name]: this.database }
|
|
110
|
+
this.databaseUrls.set(data.organization_id, dbUrl)
|
|
111
|
+
} else {
|
|
112
|
+
let organization = await this.databases[this.database.name]['readDocument']({
|
|
113
|
+
dbUrl: this.database.url[0],
|
|
114
|
+
database: process.env.organization_id,
|
|
115
|
+
collection: 'organizations',
|
|
116
|
+
document: [{ _id: data.organization_id }],
|
|
117
|
+
organization_id: process.env.organization_id
|
|
118
|
+
})
|
|
119
|
+
if (organization && organization.document && organization.document[0])
|
|
120
|
+
organization = organization.document[0]
|
|
121
|
+
if (organization && organization.databases) {
|
|
122
|
+
dbUrl = organization.databases
|
|
123
|
+
this.databaseUrls.set(data.organization_id, dbUrl)
|
|
124
|
+
} else {
|
|
125
|
+
this.databaseUrls.set(data.organization_id, false)
|
|
126
|
+
if (organization)
|
|
127
|
+
return resolve({ dbUrl: false, error: 'database url could not be found' })
|
|
128
|
+
else
|
|
129
|
+
return resolve({ organization: false, error: 'organization could not be found' })
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
if (!data['timeStamp'])
|
|
135
|
+
data['timeStamp'] = new Date().toISOString()
|
|
136
136
|
|
|
137
137
|
if (action == 'updateDocument' && data.upsert != false)
|
|
138
138
|
data.upsert = true
|
|
139
|
-
|
|
140
|
-
|
|
139
|
+
|
|
140
|
+
// TODO: support stats from multiple dbs
|
|
141
141
|
if (data.collection || action === 'databaseStats') {
|
|
142
142
|
if (!data.database)
|
|
143
143
|
data['database'] = data.organization_id
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
144
|
+
|
|
145
|
+
if (action === 'updateDocument' && data.organization_id !== process.env.organization_id) {
|
|
146
|
+
let syncKeys
|
|
147
|
+
if (data.collection === 'organizations')
|
|
148
|
+
syncKeys = ['name', 'logo', 'databases', 'hosts', 'apis']
|
|
149
|
+
else if (data.collection === 'users')
|
|
150
|
+
syncKeys = ['name', 'email', 'password', 'avatar']
|
|
151
|
+
|
|
152
|
+
if (syncKeys && syncKeys.length) {
|
|
153
|
+
let platformUpdate = {
|
|
154
|
+
dbUrl: this.database.url,
|
|
155
|
+
database: process.env.organization_id,
|
|
156
|
+
collection: data.collection,
|
|
157
|
+
document: [{}],
|
|
158
|
+
organization_id: process.env.organization_id
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
let document = data.document[0] || data.document
|
|
162
|
+
if (document) {
|
|
163
|
+
for (let key of syncKeys) {
|
|
164
|
+
if (document[key])
|
|
165
|
+
platformUpdate.document[0][key] = document[key]
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
this.databases[this.database.name][action](platformUpdate)
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
}
|
|
175
|
+
if (!data.db || !data.db.length)
|
|
176
|
+
data.db = ['mongodb']
|
|
177
|
+
else if (!Array.isArray(data.db))
|
|
178
|
+
data.db = [data.db]
|
|
179
|
+
|
|
180
|
+
for (let i = 0; i < data.db.length; i++) {
|
|
181
|
+
if (dbUrl && dbUrl[data.db[i]]) {
|
|
182
|
+
let db = dbUrl[data.db[i]]
|
|
183
|
+
|
|
184
|
+
if (db.name && this.databases[db.name]) {
|
|
185
|
+
|
|
186
|
+
for (let i = 0; i < db.url.length; i++) {
|
|
187
|
+
data['dbUrl'] = db.url[i]
|
|
188
|
+
data = await this.databases[db.name][action](data)
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
//TODO: sorting should take place here in order to return sorted values from multiple dbs
|
|
192
|
+
}
|
|
193
|
+
}
|
|
174
194
|
}
|
|
175
195
|
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
error = {location: 'crudServer', message: error}
|
|
220
|
-
|
|
221
|
-
if (database)
|
|
222
|
-
error['database'] = database
|
|
223
|
-
|
|
224
|
-
if (data.error)
|
|
225
|
-
data.error.push(error)
|
|
226
|
-
else
|
|
227
|
-
data.error = [error]
|
|
228
|
-
}
|
|
196
|
+
delete data.dbUrl
|
|
197
|
+
if (socket) {
|
|
198
|
+
if (data.organization_id === process.env.organization_id && socket.config.organization_id !== data.organization_id) {
|
|
199
|
+
this.wsManager.broadcast({
|
|
200
|
+
config: {
|
|
201
|
+
organization_id: process.env.organization_id,
|
|
202
|
+
}
|
|
203
|
+
}, action, { ...data });
|
|
204
|
+
data.organization_id = socket.config.organization_id
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
this.wsManager.broadcast(socket, action, data);
|
|
208
|
+
process.emit('changed-document', data)
|
|
209
|
+
resolve()
|
|
210
|
+
} else {
|
|
211
|
+
resolve(data)
|
|
212
|
+
}
|
|
213
|
+
} catch (error) {
|
|
214
|
+
if (socket) {
|
|
215
|
+
errorHandler(data, error)
|
|
216
|
+
this.wsManager.send(socket, action, data);
|
|
217
|
+
resolve()
|
|
218
|
+
} else {
|
|
219
|
+
resolve(data)
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
errorHandler(data, error, database, collection) {
|
|
226
|
+
if (typeof error == 'object')
|
|
227
|
+
error['db'] = 'mongodb'
|
|
228
|
+
else
|
|
229
|
+
error = { location: 'crudServer', message: error }
|
|
230
|
+
|
|
231
|
+
if (database)
|
|
232
|
+
error['database'] = database
|
|
233
|
+
|
|
234
|
+
if (data.error)
|
|
235
|
+
data.error.push(error)
|
|
236
|
+
else
|
|
237
|
+
data.error = [error]
|
|
238
|
+
}
|
|
229
239
|
}
|
|
230
240
|
|
|
231
241
|
module.exports = CoCreateCrudServer;
|