@cocreate/crud-server 1.15.4 → 1.16.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 +14 -0
- package/package.json +3 -3
- package/src/index.js +5 -7
- package/src/mongodb/mongodb.js +0 -791
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
|
+
## [1.16.1](https://github.com/CoCreate-app/CoCreate-crud-server/compare/v1.16.0...v1.16.1) (2022-12-04)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* bump dependencies ([108f8c5](https://github.com/CoCreate-app/CoCreate-crud-server/commit/108f8c52d71ae801ae3f45443b54b9a1734bf00f))
|
|
7
|
+
|
|
8
|
+
# [1.16.0](https://github.com/CoCreate-app/CoCreate-crud-server/compare/v1.15.4...v1.16.0) (2022-12-04)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Features
|
|
12
|
+
|
|
13
|
+
* accepts an object of databases, currently supports mongodb ([743ea2d](https://github.com/CoCreate-app/CoCreate-crud-server/commit/743ea2d3ab58101be1d9cfa842083d0c95ffd0e4))
|
|
14
|
+
|
|
1
15
|
## [1.15.4](https://github.com/CoCreate-app/CoCreate-crud-server/compare/v1.15.3...v1.15.4) (2022-12-04)
|
|
2
16
|
|
|
3
17
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cocreate/crud-server",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.16.1",
|
|
4
4
|
"description": "CoCreate-crud-server",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cocreate-crud",
|
|
@@ -40,8 +40,8 @@
|
|
|
40
40
|
},
|
|
41
41
|
"homepage": "https://cocreate.app/docs/CoCreate-crud-server",
|
|
42
42
|
"dependencies": {
|
|
43
|
-
"@cocreate/docs": "^1.4.
|
|
44
|
-
"@cocreate/utils": "^1.13.
|
|
43
|
+
"@cocreate/docs": "^1.4.13",
|
|
44
|
+
"@cocreate/utils": "^1.13.1",
|
|
45
45
|
"csvtojson": "^2.0.10",
|
|
46
46
|
"json-2-csv": "^3.10.3",
|
|
47
47
|
"mongodb": "^4.4.0"
|
package/src/index.js
CHANGED
|
@@ -1,13 +1,11 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const {ObjectId, searchData, sortData} = require("@cocreate/utils");
|
|
4
|
-
const mongodb = require('./mongodb/mongodb');
|
|
5
4
|
|
|
6
5
|
class CoCreateCrudServer {
|
|
7
|
-
constructor(wsManager) {
|
|
6
|
+
constructor(wsManager, databases) {
|
|
8
7
|
this.wsManager = wsManager
|
|
9
|
-
this.
|
|
10
|
-
this.mongodb = mongodb
|
|
8
|
+
this.databases = databases
|
|
11
9
|
this.ObjectId = ObjectId
|
|
12
10
|
this.init();
|
|
13
11
|
}
|
|
@@ -99,12 +97,12 @@ class CoCreateCrudServer {
|
|
|
99
97
|
}
|
|
100
98
|
|
|
101
99
|
if (!data.db || !data.db.length)
|
|
102
|
-
data.db =
|
|
100
|
+
data.db = ['mongodb']
|
|
103
101
|
let dbsLength = data.db.length
|
|
104
102
|
for (let i = 0; i < data.db.length; i++) {
|
|
105
103
|
dbsLength -= 1
|
|
106
|
-
if (this.
|
|
107
|
-
this[data.db[i]][action](data).then((data) => {
|
|
104
|
+
if (this.databases[data.db[i]]) {
|
|
105
|
+
this.databases[data.db[i]][action](data).then((data) => {
|
|
108
106
|
//ToDo: sorting should take place here in order to return sorted values from multiple dbs
|
|
109
107
|
if (!dbsLength) {
|
|
110
108
|
if (socket) {
|
package/src/mongodb/mongodb.js
DELETED
|
@@ -1,791 +0,0 @@
|
|
|
1
|
-
const {MongoClient, ObjectId} = require('mongodb');
|
|
2
|
-
const {dotNotationToObject, searchData, sortData} = require('@cocreate/utils')
|
|
3
|
-
|
|
4
|
-
function mongoClient(dbUrl) {
|
|
5
|
-
try {
|
|
6
|
-
dbUrl = "mongodb+srv://cocreate-app:rolling123@cocreatedb.dnjr1.mongodb.net" || dbUrl || process.env.MONGO_URL || config.db_url;
|
|
7
|
-
if (!dbUrl || !dbUrl.includes('mongodb'))
|
|
8
|
-
console.log('CoCreate.config.js missing dbUrl')
|
|
9
|
-
dbClient = MongoClient.connect(dbUrl, { useNewUrlParser: true, useUnifiedTopology: true });
|
|
10
|
-
return dbClient;
|
|
11
|
-
} catch (error) {
|
|
12
|
-
console.error(error)
|
|
13
|
-
return {
|
|
14
|
-
status: false
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
let dbClient;
|
|
20
|
-
mongoClient().then(Client => {
|
|
21
|
-
dbClient = Client
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
async function databaseStats(data) {
|
|
25
|
-
let stats = await dbClient.db(data.organization_id).stats()
|
|
26
|
-
return stats
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
function createDatabase(data) {
|
|
30
|
-
return database('createDatabase', data)
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
function readDatabase(data) {
|
|
34
|
-
return database('readDatabase', data)
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function updateDatabase(data) {
|
|
38
|
-
return database('updateDatabase', data)
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
function deleteDatabase(data) {
|
|
42
|
-
return database('deleteDatabase', data)
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
function database(action, data){
|
|
46
|
-
return new Promise((resolve, reject) => {
|
|
47
|
-
const self = this;
|
|
48
|
-
|
|
49
|
-
try {
|
|
50
|
-
if (action == 'readDatabase') {
|
|
51
|
-
let db = dbClient.db().admin();
|
|
52
|
-
|
|
53
|
-
// List all the available databases
|
|
54
|
-
db.listDatabases(function(err, dbs) {
|
|
55
|
-
resolve({...data, database: dbs.databases})
|
|
56
|
-
})
|
|
57
|
-
|
|
58
|
-
}
|
|
59
|
-
if (action == 'deleteDatabase') {
|
|
60
|
-
const db = dbClient.db(data.database);
|
|
61
|
-
db.dropDatabase().then(response => {
|
|
62
|
-
resolve(response)
|
|
63
|
-
})
|
|
64
|
-
}
|
|
65
|
-
} catch(error) {
|
|
66
|
-
errorLog.push(error)
|
|
67
|
-
data['error'] = errorLog
|
|
68
|
-
console.log(action, 'error', error);
|
|
69
|
-
resolve(data);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
}, (err) => {
|
|
73
|
-
errorHandler(data, err)
|
|
74
|
-
});
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
function createCollection(data){
|
|
78
|
-
return collection('createCollection', data)
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
function readCollection(data) {
|
|
82
|
-
return collection('readCollection', data)
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
function updateCollection(data) {
|
|
86
|
-
return collection('updateCollection', data)
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
function deleteCollection(data) {
|
|
90
|
-
return collection('deleteCollection', data)
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
function collection(action, data){
|
|
95
|
-
return new Promise((resolve, reject) => {
|
|
96
|
-
|
|
97
|
-
const self = this;
|
|
98
|
-
let type = 'collection'
|
|
99
|
-
let collectionArray = [];
|
|
100
|
-
|
|
101
|
-
let errorLog = [];
|
|
102
|
-
const errorHandler = (error) => {
|
|
103
|
-
if (error) {
|
|
104
|
-
error.db = 'mongodb'
|
|
105
|
-
errorLog.push(error);
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
try {
|
|
110
|
-
if (data.request)
|
|
111
|
-
data.collection = data.request
|
|
112
|
-
|
|
113
|
-
let databases = data.database;
|
|
114
|
-
if (!Array.isArray(databases))
|
|
115
|
-
databases = [databases]
|
|
116
|
-
|
|
117
|
-
let databasesLength = databases.length
|
|
118
|
-
for (let database of databases) {
|
|
119
|
-
const db = dbClient.db(database);
|
|
120
|
-
if (action == 'readCollection') {
|
|
121
|
-
|
|
122
|
-
let {query, sort} = getFilters(data);
|
|
123
|
-
|
|
124
|
-
db.listCollections(query).toArray(function(error, result) {
|
|
125
|
-
if (error) {
|
|
126
|
-
error.database = database
|
|
127
|
-
errorHandler(error)
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
if (result)
|
|
131
|
-
for (let res of result)
|
|
132
|
-
collectionArray.push({name: res.name, database, db: 'mongodb'})
|
|
133
|
-
|
|
134
|
-
databasesLength -= 1
|
|
135
|
-
if (!databasesLength) {
|
|
136
|
-
data = createData(data, collectionArray, type, errorLog)
|
|
137
|
-
resolve(data)
|
|
138
|
-
}
|
|
139
|
-
})
|
|
140
|
-
} else {
|
|
141
|
-
let collections
|
|
142
|
-
let value
|
|
143
|
-
if (action == 'updateCollection')
|
|
144
|
-
collections = Object.entries(data.collection)
|
|
145
|
-
else
|
|
146
|
-
collections = data.collection;
|
|
147
|
-
|
|
148
|
-
if (!Array.isArray(collections))
|
|
149
|
-
collections = [collections]
|
|
150
|
-
|
|
151
|
-
let collectionsLength = collections.length
|
|
152
|
-
for (let collection of collections) {
|
|
153
|
-
|
|
154
|
-
if (action == 'createCollection') {
|
|
155
|
-
db.createCollection(collection, function(error, result) {
|
|
156
|
-
if (error) {
|
|
157
|
-
error.database = database
|
|
158
|
-
error.collection = collection
|
|
159
|
-
errorHandler(error)
|
|
160
|
-
}
|
|
161
|
-
if (result)
|
|
162
|
-
collectionArray.push({name: collection, database, db: 'mongodb'})
|
|
163
|
-
|
|
164
|
-
collectionsLength -= 1
|
|
165
|
-
if (!collectionsLength)
|
|
166
|
-
databasesLength -= 1
|
|
167
|
-
|
|
168
|
-
if (!databasesLength && !collectionsLength) {
|
|
169
|
-
data = createData(data, collectionArray, type, errorLog)
|
|
170
|
-
resolve(data)
|
|
171
|
-
}
|
|
172
|
-
})
|
|
173
|
-
} else {
|
|
174
|
-
if (action == 'updateCollection') {
|
|
175
|
-
[collection, value] = collection
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
const collectionObj = db.collection(collection);
|
|
179
|
-
|
|
180
|
-
if (action == 'updateCollection') {
|
|
181
|
-
collectionObj.rename(value, function(error, result) {
|
|
182
|
-
if (error) {
|
|
183
|
-
error.database = database
|
|
184
|
-
error.collection = collection
|
|
185
|
-
errorHandler(error)
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
if (result)
|
|
189
|
-
collectionArray.push({name: value, oldName: collection, database, db: 'mongodb'})
|
|
190
|
-
|
|
191
|
-
collectionsLength -= 1
|
|
192
|
-
if (!collectionsLength)
|
|
193
|
-
databasesLength -= 1
|
|
194
|
-
|
|
195
|
-
if (!databasesLength && !collectionsLength) {
|
|
196
|
-
data = createData(data, collectionArray, type, errorLog)
|
|
197
|
-
resolve(data)
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
})
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
if (action == 'deleteCollection') {
|
|
204
|
-
collectionObj.drop( function(error, result) {
|
|
205
|
-
if (error) {
|
|
206
|
-
error.database = database
|
|
207
|
-
error.collection = collection
|
|
208
|
-
errorHandler(error)
|
|
209
|
-
}
|
|
210
|
-
if (result)
|
|
211
|
-
collectionArray.push({name: collection, database, db: 'mongodb'})
|
|
212
|
-
|
|
213
|
-
collectionsLength -= 1
|
|
214
|
-
if (!collectionsLength)
|
|
215
|
-
databasesLength -= 1
|
|
216
|
-
|
|
217
|
-
if (!databasesLength && !collectionsLength) {
|
|
218
|
-
data = createData(data, collectionArray, type, errorLog)
|
|
219
|
-
resolve(data)
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
})
|
|
223
|
-
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
} catch(error) {
|
|
231
|
-
errorLog.push(error)
|
|
232
|
-
data['error'] = errorLog
|
|
233
|
-
console.log(action, 'error', error);
|
|
234
|
-
resolve(data);
|
|
235
|
-
}
|
|
236
|
-
}, (err) => {
|
|
237
|
-
errorHandler(data, err)
|
|
238
|
-
});
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
function createDocument(data){
|
|
242
|
-
return document('createDocument', data)
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
function readDocument(data) {
|
|
246
|
-
return document('readDocument', data)
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
function updateDocument(data) {
|
|
250
|
-
return document('updateDocument', data)
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
function deleteDocument(data) {
|
|
254
|
-
return document('deleteDocument', data)
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
function document(action, data){
|
|
258
|
-
return new Promise(async (resolve, reject) => {
|
|
259
|
-
|
|
260
|
-
const self = this;
|
|
261
|
-
|
|
262
|
-
let errorLog = [];
|
|
263
|
-
const errorHandler = (error) => {
|
|
264
|
-
if (error) {
|
|
265
|
-
error.db = 'mongodb'
|
|
266
|
-
errorLog.push(error);
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
try {
|
|
271
|
-
let type = 'document'
|
|
272
|
-
let documents = [];
|
|
273
|
-
|
|
274
|
-
if (data.request)
|
|
275
|
-
data[type] = data.request
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
if (!data['timeStamp'])
|
|
279
|
-
data['timeStamp'] = new Date().toISOString()
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
let isFilter
|
|
283
|
-
if (data.filter && data.filter.query)
|
|
284
|
-
isFilter = true
|
|
285
|
-
|
|
286
|
-
let databases = data.database;
|
|
287
|
-
if (!Array.isArray(databases))
|
|
288
|
-
databases = [databases]
|
|
289
|
-
|
|
290
|
-
let databasesLength = databases.length
|
|
291
|
-
for (let database of databases) {
|
|
292
|
-
let collections = data.collection;
|
|
293
|
-
if (!Array.isArray(collections))
|
|
294
|
-
collections = [collections]
|
|
295
|
-
|
|
296
|
-
let collectionsLength = collections.length
|
|
297
|
-
for (let collection of collections) {
|
|
298
|
-
const db = dbClient.db(database);
|
|
299
|
-
const collectionObj = db.collection(collection);
|
|
300
|
-
|
|
301
|
-
let {query, sort} = getFilters(data);
|
|
302
|
-
if (data['organization_id']) {
|
|
303
|
-
query['organization_id'] = { $eq: data['organization_id'] }
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
let _ids = []
|
|
307
|
-
let update_ids = []
|
|
308
|
-
let updateData = {}
|
|
309
|
-
|
|
310
|
-
if (data[type]) {
|
|
311
|
-
if (!Array.isArray(data[type]))
|
|
312
|
-
data[type] = [data[type]]
|
|
313
|
-
for (let i = 0; i < data[type].length; i++) {
|
|
314
|
-
data[type][i] = replaceArray(data[type][i])
|
|
315
|
-
data[type][i]['organization_id'] = data['organization_id'];
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
if (action == 'createDocument') {
|
|
319
|
-
data[type][i] = dotNotationToObject(data[type][i])
|
|
320
|
-
|
|
321
|
-
if (!data[type][i]._id)
|
|
322
|
-
data[type][i]._id = ObjectId()
|
|
323
|
-
else
|
|
324
|
-
data[type][i]._id = ObjectId(data[type][i]._id)
|
|
325
|
-
data[type][i]['created'] = {on: data.timeStamp, by: data.user_id || data.clientId}
|
|
326
|
-
}
|
|
327
|
-
if (action == 'readDocument') {
|
|
328
|
-
if (data[type][i]._id)
|
|
329
|
-
_ids.push(ObjectId(data[type][i]._id))
|
|
330
|
-
}
|
|
331
|
-
if (action =='updateDocument') {
|
|
332
|
-
if (data[type][i]._id)
|
|
333
|
-
update_ids.push({_id: data[type][i]._id, updateDoc: data[type][i], updateType: '_id'})
|
|
334
|
-
|
|
335
|
-
if (!data[type][i]._id)
|
|
336
|
-
updateData = createUpdate({document: [data[type][i]]}, type)
|
|
337
|
-
|
|
338
|
-
data[type][i]['modified'] = {on: data.timeStamp, by: data.user_id || data.clientId}
|
|
339
|
-
|
|
340
|
-
}
|
|
341
|
-
if (action =='deleteDocument') {
|
|
342
|
-
if (data[type][i]._id) {
|
|
343
|
-
_ids.push(ObjectId(data[type][i]._id))
|
|
344
|
-
documents.push({_id: data[type][i]._id, db: 'mongodb', database, collection})
|
|
345
|
-
}
|
|
346
|
-
}
|
|
347
|
-
}
|
|
348
|
-
if (_ids.length == 1)
|
|
349
|
-
query['_id'] = ObjectId(_ids[0])
|
|
350
|
-
else if (_ids.length > 0)
|
|
351
|
-
query['_id'] = {$in: _ids}
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
if (action == 'createDocument') {
|
|
356
|
-
collectionObj.insertMany(data[type], function(error, result) {
|
|
357
|
-
if (error) {
|
|
358
|
-
error.database = database
|
|
359
|
-
error.collection = collection
|
|
360
|
-
errorHandler(error)
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
for (let i = 0; i < data[type].length; i++)
|
|
364
|
-
documents.push({db: 'mongodb', database, collection, ...data[type][i]})
|
|
365
|
-
|
|
366
|
-
collectionsLength -= 1
|
|
367
|
-
if (!collectionsLength)
|
|
368
|
-
databasesLength -= 1
|
|
369
|
-
|
|
370
|
-
if (!databasesLength && !collectionsLength) {
|
|
371
|
-
data = createData(data, documents, type, errorLog)
|
|
372
|
-
resolve(data)
|
|
373
|
-
}
|
|
374
|
-
});
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
if (action == 'readDocument') {
|
|
378
|
-
let index = 0, limit = 0
|
|
379
|
-
if (data.filter) {
|
|
380
|
-
const count = await collectionObj.estimatedDocumentCount()
|
|
381
|
-
data.filter.count = count
|
|
382
|
-
|
|
383
|
-
if (data.filter.startIndex)
|
|
384
|
-
index = data.filter.startIndex
|
|
385
|
-
if (data.filter.limit)
|
|
386
|
-
limit = data.filter.limit
|
|
387
|
-
if (limit)
|
|
388
|
-
limit = index + limit;
|
|
389
|
-
}
|
|
390
|
-
|
|
391
|
-
collectionObj.find(query).limit(limit).sort(sort).toArray(function(error, result) {
|
|
392
|
-
if (error) {
|
|
393
|
-
error.database = database
|
|
394
|
-
error.collection = collection
|
|
395
|
-
errorHandler(error)
|
|
396
|
-
}
|
|
397
|
-
|
|
398
|
-
if (result) {
|
|
399
|
-
// ToDo: forEach at cursor
|
|
400
|
-
for (let doc of result) {
|
|
401
|
-
let isMatch = true
|
|
402
|
-
if (data.filter && data.filter['search'])
|
|
403
|
-
isMatch = searchData(doc, data.filter['search'])
|
|
404
|
-
if (isMatch) {
|
|
405
|
-
doc.db = 'mongodb'
|
|
406
|
-
doc.database = database
|
|
407
|
-
doc.collection = collection
|
|
408
|
-
doc._id = doc._id.toString()
|
|
409
|
-
documents.push(doc)
|
|
410
|
-
}
|
|
411
|
-
}
|
|
412
|
-
|
|
413
|
-
if (index && limit) {
|
|
414
|
-
documents = documents.slice(index, limit)
|
|
415
|
-
}
|
|
416
|
-
|
|
417
|
-
if (data.returnDocument == false) {
|
|
418
|
-
|
|
419
|
-
for (let item of data['data']) {
|
|
420
|
-
let resp = {};
|
|
421
|
-
resp['_id'] = tmp['_id']
|
|
422
|
-
data[type].forEach((f) => resp[f] = item[f])
|
|
423
|
-
documents.push(resp);
|
|
424
|
-
}
|
|
425
|
-
|
|
426
|
-
data['data'] = documents
|
|
427
|
-
}
|
|
428
|
-
}
|
|
429
|
-
|
|
430
|
-
collectionsLength -= 1
|
|
431
|
-
if (!collectionsLength)
|
|
432
|
-
databasesLength -= 1
|
|
433
|
-
|
|
434
|
-
if (!databasesLength && !collectionsLength) {
|
|
435
|
-
data = createData(data, documents, type, errorLog)
|
|
436
|
-
resolve(data)
|
|
437
|
-
}
|
|
438
|
-
});
|
|
439
|
-
}
|
|
440
|
-
|
|
441
|
-
if (action == 'updateDocument' || action == 'deleteDocument') {
|
|
442
|
-
const queryDocs = () => {
|
|
443
|
-
return new Promise((resolve, reject) => {
|
|
444
|
-
|
|
445
|
-
collectionObj.find(query).sort(sort).toArray(function(error, result) {
|
|
446
|
-
if (error) {
|
|
447
|
-
error.database = database
|
|
448
|
-
error.collection = collection
|
|
449
|
-
errorHandler(error)
|
|
450
|
-
}
|
|
451
|
-
if (data.filter && data.filter.search) {
|
|
452
|
-
let searchResult = []
|
|
453
|
-
|
|
454
|
-
for (let doc of result) {
|
|
455
|
-
let isMatch = searchData(doc, data.filter.search)
|
|
456
|
-
if (isMatch)
|
|
457
|
-
searchResult.push(doc)
|
|
458
|
-
}
|
|
459
|
-
result = searchResult
|
|
460
|
-
}
|
|
461
|
-
resolve(result)
|
|
462
|
-
})
|
|
463
|
-
}, (err) => {
|
|
464
|
-
console.log(err);
|
|
465
|
-
});
|
|
466
|
-
}
|
|
467
|
-
|
|
468
|
-
let Result, $update, update, projection;
|
|
469
|
-
|
|
470
|
-
if (isFilter && data.returnDocument != false)
|
|
471
|
-
if (action == 'deleteDocument' || action == 'updateDocument' && updateData.update)
|
|
472
|
-
Result = await queryDocs()
|
|
473
|
-
|
|
474
|
-
if (Result) {
|
|
475
|
-
for (let doc of Result) {
|
|
476
|
-
if (action == 'deleteDocument')
|
|
477
|
-
documents.push({_id: doc._id, db: 'mongodb', database, collection})
|
|
478
|
-
else
|
|
479
|
-
doc['modified'] = {on: data.timeStamp, by: data.user_id || data.clientId}
|
|
480
|
-
|
|
481
|
-
_ids.push(doc._id)
|
|
482
|
-
}
|
|
483
|
-
update_ids.push({updateType: 'filter'})
|
|
484
|
-
}
|
|
485
|
-
|
|
486
|
-
if (action == 'updateDocument') {
|
|
487
|
-
let docsLength = update_ids.length
|
|
488
|
-
for (let {updateDoc, updateType} of update_ids) {
|
|
489
|
-
|
|
490
|
-
if (updateType == '_id') {
|
|
491
|
-
let update_id = updateDoc._id
|
|
492
|
-
query['_id'] = ObjectId(update_id)
|
|
493
|
-
$update = createUpdate({document: [updateDoc]}, type)
|
|
494
|
-
update = $update.update
|
|
495
|
-
projection = $update.projection
|
|
496
|
-
documents.push({_id: update_id, db: 'mongodb', database, collection, ...update['$set']})
|
|
497
|
-
}
|
|
498
|
-
|
|
499
|
-
if (updateType == 'filter') {
|
|
500
|
-
query['_id'] = {$in: _ids}
|
|
501
|
-
$update = updateData
|
|
502
|
-
update = $update.update
|
|
503
|
-
projection = $update.projection
|
|
504
|
-
for (let _id of _ids)
|
|
505
|
-
documents.push({_id, db: 'mongodb', database, collection, ...update['$set']})
|
|
506
|
-
|
|
507
|
-
}
|
|
508
|
-
|
|
509
|
-
update['$set']['organization_id'] = data.organization_id
|
|
510
|
-
|
|
511
|
-
collectionObj.updateMany(query, update, {
|
|
512
|
-
upsert: data.upsert,
|
|
513
|
-
projection
|
|
514
|
-
}).then((result) => {
|
|
515
|
-
|
|
516
|
-
}).catch((error) => {
|
|
517
|
-
errorLog.push(error)
|
|
518
|
-
console.log(action, 'error', error);
|
|
519
|
-
}).finally((error) => {
|
|
520
|
-
docsLength -= 1
|
|
521
|
-
if (!docsLength)
|
|
522
|
-
collectionsLength -= 1
|
|
523
|
-
|
|
524
|
-
if (!collectionsLength)
|
|
525
|
-
databasesLength -= 1
|
|
526
|
-
|
|
527
|
-
if (!databasesLength && !collectionsLength) {
|
|
528
|
-
data = createData(data, documents, type, errorLog)
|
|
529
|
-
resolve(data)
|
|
530
|
-
}
|
|
531
|
-
})
|
|
532
|
-
}
|
|
533
|
-
|
|
534
|
-
if (!update_ids.length) {
|
|
535
|
-
docsLength -= 1
|
|
536
|
-
if (!docsLength)
|
|
537
|
-
collectionsLength -= 1
|
|
538
|
-
|
|
539
|
-
if (!collectionsLength)
|
|
540
|
-
databasesLength -= 1
|
|
541
|
-
|
|
542
|
-
if (!databasesLength && !collectionsLength) {
|
|
543
|
-
data = createData(data, documents, type, errorLog)
|
|
544
|
-
resolve(data)
|
|
545
|
-
}
|
|
546
|
-
}
|
|
547
|
-
|
|
548
|
-
}
|
|
549
|
-
|
|
550
|
-
if (action == 'deleteDocument') {
|
|
551
|
-
if (_ids.length == 1)
|
|
552
|
-
query['_id'] = ObjectId(_ids[0])
|
|
553
|
-
else if (_ids.length > 0)
|
|
554
|
-
query['_id'] = {$in: _ids}
|
|
555
|
-
collectionObj.deleteMany(query, function(error, result) {
|
|
556
|
-
collectionsLength -= 1
|
|
557
|
-
if (!collectionsLength)
|
|
558
|
-
databasesLength -= 1
|
|
559
|
-
|
|
560
|
-
if (!databasesLength && !collectionsLength) {
|
|
561
|
-
data = createData(data, documents, type, errorLog)
|
|
562
|
-
resolve(data)
|
|
563
|
-
}
|
|
564
|
-
|
|
565
|
-
})
|
|
566
|
-
}
|
|
567
|
-
|
|
568
|
-
}
|
|
569
|
-
|
|
570
|
-
}
|
|
571
|
-
}
|
|
572
|
-
|
|
573
|
-
} catch(error) {
|
|
574
|
-
errorLog.push(error)
|
|
575
|
-
data['error'] = errorLog
|
|
576
|
-
console.log(action, 'error', error);
|
|
577
|
-
resolve(data);
|
|
578
|
-
}
|
|
579
|
-
}, (err) => {
|
|
580
|
-
errorHandler(data, err)
|
|
581
|
-
});
|
|
582
|
-
|
|
583
|
-
}
|
|
584
|
-
|
|
585
|
-
function createUpdate(data, type) {
|
|
586
|
-
let update = {}, projection = {};
|
|
587
|
-
if (data[type][0]) {
|
|
588
|
-
update['$set'] = data[type][0]
|
|
589
|
-
// update['$set']['organization_id'] = data['organization_id'];
|
|
590
|
-
if (update['$set']['_id'])
|
|
591
|
-
delete update['$set']['_id']
|
|
592
|
-
Object.keys(update['$set']).forEach(x => {
|
|
593
|
-
projection[x] = 1
|
|
594
|
-
})
|
|
595
|
-
}
|
|
596
|
-
|
|
597
|
-
if ( data['deleteName'] ) {
|
|
598
|
-
update['$unset'] = replaceArray(data['deleteName']);
|
|
599
|
-
}
|
|
600
|
-
|
|
601
|
-
if ( data['updateName'] ) {
|
|
602
|
-
update['$rename'] = replaceArray(data['updateName'])
|
|
603
|
-
for (const [key, value] of Object.entries(update['$rename'])) {
|
|
604
|
-
if (/\.([0-9]*)/g.test(key) || /\[([0-9]*)\]/g.test(value)) {
|
|
605
|
-
console.log('key is array', /\[([0-9]*)\]/g.test(value), /\.([0-9]*)/g.test(key))
|
|
606
|
-
} else {
|
|
607
|
-
let newValue = replaceArray({[value]: value})
|
|
608
|
-
let oldkey = key;
|
|
609
|
-
for (const [key] of Object.entries(newValue)) {
|
|
610
|
-
update['$rename'][oldkey] = key
|
|
611
|
-
}
|
|
612
|
-
}
|
|
613
|
-
}
|
|
614
|
-
}
|
|
615
|
-
|
|
616
|
-
return {update, projection}
|
|
617
|
-
|
|
618
|
-
}
|
|
619
|
-
|
|
620
|
-
function createData(data, array, type, errorLog) {
|
|
621
|
-
if (errorLog.length > 0)
|
|
622
|
-
data['error'] = errorLog
|
|
623
|
-
|
|
624
|
-
if (!data.request)
|
|
625
|
-
data.request = data[type] || {}
|
|
626
|
-
|
|
627
|
-
if (data.filter && data.filter.sort)
|
|
628
|
-
data[type] = sortData(array, data.filter.sort)
|
|
629
|
-
else
|
|
630
|
-
data[type] = array
|
|
631
|
-
|
|
632
|
-
if (data.returnLog){
|
|
633
|
-
if (!data.log)
|
|
634
|
-
data.log = []
|
|
635
|
-
data.log.push(...data[type])
|
|
636
|
-
}
|
|
637
|
-
|
|
638
|
-
return data
|
|
639
|
-
}
|
|
640
|
-
|
|
641
|
-
function getFilters(data) {
|
|
642
|
-
let query = {}, sort = {}
|
|
643
|
-
let filter = {
|
|
644
|
-
query: [],
|
|
645
|
-
sort: [],
|
|
646
|
-
search: {
|
|
647
|
-
value: [],
|
|
648
|
-
type: "or"
|
|
649
|
-
},
|
|
650
|
-
startIndex: 0,
|
|
651
|
-
...data.filter
|
|
652
|
-
};
|
|
653
|
-
|
|
654
|
-
query = createQuery(filter.query);
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
if (filter.sort)
|
|
658
|
-
for (let i = 0; i < filter.sort.length; i++)
|
|
659
|
-
sort[filter.sort[i].name] = filter.sort[i].direction
|
|
660
|
-
|
|
661
|
-
return {query, sort}
|
|
662
|
-
}
|
|
663
|
-
|
|
664
|
-
// ToDo: create impved mongodb query to cover many cases
|
|
665
|
-
function createQuery(filters) {
|
|
666
|
-
let query = new Object();
|
|
667
|
-
|
|
668
|
-
filters.forEach((item) => {
|
|
669
|
-
if (!item.name) {
|
|
670
|
-
return;
|
|
671
|
-
}
|
|
672
|
-
var key = item.name;
|
|
673
|
-
if (!query[key]) {
|
|
674
|
-
query[key] = {};
|
|
675
|
-
}
|
|
676
|
-
|
|
677
|
-
if (item.name == "_id")
|
|
678
|
-
item.value = ObjectId(item.value)
|
|
679
|
-
|
|
680
|
-
switch (item.operator) {
|
|
681
|
-
case '$includes':
|
|
682
|
-
query[key]['$regex'] = item.value;
|
|
683
|
-
break;
|
|
684
|
-
|
|
685
|
-
case '$range':
|
|
686
|
-
if (item.value[0] !== null && item.value[1] !== null) {
|
|
687
|
-
query[key] = {$gte: item.value[0], $lte: item.value[1]};
|
|
688
|
-
} else if (item.value[0] !== null) {
|
|
689
|
-
query[key] = {$gte: item.value[0]};
|
|
690
|
-
} else if (item.value[1] !== null) {
|
|
691
|
-
query[key] = {$lte: item.value[1]};
|
|
692
|
-
}
|
|
693
|
-
break;
|
|
694
|
-
|
|
695
|
-
case '$eq':
|
|
696
|
-
case '$ne':
|
|
697
|
-
case '$lt':
|
|
698
|
-
case '$lte':
|
|
699
|
-
case '$gt':
|
|
700
|
-
case '$gte':
|
|
701
|
-
case '$regex':
|
|
702
|
-
query[key][item.operator] = item.value;
|
|
703
|
-
break;
|
|
704
|
-
case '$in':
|
|
705
|
-
// var in_values = [];
|
|
706
|
-
// item.value.forEach(function(v) {
|
|
707
|
-
// in_values.push(new RegExp(".*" + v + ".*", "i"));
|
|
708
|
-
// });
|
|
709
|
-
|
|
710
|
-
query[key] = {$in : item.value }
|
|
711
|
-
break;
|
|
712
|
-
case '$nin':
|
|
713
|
-
query[key][item.operator] = item.value;
|
|
714
|
-
break;
|
|
715
|
-
case '$geoWithin':
|
|
716
|
-
try {
|
|
717
|
-
let value = JSON.parse(item.value);
|
|
718
|
-
if (item.type) {
|
|
719
|
-
query[key]['$geoWithin'] = {
|
|
720
|
-
[item.type]: value
|
|
721
|
-
}
|
|
722
|
-
}
|
|
723
|
-
} catch(e) {
|
|
724
|
-
console.log('geowithin error');
|
|
725
|
-
}
|
|
726
|
-
break;
|
|
727
|
-
}
|
|
728
|
-
})
|
|
729
|
-
|
|
730
|
-
//. global search
|
|
731
|
-
//. we have to set indexes in text fields ex: db.chart.createIndex({ "$**": "text" })
|
|
732
|
-
// if (data['searchKey']) {
|
|
733
|
-
// query["$text"] = {$search: "\"Ni\""};
|
|
734
|
-
// }
|
|
735
|
-
|
|
736
|
-
return query;
|
|
737
|
-
}
|
|
738
|
-
|
|
739
|
-
function errorHandler(data, error, database, collection){
|
|
740
|
-
if (typeof error == 'object')
|
|
741
|
-
error['db'] = 'mongodb'
|
|
742
|
-
else
|
|
743
|
-
error = {db: 'mongodb', message: error}
|
|
744
|
-
|
|
745
|
-
if (database)
|
|
746
|
-
error['database'] = database
|
|
747
|
-
if (collection)
|
|
748
|
-
error['collection'] = collection
|
|
749
|
-
if (data.error)
|
|
750
|
-
data.error.push(error)
|
|
751
|
-
else
|
|
752
|
-
data.error = [error]
|
|
753
|
-
}
|
|
754
|
-
|
|
755
|
-
function replaceArray(data) {
|
|
756
|
-
let keys = Object.keys(data);
|
|
757
|
-
let objectData = {};
|
|
758
|
-
|
|
759
|
-
keys.forEach((k) => {
|
|
760
|
-
let nk = k
|
|
761
|
-
if (/\[([0-9]*)\]/g.test(k)) {
|
|
762
|
-
nk = nk.replace(/\[/g, '.');
|
|
763
|
-
if (nk.endsWith(']'))
|
|
764
|
-
nk = nk.slice(0, -1)
|
|
765
|
-
nk = nk.replace(/\]./g, '.');
|
|
766
|
-
nk = nk.replace(/\]/g, '.');
|
|
767
|
-
}
|
|
768
|
-
objectData[nk] = data[k];
|
|
769
|
-
});
|
|
770
|
-
|
|
771
|
-
return objectData;
|
|
772
|
-
}
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
module.exports = {
|
|
776
|
-
databaseStats,
|
|
777
|
-
createDatabase,
|
|
778
|
-
readDatabase,
|
|
779
|
-
updateDatabase,
|
|
780
|
-
deleteDatabase,
|
|
781
|
-
|
|
782
|
-
createCollection,
|
|
783
|
-
readCollection,
|
|
784
|
-
updateCollection,
|
|
785
|
-
deleteCollection,
|
|
786
|
-
|
|
787
|
-
createDocument,
|
|
788
|
-
readDocument,
|
|
789
|
-
updateDocument,
|
|
790
|
-
deleteDocument,
|
|
791
|
-
}
|