@cocreate/crud-server 1.11.1 → 1.13.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 +32 -0
- package/package.json +2 -2
- package/src/index.js +152 -11
- package/src/mongodb/mongodb.js +776 -0
- package/src/crud.js +0 -423
- package/src/database.js +0 -43
package/src/crud.js
DELETED
|
@@ -1,423 +0,0 @@
|
|
|
1
|
-
const {ObjectId} = require("mongodb");
|
|
2
|
-
const {replaceArray} = require("./utils.crud.js")
|
|
3
|
-
const {searchData, sortData} = require("@cocreate/filter")
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
class CoCreateCrud {
|
|
8
|
-
constructor(wsManager, dbClient) {
|
|
9
|
-
this.wsManager = wsManager
|
|
10
|
-
this.dbClient = dbClient
|
|
11
|
-
this.init();
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
init() {
|
|
15
|
-
if (this.wsManager) {
|
|
16
|
-
this.wsManager.on('createDocument', (socket, data) => this.createDocument(socket, data))
|
|
17
|
-
this.wsManager.on('readDocument', (socket, data) => this.readDocument(socket, data))
|
|
18
|
-
this.wsManager.on('updateDocument', (socket, data) => this.updateDocument(socket, data))
|
|
19
|
-
this.wsManager.on('deleteDocument', (socket, data) => this.deleteDocument(socket, data))
|
|
20
|
-
this.wsManager.on('readDocuments', (socket, data) => this.readDocuments(socket, data))
|
|
21
|
-
this.wsManager.on('createCollection', (socket, data) => this.createCollection(socket, data))
|
|
22
|
-
this.wsManager.on('updateCollection', (socket, data) => this.updateCollection(socket, data))
|
|
23
|
-
this.wsManager.on('deleteCollection', (socket, data) => this.deleteCollection(socket, data))
|
|
24
|
-
this.wsManager.on('readCollections', (socket, data) => this.readCollections(socket, data))
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/** Create Document **/
|
|
29
|
-
async createDocument(socket, data){
|
|
30
|
-
const self = this;
|
|
31
|
-
if(!data.data) return;
|
|
32
|
-
|
|
33
|
-
try{
|
|
34
|
-
const db = this.dbClient.db(data['organization_id']);
|
|
35
|
-
const collection = db.collection(data["collection"]);
|
|
36
|
-
let insertData = replaceArray(data.data);
|
|
37
|
-
insertData['organization_id'] = data['organization_id'];
|
|
38
|
-
|
|
39
|
-
collection.insertOne(insertData, function(error, result) {
|
|
40
|
-
if(!error && result){
|
|
41
|
-
const response = {...data, document_id: `${result.insertedId}`, data: insertData }
|
|
42
|
-
response.data['_id'] = result.insertedId;
|
|
43
|
-
self.broadcast(socket, 'createDocument', response)
|
|
44
|
-
} else {
|
|
45
|
-
self.wsManager.send(socket, 'ServerError', error);
|
|
46
|
-
}
|
|
47
|
-
});
|
|
48
|
-
}catch(error){
|
|
49
|
-
console.log('createDocument error', error);
|
|
50
|
-
self.wsManager.send(socket, 'ServerError', 'error');
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/** Read Document **/
|
|
55
|
-
async readDocument(socket, data) {
|
|
56
|
-
if (!data['collection'] || data['collection'] == 'null' || typeof data['collection'] !== 'string') {
|
|
57
|
-
this.wsManager.send(socket, 'ServerError', 'error');
|
|
58
|
-
return;
|
|
59
|
-
}
|
|
60
|
-
const self = this;
|
|
61
|
-
|
|
62
|
-
try {
|
|
63
|
-
const db = this.dbClient.db(data['organization_id']);
|
|
64
|
-
const collection = db.collection(data["collection"]);
|
|
65
|
-
|
|
66
|
-
const {query, sort} = this.getFilters(data);
|
|
67
|
-
if (data['organization_id']) {
|
|
68
|
-
query['organization_id'] = data['organization_id'];
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
collection.find(query).sort(sort).toArray(function(error, result) {
|
|
72
|
-
if (!error && result && result.length > 0) {
|
|
73
|
-
let tmp = result[0];
|
|
74
|
-
|
|
75
|
-
// ToDo: returns values of defined names
|
|
76
|
-
if (data.returnDocument == false && data.data) {
|
|
77
|
-
let resp = {};
|
|
78
|
-
resp['_id'] = tmp['_id']
|
|
79
|
-
data.data.forEach((f) => resp[f] = tmp[f])
|
|
80
|
-
tmp = resp;
|
|
81
|
-
}
|
|
82
|
-
data.data = tmp
|
|
83
|
-
self.wsManager.send(socket, 'readDocument', data);
|
|
84
|
-
} else {
|
|
85
|
-
self.wsManager.send(socket, 'readDocument error', data);
|
|
86
|
-
}
|
|
87
|
-
});
|
|
88
|
-
} catch (error) {
|
|
89
|
-
console.log('readDocument error', error, data);
|
|
90
|
-
self.wsManager.send(socket, 'ServerError', 'error');
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/** Update Document **/
|
|
95
|
-
async updateDocument(socket, data) {
|
|
96
|
-
const self = this;
|
|
97
|
-
try {
|
|
98
|
-
let {query, sort} = this.getFilters(data);
|
|
99
|
-
if (data['data'] && data['data']['_id'])
|
|
100
|
-
delete data['data']['_id']
|
|
101
|
-
|
|
102
|
-
let update = {}, projection = {}, returnNewDocument = false;
|
|
103
|
-
if (data.data) {
|
|
104
|
-
update['$set'] = this.valueTypes(data.data)
|
|
105
|
-
update['$set']['organization_id'] = data['organization_id'];
|
|
106
|
-
if (update['$set']['_id'])
|
|
107
|
-
delete update['$set']['_id']
|
|
108
|
-
Object.keys(update['$set']).forEach(x => {
|
|
109
|
-
projection[x] = 1
|
|
110
|
-
})
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
if( data['deleteName'] ) {
|
|
115
|
-
update['$unset'] = replaceArray(data['deleteName']);
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
if( data['updateName'] ) {
|
|
119
|
-
update['$rename'] = replaceArray(data['updateName'])
|
|
120
|
-
for (const [key, value] of Object.entries(update['$rename'])) {
|
|
121
|
-
if (/\.([0-9]*)/g.test(key) || /\[([0-9]*)\]/g.test(value)) {
|
|
122
|
-
console.log('key is array', /\[([0-9]*)\]/g.test(value), /\.([0-9]*)/g.test(key))
|
|
123
|
-
} else {
|
|
124
|
-
let newValue = replaceArray({[value]: value})
|
|
125
|
-
let oldkey = key;
|
|
126
|
-
for (const [key] of Object.entries(newValue)) {
|
|
127
|
-
update['$rename'][oldkey] = key
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
returnNewDocument == true
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
const db = this.dbClient.db(data['organization_id']);
|
|
135
|
-
const collection = db.collection(data["collection"]);
|
|
136
|
-
collection.findOneAndUpdate( query, update, {
|
|
137
|
-
returnOriginal: false,
|
|
138
|
-
returnNewDocument: returnNewDocument || false,
|
|
139
|
-
upsert: data.upsert || false,
|
|
140
|
-
projection: projection,
|
|
141
|
-
}
|
|
142
|
-
).then((result) => {
|
|
143
|
-
if (result) {
|
|
144
|
-
update['$set']['_id'] = data.data._id || data.document_id
|
|
145
|
-
data.data = update['$set']
|
|
146
|
-
self.broadcast(socket, 'updateDocument', data)
|
|
147
|
-
} else {
|
|
148
|
-
self.wsManager.send(socket, 'ServerError', error);
|
|
149
|
-
}
|
|
150
|
-
}).catch((error) => {
|
|
151
|
-
console.log('error', error)
|
|
152
|
-
self.wsManager.send(socket, 'ServerError', error);
|
|
153
|
-
});
|
|
154
|
-
|
|
155
|
-
} catch (error) {
|
|
156
|
-
console.log(error)
|
|
157
|
-
self.wsManager.send(socket, 'updateDocumentError', error);
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
/** Delete Document **/
|
|
162
|
-
async deleteDocument(socket, data) {
|
|
163
|
-
const self = this;
|
|
164
|
-
|
|
165
|
-
try {
|
|
166
|
-
const db = this.dbClient.db(data['organization_id']);
|
|
167
|
-
const collection = db.collection(data["collection"]);
|
|
168
|
-
const query = {
|
|
169
|
-
"_id": new ObjectId(data["document_id"])
|
|
170
|
-
};
|
|
171
|
-
|
|
172
|
-
collection.deleteOne(query, function(error, result) {
|
|
173
|
-
if (!error) {
|
|
174
|
-
self.broadcast(socket, 'deleteDocument', data)
|
|
175
|
-
} else {
|
|
176
|
-
self.wsManager.send(socket, 'ServerError', error);
|
|
177
|
-
}
|
|
178
|
-
})
|
|
179
|
-
} catch (error) {
|
|
180
|
-
console.log(error);
|
|
181
|
-
self.wsManager.send(socket, 'ServerError', 'error');
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
async readDocuments(socket, data) {
|
|
186
|
-
function sleep(ms) {
|
|
187
|
-
return new Promise((resolve) => {
|
|
188
|
-
setTimeout(resolve, ms);
|
|
189
|
-
});
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
const self = this;
|
|
193
|
-
|
|
194
|
-
try {
|
|
195
|
-
const db = this.dbClient.db(data['organization_id']);
|
|
196
|
-
const collection = db.collection(data["collection"]);
|
|
197
|
-
let {query, sort} = this.getFilters(data);
|
|
198
|
-
collection.find(query).sort(sort).toArray(function(error, result) {
|
|
199
|
-
if (result) {
|
|
200
|
-
data['data'] = searchData(result, data.filter)
|
|
201
|
-
self.wsManager.send(socket, 'readDocuments', data );
|
|
202
|
-
} else {
|
|
203
|
-
console.log(error)
|
|
204
|
-
self.wsManager.send(socket, 'ServerError', error);
|
|
205
|
-
}
|
|
206
|
-
})
|
|
207
|
-
} catch (error) {
|
|
208
|
-
console.log('readDocuments error', error);
|
|
209
|
-
this.wsManager.send(socket, 'ServerError', 'error');
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
/** Create Collection **/
|
|
214
|
-
async createCollection(socket, data) {
|
|
215
|
-
const self = this;
|
|
216
|
-
|
|
217
|
-
try {
|
|
218
|
-
const db = this.dbClient.db(data['organization_id']);
|
|
219
|
-
db.createCollection(data.collection, function(error, result) {
|
|
220
|
-
if (!error) {
|
|
221
|
-
self.broadcast(socket, 'createCollection', data)
|
|
222
|
-
} else {
|
|
223
|
-
self.wsManager.send(socket, 'ServerError', error);
|
|
224
|
-
}
|
|
225
|
-
})
|
|
226
|
-
} catch (error) {
|
|
227
|
-
console.log(error);
|
|
228
|
-
self.wsManager.send(socket, 'ServerError', 'error');
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
/** Update Collection **/
|
|
233
|
-
async updateCollection(socket, data) {
|
|
234
|
-
const self = this;
|
|
235
|
-
|
|
236
|
-
try {
|
|
237
|
-
const db = this.dbClient.db(data['organization_id']);
|
|
238
|
-
const collection = db.collection(data["collection"]);
|
|
239
|
-
collection.rename(data.target, function(error, result) {
|
|
240
|
-
if (!error) {
|
|
241
|
-
self.broadcast(socket, 'updateCollection', data)
|
|
242
|
-
} else {
|
|
243
|
-
self.wsManager.send(socket, 'ServerError', error);
|
|
244
|
-
}
|
|
245
|
-
})
|
|
246
|
-
} catch (error) {
|
|
247
|
-
console.log(error);
|
|
248
|
-
self.wsManager.send(socket, 'ServerError', 'error');
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
/** Delete Collection **/
|
|
253
|
-
async deleteCollection(socket, data) {
|
|
254
|
-
const self = this;
|
|
255
|
-
|
|
256
|
-
try {
|
|
257
|
-
const db = this.dbClient.db(data['organization_id']);
|
|
258
|
-
const collection = db.collection(data["collection"]);
|
|
259
|
-
collection.drop( function(error, result) {
|
|
260
|
-
if (!error) {
|
|
261
|
-
self.broadcast(socket, 'deleteCollection', data)
|
|
262
|
-
} else {
|
|
263
|
-
self.wsManager.send(socket, 'ServerError', error);
|
|
264
|
-
}
|
|
265
|
-
})
|
|
266
|
-
} catch (error) {
|
|
267
|
-
console.log(error);
|
|
268
|
-
self.wsManager.send(socket, 'ServerError', 'error');
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
async readCollections(socket, data) {
|
|
273
|
-
try {
|
|
274
|
-
const self = this;
|
|
275
|
-
data['collection'] = 'collections'
|
|
276
|
-
|
|
277
|
-
let {query, sort} = this.getFilters(data);
|
|
278
|
-
const db = this.dbClient.db(data['organization_id']);
|
|
279
|
-
db.listCollections(query).toArray(function(error, result) {
|
|
280
|
-
if (!error && result && result.length > 0) {
|
|
281
|
-
data.data = sortData(result, sort)
|
|
282
|
-
self.wsManager.send(socket, 'readCollections', data);
|
|
283
|
-
}
|
|
284
|
-
})
|
|
285
|
-
} catch(error) {
|
|
286
|
-
console.log('readCollections error', error);
|
|
287
|
-
this.wsManager.send(socket, 'ServerError', 'error');
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
getFilters(data) {
|
|
292
|
-
let query = {}, sort = {}
|
|
293
|
-
data.filter = {
|
|
294
|
-
query: [],
|
|
295
|
-
sort: [],
|
|
296
|
-
search: {
|
|
297
|
-
value: [],
|
|
298
|
-
type: "or"
|
|
299
|
-
},
|
|
300
|
-
startIndex: 0,
|
|
301
|
-
...data.filter
|
|
302
|
-
};
|
|
303
|
-
if (data.filter) {
|
|
304
|
-
|
|
305
|
-
query = this.createQuery(data['filter'].query);
|
|
306
|
-
if (data["document_id"]) {
|
|
307
|
-
query['_id'] = ObjectId(data["document_id"]);
|
|
308
|
-
}
|
|
309
|
-
if (data.filter.sort)
|
|
310
|
-
data.filter.sort.forEach((order) => {
|
|
311
|
-
sort[order.name] = order.type
|
|
312
|
-
});
|
|
313
|
-
}
|
|
314
|
-
return {query, sort}
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
createQuery(filters, data) {
|
|
318
|
-
let query = new Object();
|
|
319
|
-
|
|
320
|
-
filters.forEach((item) => {
|
|
321
|
-
if (!item.name) {
|
|
322
|
-
return;
|
|
323
|
-
}
|
|
324
|
-
var key = item.name;
|
|
325
|
-
if (!query[key]) {
|
|
326
|
-
query[key] = {};
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
if (item.name == "_id")
|
|
330
|
-
item.value = ObjectId(item.value)
|
|
331
|
-
|
|
332
|
-
switch (item.operator) {
|
|
333
|
-
case '$contain':
|
|
334
|
-
var in_values = [];
|
|
335
|
-
item.value.forEach(function(v) {
|
|
336
|
-
in_values.push(new RegExp(".*" + v + ".*", "i"));
|
|
337
|
-
});
|
|
338
|
-
|
|
339
|
-
query[key] = {$in : in_values }
|
|
340
|
-
break;
|
|
341
|
-
|
|
342
|
-
case '$range':
|
|
343
|
-
if (item.value[0] !== null && item.value[1] !== null) {
|
|
344
|
-
query[key] = {$gte: item.value[0], $lte: item.value[1]};
|
|
345
|
-
} else if (item.value[0] !== null) {
|
|
346
|
-
query[key] = {$gte: item.value[0]};
|
|
347
|
-
} else if (item.value[1] !== null) {
|
|
348
|
-
query[key] = {$lte: item.value[1]};
|
|
349
|
-
}
|
|
350
|
-
break;
|
|
351
|
-
|
|
352
|
-
case '$eq':
|
|
353
|
-
case '$ne':
|
|
354
|
-
case '$lt':
|
|
355
|
-
case '$lte':
|
|
356
|
-
case '$gt':
|
|
357
|
-
case '$gte':
|
|
358
|
-
query[key][item.operator] = item.value[0] || item.value;
|
|
359
|
-
break;
|
|
360
|
-
case '$in':
|
|
361
|
-
case '$nin':
|
|
362
|
-
query[key][item.operator] = item.value;
|
|
363
|
-
break;
|
|
364
|
-
case '$geoWithin':
|
|
365
|
-
try {
|
|
366
|
-
let value = JSON.parse(item.value);
|
|
367
|
-
if (item.type) {
|
|
368
|
-
query[key]['$geoWithin'] = {
|
|
369
|
-
[item.type]: value
|
|
370
|
-
}
|
|
371
|
-
}
|
|
372
|
-
} catch(e) {
|
|
373
|
-
console.log('geowithin error');
|
|
374
|
-
}
|
|
375
|
-
break;
|
|
376
|
-
}
|
|
377
|
-
})
|
|
378
|
-
|
|
379
|
-
//. global search
|
|
380
|
-
//. we have to set indexes in text fields ex: db.chart.createIndex({ "$**": "text" })
|
|
381
|
-
// if (data['searchKey']) {
|
|
382
|
-
// query["$text"] = {$search: "\"Ni\""};
|
|
383
|
-
// }
|
|
384
|
-
return query;
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
valueTypes(data) {
|
|
388
|
-
let object = {}
|
|
389
|
-
if( typeof data === 'object' ) {
|
|
390
|
-
// update['$set'] = {}
|
|
391
|
-
for (const [key, value] of Object.entries(replaceArray(data))) {
|
|
392
|
-
let val;
|
|
393
|
-
let valueType = typeof value;
|
|
394
|
-
switch(valueType) {
|
|
395
|
-
case 'string':
|
|
396
|
-
val = value
|
|
397
|
-
break;
|
|
398
|
-
case 'number':
|
|
399
|
-
val = Number(value)
|
|
400
|
-
break;
|
|
401
|
-
case 'object':
|
|
402
|
-
if (Array.isArray(value))
|
|
403
|
-
val = new Array(...value)
|
|
404
|
-
else
|
|
405
|
-
val = new Object(value)
|
|
406
|
-
break;
|
|
407
|
-
default:
|
|
408
|
-
val = value
|
|
409
|
-
}
|
|
410
|
-
object[key] = val
|
|
411
|
-
}
|
|
412
|
-
return object;
|
|
413
|
-
}
|
|
414
|
-
}
|
|
415
|
-
|
|
416
|
-
broadcast(socket, component, response) {
|
|
417
|
-
this.wsManager.broadcast(socket, component, response);
|
|
418
|
-
process.emit('changed-document', response)
|
|
419
|
-
}
|
|
420
|
-
|
|
421
|
-
}
|
|
422
|
-
|
|
423
|
-
module.exports = CoCreateCrud;
|
package/src/database.js
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
class CoCreateDatabases {
|
|
2
|
-
constructor(wsManager, dbClient) {
|
|
3
|
-
this.wsManager = wsManager
|
|
4
|
-
this.dbClient = dbClient
|
|
5
|
-
this.init();
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
init() {
|
|
9
|
-
if (this.wsManager) {
|
|
10
|
-
this.wsManager.on('readDatabases', (socket, data) => this.readDatabases(socket, data));
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
/** Create Document **/
|
|
15
|
-
async readDatabases(socket, req_data){
|
|
16
|
-
const self = this;
|
|
17
|
-
// if(!req_data.data) return;
|
|
18
|
-
|
|
19
|
-
try {
|
|
20
|
-
var db = this.dbClient.db().admin();
|
|
21
|
-
|
|
22
|
-
// List all the available databases
|
|
23
|
-
db.listDatabases(function(err, dbs) {
|
|
24
|
-
if (dbs.databases.length > 0){
|
|
25
|
-
console.log('dbs', dbs)
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
// db.close();
|
|
29
|
-
})
|
|
30
|
-
} catch(error) {
|
|
31
|
-
console.log('readDatabases error', error);
|
|
32
|
-
self.wsManager.send(socket, 'ServerError', 'error');
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
broadcast(socket, component, response) {
|
|
38
|
-
this.wsManager.broadcast(socket, component, response);
|
|
39
|
-
process.emit('changed-document', response)
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
module.exports = CoCreateDatabases;
|