@cocreate/mongodb 1.0.9 → 1.1.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 +19 -0
- package/package.json +4 -4
- package/src/index.js +59 -90
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,22 @@
|
|
|
1
|
+
# [1.1.0](https://github.com/CoCreate-app/CoCreate-mongodb/compare/v1.0.10...v1.1.0) (2022-12-11)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* bump dependencies ([127aa5f](https://github.com/CoCreate-app/CoCreate-mongodb/commit/127aa5f316d6d9276ef29b83613a8d1fa9cc26ad))
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Features
|
|
10
|
+
|
|
11
|
+
* query, search and sort databases and collections ([d19b2cd](https://github.com/CoCreate-app/CoCreate-mongodb/commit/d19b2cd1c516c15685c1cb4818468f69bd2f9a3b))
|
|
12
|
+
|
|
13
|
+
## [1.0.10](https://github.com/CoCreate-app/CoCreate-mongodb/compare/v1.0.9...v1.0.10) (2022-12-09)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
### Bug Fixes
|
|
17
|
+
|
|
18
|
+
* bump dependencies ([c69c11a](https://github.com/CoCreate-app/CoCreate-mongodb/commit/c69c11a845e007c0a01826e71497b2c03e4eb8e5))
|
|
19
|
+
|
|
1
20
|
## [1.0.9](https://github.com/CoCreate-app/CoCreate-mongodb/compare/v1.0.8...v1.0.9) (2022-12-08)
|
|
2
21
|
|
|
3
22
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cocreate/mongodb",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "A simple mongodb component in vanilla javascript. Easily configured using HTML5 data-attributes and/or JavaScript API.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"mongodb",
|
|
@@ -61,9 +61,9 @@
|
|
|
61
61
|
"webpack-log": "^3.0.1"
|
|
62
62
|
},
|
|
63
63
|
"dependencies": {
|
|
64
|
-
"@cocreate/docs": "^1.4.
|
|
65
|
-
"@cocreate/hosting": "^1.6.
|
|
66
|
-
"@cocreate/utils": "^1.15.
|
|
64
|
+
"@cocreate/docs": "^1.4.17",
|
|
65
|
+
"@cocreate/hosting": "^1.6.16",
|
|
66
|
+
"@cocreate/utils": "^1.15.2",
|
|
67
67
|
"mongodb": "^4.12.1"
|
|
68
68
|
}
|
|
69
69
|
}
|
package/src/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
const {MongoClient, ObjectId} = require('mongodb');
|
|
2
|
-
const {dotNotationToObject, searchData, sortData} = require('@cocreate/utils')
|
|
2
|
+
const {dotNotationToObject, queryData, searchData, sortData} = require('@cocreate/utils')
|
|
3
3
|
|
|
4
4
|
function mongoClient(dbUrl) {
|
|
5
5
|
try {
|
|
@@ -44,15 +44,23 @@ function deleteDatabase(data) {
|
|
|
44
44
|
|
|
45
45
|
function database(action, data){
|
|
46
46
|
return new Promise((resolve, reject) => {
|
|
47
|
-
|
|
48
|
-
|
|
47
|
+
let type = 'database'
|
|
48
|
+
let databaseArray = []
|
|
49
|
+
|
|
49
50
|
try {
|
|
50
51
|
if (action == 'readDatabase') {
|
|
51
52
|
let db = dbClient.db().admin();
|
|
52
53
|
|
|
53
54
|
// List all the available databases
|
|
54
55
|
db.listDatabases(function(err, dbs) {
|
|
55
|
-
|
|
56
|
+
|
|
57
|
+
for (let database of dbs.databases){
|
|
58
|
+
let isFilter = queryData(database, data.filter.query)
|
|
59
|
+
if (isFilter)
|
|
60
|
+
databaseArray.push({database, db: 'mongodb'})
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
resolve(createData(data, databaseArray, type))
|
|
56
64
|
})
|
|
57
65
|
|
|
58
66
|
}
|
|
@@ -63,17 +71,17 @@ function database(action, data){
|
|
|
63
71
|
})
|
|
64
72
|
}
|
|
65
73
|
} catch(error) {
|
|
66
|
-
|
|
67
|
-
data['error'] = errorLog
|
|
74
|
+
errorHandler(data, error)
|
|
68
75
|
console.log(action, 'error', error);
|
|
69
76
|
resolve(data);
|
|
70
77
|
}
|
|
71
78
|
|
|
72
|
-
}, (
|
|
73
|
-
errorHandler(data,
|
|
79
|
+
}, (error) => {
|
|
80
|
+
errorHandler(data, error)
|
|
74
81
|
});
|
|
75
82
|
}
|
|
76
83
|
|
|
84
|
+
|
|
77
85
|
function createCollection(data){
|
|
78
86
|
return collection('createCollection', data)
|
|
79
87
|
}
|
|
@@ -90,22 +98,11 @@ function deleteCollection(data) {
|
|
|
90
98
|
return collection('deleteCollection', data)
|
|
91
99
|
}
|
|
92
100
|
|
|
93
|
-
|
|
94
101
|
function collection(action, data){
|
|
95
102
|
return new Promise((resolve, reject) => {
|
|
96
|
-
|
|
97
|
-
const self = this;
|
|
98
103
|
let type = 'collection'
|
|
99
104
|
let collectionArray = [];
|
|
100
105
|
|
|
101
|
-
let errorLog = [];
|
|
102
|
-
const errorHandler = (error) => {
|
|
103
|
-
if (error) {
|
|
104
|
-
error.db = 'mongodb'
|
|
105
|
-
errorLog.push(error);
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
|
|
109
106
|
try {
|
|
110
107
|
if (data.request)
|
|
111
108
|
data.collection = data.request
|
|
@@ -121,19 +118,21 @@ function collection(action, data){
|
|
|
121
118
|
|
|
122
119
|
let {query, sort} = getFilters(data);
|
|
123
120
|
|
|
124
|
-
db.listCollections(
|
|
125
|
-
if (error)
|
|
126
|
-
error
|
|
127
|
-
errorHandler(error)
|
|
128
|
-
}
|
|
121
|
+
db.listCollections().toArray(function(error, result) {
|
|
122
|
+
if (error)
|
|
123
|
+
errorHandler(data, error, database)
|
|
129
124
|
|
|
130
|
-
if (result)
|
|
131
|
-
for (let res of result)
|
|
132
|
-
|
|
125
|
+
if (result) {
|
|
126
|
+
for (let res of result) {
|
|
127
|
+
let isFilter = queryData(res, data.filter.query)
|
|
128
|
+
if (isFilter)
|
|
129
|
+
collectionArray.push({name: res.name, database, db: 'mongodb'})
|
|
130
|
+
}
|
|
131
|
+
}
|
|
133
132
|
|
|
134
133
|
databasesLength -= 1
|
|
135
134
|
if (!databasesLength) {
|
|
136
|
-
data = createData(data, collectionArray, type
|
|
135
|
+
data = createData(data, collectionArray, type)
|
|
137
136
|
resolve(data)
|
|
138
137
|
}
|
|
139
138
|
})
|
|
@@ -153,11 +152,9 @@ function collection(action, data){
|
|
|
153
152
|
|
|
154
153
|
if (action == 'createCollection') {
|
|
155
154
|
db.createCollection(collection, function(error, result) {
|
|
156
|
-
if (error)
|
|
157
|
-
error
|
|
158
|
-
|
|
159
|
-
errorHandler(error)
|
|
160
|
-
}
|
|
155
|
+
if (error)
|
|
156
|
+
errorHandler(data, error, database, collection)
|
|
157
|
+
|
|
161
158
|
if (result)
|
|
162
159
|
collectionArray.push({name: collection, database, db: 'mongodb'})
|
|
163
160
|
|
|
@@ -166,7 +163,7 @@ function collection(action, data){
|
|
|
166
163
|
databasesLength -= 1
|
|
167
164
|
|
|
168
165
|
if (!databasesLength && !collectionsLength) {
|
|
169
|
-
data = createData(data, collectionArray, type
|
|
166
|
+
data = createData(data, collectionArray, type)
|
|
170
167
|
resolve(data)
|
|
171
168
|
}
|
|
172
169
|
})
|
|
@@ -179,11 +176,8 @@ function collection(action, data){
|
|
|
179
176
|
|
|
180
177
|
if (action == 'updateCollection') {
|
|
181
178
|
collectionObj.rename(value, function(error, result) {
|
|
182
|
-
if (error)
|
|
183
|
-
error
|
|
184
|
-
error.collection = collection
|
|
185
|
-
errorHandler(error)
|
|
186
|
-
}
|
|
179
|
+
if (error)
|
|
180
|
+
errorHandler(data, error, database, collection)
|
|
187
181
|
|
|
188
182
|
if (result)
|
|
189
183
|
collectionArray.push({name: value, oldName: collection, database, db: 'mongodb'})
|
|
@@ -193,7 +187,7 @@ function collection(action, data){
|
|
|
193
187
|
databasesLength -= 1
|
|
194
188
|
|
|
195
189
|
if (!databasesLength && !collectionsLength) {
|
|
196
|
-
data = createData(data, collectionArray, type
|
|
190
|
+
data = createData(data, collectionArray, type)
|
|
197
191
|
resolve(data)
|
|
198
192
|
}
|
|
199
193
|
|
|
@@ -202,11 +196,9 @@ function collection(action, data){
|
|
|
202
196
|
|
|
203
197
|
if (action == 'deleteCollection') {
|
|
204
198
|
collectionObj.drop( function(error, result) {
|
|
205
|
-
if (error)
|
|
206
|
-
error
|
|
207
|
-
|
|
208
|
-
errorHandler(error)
|
|
209
|
-
}
|
|
199
|
+
if (error)
|
|
200
|
+
errorHandler(data, error, database, collection)
|
|
201
|
+
|
|
210
202
|
if (result)
|
|
211
203
|
collectionArray.push({name: collection, database, db: 'mongodb'})
|
|
212
204
|
|
|
@@ -215,7 +207,7 @@ function collection(action, data){
|
|
|
215
207
|
databasesLength -= 1
|
|
216
208
|
|
|
217
209
|
if (!databasesLength && !collectionsLength) {
|
|
218
|
-
data = createData(data, collectionArray, type
|
|
210
|
+
data = createData(data, collectionArray, type)
|
|
219
211
|
resolve(data)
|
|
220
212
|
}
|
|
221
213
|
|
|
@@ -228,16 +220,16 @@ function collection(action, data){
|
|
|
228
220
|
}
|
|
229
221
|
}
|
|
230
222
|
} catch(error) {
|
|
231
|
-
|
|
232
|
-
data['error'] = errorLog
|
|
223
|
+
errorHandler(data, error)
|
|
233
224
|
console.log(action, 'error', error);
|
|
234
225
|
resolve(data);
|
|
235
226
|
}
|
|
236
|
-
}, (
|
|
237
|
-
errorHandler(data,
|
|
227
|
+
}, (error) => {
|
|
228
|
+
errorHandler(data, error)
|
|
238
229
|
});
|
|
239
230
|
}
|
|
240
231
|
|
|
232
|
+
|
|
241
233
|
function createDocument(data){
|
|
242
234
|
return document('createDocument', data)
|
|
243
235
|
}
|
|
@@ -256,17 +248,6 @@ function deleteDocument(data) {
|
|
|
256
248
|
|
|
257
249
|
function document(action, data){
|
|
258
250
|
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
251
|
try {
|
|
271
252
|
let type = 'document'
|
|
272
253
|
let documents = [];
|
|
@@ -353,11 +334,8 @@ function document(action, data){
|
|
|
353
334
|
|
|
354
335
|
if (action == 'createDocument') {
|
|
355
336
|
collectionObj.insertMany(data[type], function(error, result) {
|
|
356
|
-
if (error)
|
|
357
|
-
error
|
|
358
|
-
error.collection = collection
|
|
359
|
-
errorHandler(error)
|
|
360
|
-
}
|
|
337
|
+
if (error)
|
|
338
|
+
errorHandler(data, error, database, collection)
|
|
361
339
|
|
|
362
340
|
for (let i = 0; i < data[type].length; i++)
|
|
363
341
|
documents.push({db: 'mongodb', database, collection, ...data[type][i]})
|
|
@@ -367,7 +345,7 @@ function document(action, data){
|
|
|
367
345
|
databasesLength -= 1
|
|
368
346
|
|
|
369
347
|
if (!databasesLength && !collectionsLength) {
|
|
370
|
-
data = createData(data, documents, type
|
|
348
|
+
data = createData(data, documents, type)
|
|
371
349
|
resolve(data)
|
|
372
350
|
}
|
|
373
351
|
});
|
|
@@ -388,11 +366,8 @@ function document(action, data){
|
|
|
388
366
|
}
|
|
389
367
|
|
|
390
368
|
collectionObj.find(query).limit(limit).sort(sort).toArray(function(error, result) {
|
|
391
|
-
if (error)
|
|
392
|
-
error
|
|
393
|
-
error.collection = collection
|
|
394
|
-
errorHandler(error)
|
|
395
|
-
}
|
|
369
|
+
if (error)
|
|
370
|
+
errorHandler(data, error, database, collection)
|
|
396
371
|
|
|
397
372
|
if (result) {
|
|
398
373
|
// ToDo: forEach at cursor
|
|
@@ -431,7 +406,7 @@ function document(action, data){
|
|
|
431
406
|
databasesLength -= 1
|
|
432
407
|
|
|
433
408
|
if (!databasesLength && !collectionsLength) {
|
|
434
|
-
data = createData(data, documents, type
|
|
409
|
+
data = createData(data, documents, type)
|
|
435
410
|
resolve(data)
|
|
436
411
|
}
|
|
437
412
|
});
|
|
@@ -442,11 +417,9 @@ function document(action, data){
|
|
|
442
417
|
return new Promise((resolve, reject) => {
|
|
443
418
|
|
|
444
419
|
collectionObj.find(query).sort(sort).toArray(function(error, result) {
|
|
445
|
-
if (error)
|
|
446
|
-
error
|
|
447
|
-
|
|
448
|
-
errorHandler(error)
|
|
449
|
-
}
|
|
420
|
+
if (error)
|
|
421
|
+
errorHandler(data, error, database, collection)
|
|
422
|
+
|
|
450
423
|
if (data.filter && data.filter.search) {
|
|
451
424
|
let searchResult = []
|
|
452
425
|
|
|
@@ -513,7 +486,7 @@ function document(action, data){
|
|
|
513
486
|
}).then((result) => {
|
|
514
487
|
|
|
515
488
|
}).catch((error) => {
|
|
516
|
-
|
|
489
|
+
errorHandler(data, error, database, collection)
|
|
517
490
|
console.log(action, 'error', error);
|
|
518
491
|
}).finally((error) => {
|
|
519
492
|
docsLength -= 1
|
|
@@ -524,7 +497,7 @@ function document(action, data){
|
|
|
524
497
|
databasesLength -= 1
|
|
525
498
|
|
|
526
499
|
if (!databasesLength && !collectionsLength) {
|
|
527
|
-
data = createData(data, documents, type
|
|
500
|
+
data = createData(data, documents, type)
|
|
528
501
|
resolve(data)
|
|
529
502
|
}
|
|
530
503
|
})
|
|
@@ -539,7 +512,7 @@ function document(action, data){
|
|
|
539
512
|
databasesLength -= 1
|
|
540
513
|
|
|
541
514
|
if (!databasesLength && !collectionsLength) {
|
|
542
|
-
data = createData(data, documents, type
|
|
515
|
+
data = createData(data, documents, type)
|
|
543
516
|
resolve(data)
|
|
544
517
|
}
|
|
545
518
|
}
|
|
@@ -557,7 +530,7 @@ function document(action, data){
|
|
|
557
530
|
databasesLength -= 1
|
|
558
531
|
|
|
559
532
|
if (!databasesLength && !collectionsLength) {
|
|
560
|
-
data = createData(data, documents, type
|
|
533
|
+
data = createData(data, documents, type)
|
|
561
534
|
resolve(data)
|
|
562
535
|
}
|
|
563
536
|
|
|
@@ -570,13 +543,12 @@ function document(action, data){
|
|
|
570
543
|
}
|
|
571
544
|
|
|
572
545
|
} catch(error) {
|
|
573
|
-
|
|
574
|
-
data['error'] = errorLog
|
|
546
|
+
errorHandler(data, error)
|
|
575
547
|
console.log(action, 'error', error);
|
|
576
548
|
resolve(data);
|
|
577
549
|
}
|
|
578
|
-
}, (
|
|
579
|
-
errorHandler(data,
|
|
550
|
+
}, (error) => {
|
|
551
|
+
errorHandler(data, error)
|
|
580
552
|
});
|
|
581
553
|
|
|
582
554
|
}
|
|
@@ -616,10 +588,7 @@ function createUpdate(data, type) {
|
|
|
616
588
|
|
|
617
589
|
}
|
|
618
590
|
|
|
619
|
-
function createData(data, array, type
|
|
620
|
-
if (errorLog.length > 0)
|
|
621
|
-
data['error'] = errorLog
|
|
622
|
-
|
|
591
|
+
function createData(data, array, type) {
|
|
623
592
|
if (!data.request)
|
|
624
593
|
data.request = data[type] || {}
|
|
625
594
|
|