@cocreate/mongodb 1.23.1 → 1.24.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 +5 -5
- package/src/index.js +199 -477
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
|
+
## [1.24.1](https://github.com/CoCreate-app/CoCreate-mongodb/compare/v1.24.0...v1.24.1) (2026-07-17)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* bump dependencies ([14f893d](https://github.com/CoCreate-app/CoCreate-mongodb/commit/14f893d59fffc82b979226d3259bd5bfa7b122d5))
|
|
7
|
+
|
|
8
|
+
# [1.24.0](https://github.com/CoCreate-app/CoCreate-mongodb/compare/v1.23.1...v1.24.0) (2026-07-17)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Features
|
|
12
|
+
|
|
13
|
+
* streamline database and array operations with async/await and improved error handling ([33475ea](https://github.com/CoCreate-app/CoCreate-mongodb/commit/33475eac9fb6444a91fd1a9386976abf09dde7f0))
|
|
14
|
+
|
|
1
15
|
## [1.23.1](https://github.com/CoCreate-app/CoCreate-mongodb/compare/v1.23.0...v1.23.1) (2026-07-17)
|
|
2
16
|
|
|
3
17
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cocreate/mongodb",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.24.1",
|
|
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",
|
|
@@ -16,9 +16,9 @@
|
|
|
16
16
|
"access": "public"
|
|
17
17
|
},
|
|
18
18
|
"scripts": {
|
|
19
|
-
"start": "
|
|
20
|
-
"build": "
|
|
21
|
-
"dev": "
|
|
19
|
+
"start": "webpack --config webpack.config.js",
|
|
20
|
+
"build": "webpack --mode=production --config webpack.config.js",
|
|
21
|
+
"dev": "webpack --config webpack.config.js --watch"
|
|
22
22
|
},
|
|
23
23
|
"repository": {
|
|
24
24
|
"type": "git",
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
"type": "module",
|
|
38
38
|
"main": "./src/index.js",
|
|
39
39
|
"dependencies": {
|
|
40
|
-
"@cocreate/utils": "^1.
|
|
40
|
+
"@cocreate/utils": "^1.44.0",
|
|
41
41
|
"mongodb": "^7.5.0"
|
|
42
42
|
}
|
|
43
43
|
}
|
package/src/index.js
CHANGED
|
@@ -18,10 +18,7 @@ async function dbClient(data) {
|
|
|
18
18
|
try {
|
|
19
19
|
if (!organizations[data.organization_id][data.storageUrl])
|
|
20
20
|
organizations[data.organization_id][data.storageUrl] =
|
|
21
|
-
MongoClient.connect(data.storageUrl
|
|
22
|
-
useNewUrlParser: true,
|
|
23
|
-
useUnifiedTopology: true
|
|
24
|
-
});
|
|
21
|
+
MongoClient.connect(data.storageUrl);
|
|
25
22
|
|
|
26
23
|
organizations[data.organization_id][data.storageUrl] =
|
|
27
24
|
await organizations[data.organization_id][data.storageUrl];
|
|
@@ -62,48 +59,56 @@ function database(method, data) {
|
|
|
62
59
|
// TODO: support if a database name is defined then return the database details and stats
|
|
63
60
|
// else apply filter and return dbs for which each should have stats and details
|
|
64
61
|
// if (data.database) {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
database,
|
|
82
|
-
storage: data.storageName
|
|
83
|
-
});
|
|
84
|
-
} else
|
|
62
|
+
// const db = client.db(data.organization_id)
|
|
63
|
+
// const stats = await db.stats()
|
|
64
|
+
// }
|
|
65
|
+
|
|
66
|
+
// List all the available databases
|
|
67
|
+
try {
|
|
68
|
+
const dbs = await db.listDatabases();
|
|
69
|
+
dataTransferedIn += getBytes(dbs);
|
|
70
|
+
|
|
71
|
+
for (let database of dbs.databases) {
|
|
72
|
+
if (data.$filter && data.$filter.query) {
|
|
73
|
+
let isFilter = queryData(
|
|
74
|
+
database,
|
|
75
|
+
data.$filter.query
|
|
76
|
+
);
|
|
77
|
+
if (isFilter)
|
|
85
78
|
databaseArray.push({
|
|
86
79
|
database,
|
|
87
80
|
storage: data.storageName
|
|
88
81
|
});
|
|
89
|
-
}
|
|
82
|
+
} else
|
|
83
|
+
databaseArray.push({
|
|
84
|
+
database,
|
|
85
|
+
storage: data.storageName
|
|
86
|
+
});
|
|
87
|
+
}
|
|
90
88
|
|
|
91
|
-
|
|
92
|
-
|
|
89
|
+
resolve(createData(data, databaseArray, type));
|
|
90
|
+
} catch (error) {
|
|
91
|
+
errorHandler(data, error);
|
|
92
|
+
resolve(data);
|
|
93
93
|
}
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
94
|
+
}
|
|
95
|
+
if (method == "delete") {
|
|
96
|
+
const db = client.db(data.database);
|
|
97
|
+
try {
|
|
98
|
+
const result = await db.dropDatabase();
|
|
99
|
+
dataTransferedIn += getBytes(result);
|
|
100
|
+
resolve(result);
|
|
101
|
+
} catch(error) {
|
|
102
|
+
errorHandler(data, error);
|
|
103
|
+
resolve(data);
|
|
100
104
|
}
|
|
101
|
-
} catch (error) {
|
|
102
|
-
errorHandler(data, error);
|
|
103
|
-
console.log(method, "error", error);
|
|
104
|
-
resolve(data);
|
|
105
105
|
}
|
|
106
|
-
}
|
|
106
|
+
} catch (error) {
|
|
107
|
+
errorHandler(data, error);
|
|
108
|
+
console.log(method, "error", error);
|
|
109
|
+
resolve(data);
|
|
110
|
+
}
|
|
111
|
+
},
|
|
107
112
|
(error) => {
|
|
108
113
|
errorHandler(data, error);
|
|
109
114
|
}
|
|
@@ -120,23 +125,21 @@ function array(method, data) {
|
|
|
120
125
|
|
|
121
126
|
try {
|
|
122
127
|
const client = await dbClient(data);
|
|
123
|
-
if (!client || client.status === false) return data;
|
|
128
|
+
if (!client || client.status === false) return resolve(data);
|
|
124
129
|
|
|
125
130
|
if (data.request) data.array = data.request;
|
|
126
131
|
|
|
127
132
|
let databases = data.database;
|
|
128
133
|
if (!Array.isArray(databases)) databases = [databases];
|
|
129
134
|
|
|
130
|
-
let databasesLength = databases.length;
|
|
131
135
|
for (let database of databases) {
|
|
132
136
|
const db = client.db(database);
|
|
133
137
|
|
|
134
138
|
if (method == "read") {
|
|
135
139
|
let { query, sort } = await createFilter(data);
|
|
136
140
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
141
|
+
try {
|
|
142
|
+
const result = await db.listCollections().toArray();
|
|
140
143
|
if (result) {
|
|
141
144
|
dataTransferedIn += getBytes(result);
|
|
142
145
|
|
|
@@ -160,19 +163,9 @@ function array(method, data) {
|
|
|
160
163
|
});
|
|
161
164
|
}
|
|
162
165
|
}
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
data = createData(
|
|
167
|
-
data,
|
|
168
|
-
arrayArray,
|
|
169
|
-
type,
|
|
170
|
-
dataTransferedIn,
|
|
171
|
-
dataTransferedOut
|
|
172
|
-
);
|
|
173
|
-
resolve(data);
|
|
174
|
-
}
|
|
175
|
-
});
|
|
166
|
+
} catch (error) {
|
|
167
|
+
errorHandler(data, error, database);
|
|
168
|
+
}
|
|
176
169
|
} else {
|
|
177
170
|
let arrays;
|
|
178
171
|
let value;
|
|
@@ -182,46 +175,22 @@ function array(method, data) {
|
|
|
182
175
|
|
|
183
176
|
if (!Array.isArray(arrays)) arrays = [arrays];
|
|
184
177
|
|
|
185
|
-
let arraysLength = arrays.length;
|
|
186
178
|
for (let array of arrays) {
|
|
187
179
|
if (method == "create") {
|
|
188
180
|
dataTransferedOut += getBytes(array);
|
|
189
|
-
|
|
190
|
-
array
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
);
|
|
199
|
-
|
|
200
|
-
if (result) {
|
|
201
|
-
dataTransferedIn +=
|
|
202
|
-
getBytes(result);
|
|
203
|
-
arrayArray.push({
|
|
204
|
-
name: array,
|
|
205
|
-
database,
|
|
206
|
-
storage: data.storageName
|
|
207
|
-
});
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
arraysLength -= 1;
|
|
211
|
-
if (!arraysLength) databasesLength -= 1;
|
|
212
|
-
|
|
213
|
-
if (!databasesLength && !arraysLength) {
|
|
214
|
-
data = createData(
|
|
215
|
-
data,
|
|
216
|
-
arrayArray,
|
|
217
|
-
type,
|
|
218
|
-
dataTransferedIn,
|
|
219
|
-
dataTransferedOut
|
|
220
|
-
);
|
|
221
|
-
resolve(data);
|
|
222
|
-
}
|
|
181
|
+
try {
|
|
182
|
+
const result = await db.createCollection(array);
|
|
183
|
+
if (result) {
|
|
184
|
+
dataTransferedIn += getBytes(result);
|
|
185
|
+
arrayArray.push({
|
|
186
|
+
name: array,
|
|
187
|
+
database,
|
|
188
|
+
storage: data.storageName
|
|
189
|
+
});
|
|
223
190
|
}
|
|
224
|
-
)
|
|
191
|
+
} catch (error) {
|
|
192
|
+
errorHandler(data, error, database, array);
|
|
193
|
+
}
|
|
225
194
|
} else {
|
|
226
195
|
if (method == "update") {
|
|
227
196
|
[array, value] = array;
|
|
@@ -230,96 +199,56 @@ function array(method, data) {
|
|
|
230
199
|
const arrayObj = db.collection(array);
|
|
231
200
|
|
|
232
201
|
if (method == "update") {
|
|
233
|
-
dataTransferedOut += getBytes(
|
|
234
|
-
|
|
235
|
-
value
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
if (result) {
|
|
246
|
-
dataTransferedIn +=
|
|
247
|
-
getBytes(result);
|
|
248
|
-
arrayArray.push({
|
|
249
|
-
name: value,
|
|
250
|
-
oldName: array,
|
|
251
|
-
database,
|
|
252
|
-
storage: data.storageName
|
|
253
|
-
});
|
|
254
|
-
}
|
|
255
|
-
arraysLength -= 1;
|
|
256
|
-
if (!arraysLength)
|
|
257
|
-
databasesLength -= 1;
|
|
258
|
-
|
|
259
|
-
if (
|
|
260
|
-
!databasesLength &&
|
|
261
|
-
!arraysLength
|
|
262
|
-
) {
|
|
263
|
-
data = createData(
|
|
264
|
-
data,
|
|
265
|
-
arrayArray,
|
|
266
|
-
type,
|
|
267
|
-
dataTransferedIn,
|
|
268
|
-
dataTransferedOut
|
|
269
|
-
);
|
|
270
|
-
resolve(data);
|
|
271
|
-
}
|
|
202
|
+
dataTransferedOut += getBytes({ rename: value });
|
|
203
|
+
try {
|
|
204
|
+
const result = await arrayObj.rename(value);
|
|
205
|
+
if (result) {
|
|
206
|
+
dataTransferedIn += getBytes(result);
|
|
207
|
+
arrayArray.push({
|
|
208
|
+
name: value,
|
|
209
|
+
oldName: array,
|
|
210
|
+
database,
|
|
211
|
+
storage: data.storageName
|
|
212
|
+
});
|
|
272
213
|
}
|
|
273
|
-
)
|
|
214
|
+
} catch (error) {
|
|
215
|
+
errorHandler(data, error, database, array);
|
|
216
|
+
}
|
|
274
217
|
}
|
|
275
218
|
|
|
276
219
|
if (method == "delete") {
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
errorHandler(
|
|
280
|
-
data,
|
|
281
|
-
error,
|
|
282
|
-
database,
|
|
283
|
-
array
|
|
284
|
-
);
|
|
285
|
-
|
|
220
|
+
try {
|
|
221
|
+
const result = await arrayObj.drop();
|
|
286
222
|
if (result) {
|
|
287
|
-
dataTransferedOut +=
|
|
288
|
-
getBytes(result);
|
|
223
|
+
dataTransferedOut += getBytes(result);
|
|
289
224
|
arrayArray.push({
|
|
290
225
|
name: array,
|
|
291
226
|
database,
|
|
292
227
|
storage: data.storageName
|
|
293
228
|
});
|
|
294
229
|
}
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
if (!databasesLength && !arraysLength) {
|
|
300
|
-
data = createData(
|
|
301
|
-
data,
|
|
302
|
-
arrayArray,
|
|
303
|
-
type,
|
|
304
|
-
dataTransferedIn,
|
|
305
|
-
dataTransferedOut
|
|
306
|
-
);
|
|
307
|
-
resolve(data);
|
|
308
|
-
}
|
|
309
|
-
});
|
|
230
|
+
} catch (error) {
|
|
231
|
+
errorHandler(data, error, database, array);
|
|
232
|
+
}
|
|
310
233
|
}
|
|
311
234
|
}
|
|
312
235
|
}
|
|
313
236
|
}
|
|
314
237
|
}
|
|
238
|
+
|
|
239
|
+
data = createData(
|
|
240
|
+
data,
|
|
241
|
+
arrayArray,
|
|
242
|
+
type,
|
|
243
|
+
dataTransferedIn,
|
|
244
|
+
dataTransferedOut
|
|
245
|
+
);
|
|
246
|
+
resolve(data);
|
|
315
247
|
} catch (error) {
|
|
316
248
|
errorHandler(data, error);
|
|
317
249
|
console.log(method, "error", error);
|
|
318
250
|
resolve(data);
|
|
319
251
|
}
|
|
320
|
-
},
|
|
321
|
-
(error) => {
|
|
322
|
-
errorHandler(data, error);
|
|
323
252
|
}
|
|
324
253
|
);
|
|
325
254
|
}
|
|
@@ -330,7 +259,7 @@ function object(method, data) {
|
|
|
330
259
|
try {
|
|
331
260
|
const client = await dbClient(data);
|
|
332
261
|
|
|
333
|
-
if (!client || client.status === false) return data;
|
|
262
|
+
if (!client || client.status === false) return resolve(data);
|
|
334
263
|
|
|
335
264
|
let dataTransferedIn = 0;
|
|
336
265
|
let dataTransferedOut = 0;
|
|
@@ -386,7 +315,7 @@ function object(method, data) {
|
|
|
386
315
|
let $storage = data[type][i].$storage || [];
|
|
387
316
|
let $database = data[type][i].$database || [];
|
|
388
317
|
let $array = data[type][i].$array || [];
|
|
389
|
-
let update = {...globalUpdate};
|
|
318
|
+
let update = { ...globalUpdate };
|
|
390
319
|
|
|
391
320
|
if (!Array.isArray($storage))
|
|
392
321
|
$storage = [data[type][i].$storage];
|
|
@@ -417,7 +346,10 @@ function object(method, data) {
|
|
|
417
346
|
let { query, sort, index, limit } = filter;
|
|
418
347
|
|
|
419
348
|
if (method === "create") {
|
|
420
|
-
|
|
349
|
+
// Safety check for replaceArray execution
|
|
350
|
+
if (typeof replaceArray === 'function') {
|
|
351
|
+
data[type][i] = replaceArray(data[type][i]);
|
|
352
|
+
}
|
|
421
353
|
data[type][i] = dotNotationToObject(
|
|
422
354
|
data[type][i]
|
|
423
355
|
);
|
|
@@ -427,8 +359,6 @@ function object(method, data) {
|
|
|
427
359
|
on: new Date(data.timeStamp),
|
|
428
360
|
by: data.user_id || data.clientId
|
|
429
361
|
};
|
|
430
|
-
} else if (method === "read") {
|
|
431
|
-
// projection = createProjection(projection, data[type][i]);
|
|
432
362
|
} else if (method === "update") {
|
|
433
363
|
if (!data[type][i].modified)
|
|
434
364
|
data[type][i].modified = {
|
|
@@ -445,16 +375,18 @@ function object(method, data) {
|
|
|
445
375
|
createUpdate(update, options, data[type][i]);
|
|
446
376
|
}
|
|
447
377
|
|
|
448
|
-
if (data[type][i]._id || method === "create") {
|
|
378
|
+
if (data[type][i]._id || method === "create" || isFilter) {
|
|
449
379
|
if (method !== "create") {
|
|
450
380
|
try {
|
|
451
|
-
|
|
381
|
+
if (data[type][i]._id) {
|
|
382
|
+
query._id = new ObjectId(data[type][i]._id);
|
|
383
|
+
}
|
|
452
384
|
} catch (error) {
|
|
453
385
|
if (
|
|
454
386
|
method === "update" &&
|
|
455
387
|
options.upsert
|
|
456
388
|
) {
|
|
457
|
-
data[type][i]._id = ObjectId();
|
|
389
|
+
data[type][i]._id = new ObjectId();
|
|
458
390
|
query._id = data[type][i]._id;
|
|
459
391
|
} else {
|
|
460
392
|
errorHandler(
|
|
@@ -480,10 +412,9 @@ function object(method, data) {
|
|
|
480
412
|
if (method === "create") {
|
|
481
413
|
if (data[type][i]._id) {
|
|
482
414
|
try {
|
|
483
|
-
data[type][i]._id =
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
);
|
|
415
|
+
data[type][i]._id = new ObjectId(
|
|
416
|
+
data[type][i]._id
|
|
417
|
+
);
|
|
487
418
|
} catch (error) {
|
|
488
419
|
delete data[type][i]._id;
|
|
489
420
|
}
|
|
@@ -491,338 +422,132 @@ function object(method, data) {
|
|
|
491
422
|
result = await arrayObj.insertOne(
|
|
492
423
|
data[type][i]
|
|
493
424
|
);
|
|
494
|
-
// TODO: type error occuring when pushing the item pushes but throws an error
|
|
495
425
|
data[type][i]._id =
|
|
496
426
|
result.insertedId.toString();
|
|
497
|
-
|
|
498
|
-
} else
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
if (
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
if (
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
);
|
|
515
|
-
data[type][i]._id =
|
|
516
|
-
result.insertedId.toString();
|
|
517
|
-
} else if (
|
|
518
|
-
result &&
|
|
519
|
-
new Date(
|
|
520
|
-
data[type][i].modified.on
|
|
521
|
-
) > new Date(result.modified.on)
|
|
522
|
-
) {
|
|
523
|
-
data[type][i] = {
|
|
524
|
-
...result,
|
|
525
|
-
...data[type][i]
|
|
526
|
-
};
|
|
527
|
-
createUpdate(
|
|
528
|
-
update,
|
|
529
|
-
options,
|
|
530
|
-
data[type][i]
|
|
427
|
+
documents.push({ ...data[type][i], ...reference });
|
|
428
|
+
} else {
|
|
429
|
+
// Modern cursor iteration for v7.5
|
|
430
|
+
let cursor = arrayObj.find(query, { projection });
|
|
431
|
+
if (sort && Object.keys(sort).length > 0) cursor = cursor.sort(sort);
|
|
432
|
+
if (limit) cursor = cursor.limit(limit);
|
|
433
|
+
|
|
434
|
+
let hasDocuments = false;
|
|
435
|
+
|
|
436
|
+
for await (const document of cursor) {
|
|
437
|
+
hasDocuments = true;
|
|
438
|
+
dataTransferedIn += getBytes(document);
|
|
439
|
+
|
|
440
|
+
if (data.$filter && data.$filter.search) {
|
|
441
|
+
let isMatch = searchData(
|
|
442
|
+
document,
|
|
443
|
+
data.$filter.search
|
|
531
444
|
);
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
query,
|
|
535
|
-
update,
|
|
536
|
-
options
|
|
537
|
-
);
|
|
538
|
-
} else
|
|
539
|
-
data[type][i] = {
|
|
540
|
-
...data[type][i],
|
|
541
|
-
...result
|
|
542
|
-
};
|
|
543
|
-
} else if (result)
|
|
544
|
-
data[type][i] = {
|
|
545
|
-
...data[type][i],
|
|
546
|
-
...result
|
|
547
|
-
};
|
|
548
|
-
else {
|
|
549
|
-
data[type].splice(i, 1);
|
|
550
|
-
continue;
|
|
551
|
-
}
|
|
552
|
-
} else if (method === "update") {
|
|
553
|
-
if (
|
|
554
|
-
update["$pull"] &&
|
|
555
|
-
update["$unset"]
|
|
556
|
-
) {
|
|
557
|
-
result = await arrayObj.updateOne(
|
|
558
|
-
query,
|
|
559
|
-
{ $unset: update["$unset"] },
|
|
560
|
-
options
|
|
561
|
-
);
|
|
562
|
-
delete update["$unset"];
|
|
563
|
-
}
|
|
564
|
-
|
|
565
|
-
result = await arrayObj.updateOne(
|
|
566
|
-
query,
|
|
567
|
-
update,
|
|
568
|
-
options
|
|
569
|
-
);
|
|
570
|
-
|
|
571
|
-
// TODO: handle upsert false and id does not exist
|
|
572
|
-
data[type][i]._id =
|
|
573
|
-
query._id.toString();
|
|
574
|
-
// documents.push({ ...data[type][i], ...reference })
|
|
575
|
-
} else if (method === "delete") {
|
|
576
|
-
result = await arrayObj.deleteOne(
|
|
577
|
-
query
|
|
578
|
-
);
|
|
579
|
-
// documents.push({ ...reference, _id: data[type][i]._id })
|
|
580
|
-
}
|
|
581
|
-
|
|
582
|
-
data[type][i].$storage = $storage;
|
|
583
|
-
data[type][i].$database = $database;
|
|
584
|
-
data[type][i].$array = $array;
|
|
585
|
-
|
|
586
|
-
dataTransferedIn += getBytes(result);
|
|
587
|
-
} catch (error) {
|
|
588
|
-
errorHandler(data, error, database, array);
|
|
589
|
-
}
|
|
590
|
-
} else if (isFilter) {
|
|
591
|
-
try {
|
|
592
|
-
// TODO: index is 1 if indexeddb already returned an item interfering with query
|
|
593
|
-
// if (data.array === "keys")
|
|
594
|
-
// index = 0
|
|
595
|
-
|
|
596
|
-
if (method === "read")
|
|
597
|
-
projection = {
|
|
598
|
-
...projections,
|
|
599
|
-
...projection
|
|
600
|
-
};
|
|
601
|
-
|
|
602
|
-
if (
|
|
603
|
-
data.$filter &&
|
|
604
|
-
data.$filter.startingIndex >= 0
|
|
605
|
-
)
|
|
606
|
-
index = data.$filter.startingIndex;
|
|
607
|
-
|
|
608
|
-
dataTransferedOut += getBytes({
|
|
609
|
-
query,
|
|
610
|
-
projection,
|
|
611
|
-
sort,
|
|
612
|
-
index,
|
|
613
|
-
limit
|
|
614
|
-
});
|
|
615
|
-
|
|
616
|
-
let cursor,
|
|
617
|
-
document = "";
|
|
618
|
-
if (Object.keys(sort).length > 0)
|
|
619
|
-
cursor = arrayObj
|
|
620
|
-
.find(query, { projection })
|
|
621
|
-
.sort(sort)
|
|
622
|
-
.skip(index)
|
|
623
|
-
.limit(limit)
|
|
624
|
-
.allowDiskUse(true);
|
|
625
|
-
else
|
|
626
|
-
cursor = arrayObj
|
|
627
|
-
.find(query, { projection })
|
|
628
|
-
.sort(sort)
|
|
629
|
-
.skip(index)
|
|
630
|
-
.limit(limit);
|
|
631
|
-
|
|
632
|
-
if (
|
|
633
|
-
!(await cursor.hasNext()) &&
|
|
634
|
-
method === "update" &&
|
|
635
|
-
data.upsert
|
|
636
|
-
)
|
|
637
|
-
document = {
|
|
638
|
-
_id: ObjectId(data[type][i]._id)
|
|
639
|
-
};
|
|
640
|
-
|
|
641
|
-
while (
|
|
642
|
-
(await cursor.hasNext()) ||
|
|
643
|
-
document
|
|
644
|
-
) {
|
|
645
|
-
if (!document)
|
|
646
|
-
document = await cursor.next();
|
|
647
|
-
|
|
648
|
-
dataTransferedIn += getBytes(document);
|
|
445
|
+
if (!isMatch) continue;
|
|
446
|
+
}
|
|
649
447
|
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
);
|
|
658
|
-
if (!isMatch) continue;
|
|
659
|
-
}
|
|
448
|
+
if (method === "read") {
|
|
449
|
+
document._id = document._id.toString();
|
|
450
|
+
let object = data[type].find(
|
|
451
|
+
(obj) =>
|
|
452
|
+
obj._id &&
|
|
453
|
+
obj._id.toString() === document._id.toString()
|
|
454
|
+
);
|
|
660
455
|
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
if (
|
|
672
|
-
object.$storage &&
|
|
673
|
-
object.modified &&
|
|
674
|
-
object.modified.on
|
|
675
|
-
) {
|
|
676
|
-
if (
|
|
677
|
-
document &&
|
|
678
|
-
document.modified &&
|
|
679
|
-
new Date(
|
|
680
|
-
object.modified.on
|
|
681
|
-
) >
|
|
682
|
-
new Date(
|
|
683
|
-
document.modified.on
|
|
684
|
-
)
|
|
685
|
-
) {
|
|
686
|
-
object = {
|
|
687
|
-
...document,
|
|
688
|
-
...object
|
|
689
|
-
};
|
|
690
|
-
createUpdate(
|
|
691
|
-
update,
|
|
692
|
-
options,
|
|
693
|
-
object
|
|
456
|
+
if (object && object.$storage && object.modified && object.modified.on) {
|
|
457
|
+
if (document.modified && new Date(object.modified.on) > new Date(document.modified.on)) {
|
|
458
|
+
object = { ...document, ...object };
|
|
459
|
+
let docUpdate = {};
|
|
460
|
+
let docOptions = {};
|
|
461
|
+
createUpdate(docUpdate, docOptions, object);
|
|
462
|
+
let updateResult = await arrayObj.updateOne(
|
|
463
|
+
{ _id: new ObjectId(document._id) },
|
|
464
|
+
docUpdate,
|
|
465
|
+
docOptions
|
|
694
466
|
);
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
documents.push({
|
|
705
|
-
...document,
|
|
706
|
-
...reference
|
|
707
|
-
});
|
|
708
|
-
} else
|
|
709
|
-
documents.push({
|
|
710
|
-
...document,
|
|
711
|
-
...reference
|
|
712
|
-
});
|
|
713
|
-
} else
|
|
714
|
-
documents.push({
|
|
715
|
-
...document,
|
|
716
|
-
...reference
|
|
467
|
+
dataTransferedIn += getBytes(updateResult);
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
documents.push({ ...document, ...reference });
|
|
471
|
+
} else {
|
|
472
|
+
dataTransferedOut += getBytes({
|
|
473
|
+
_id: document._id,
|
|
474
|
+
update,
|
|
475
|
+
options
|
|
717
476
|
});
|
|
718
|
-
} else {
|
|
719
|
-
dataTransferedOut += getBytes({
|
|
720
|
-
_id: document._id,
|
|
721
|
-
update,
|
|
722
|
-
options
|
|
723
|
-
});
|
|
724
477
|
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
) {
|
|
731
|
-
result =
|
|
732
|
-
await arrayObj.updateOne(
|
|
733
|
-
{
|
|
734
|
-
_id: document._id
|
|
735
|
-
},
|
|
736
|
-
{
|
|
737
|
-
$unset: update[
|
|
738
|
-
"$unset"
|
|
739
|
-
]
|
|
740
|
-
},
|
|
478
|
+
if (method === "update") {
|
|
479
|
+
if (update["$pull"] && update["$unset"]) {
|
|
480
|
+
result = await arrayObj.updateOne(
|
|
481
|
+
{ _id: document._id },
|
|
482
|
+
{ $unset: update["$unset"] },
|
|
741
483
|
options
|
|
742
484
|
);
|
|
743
|
-
|
|
744
|
-
|
|
485
|
+
delete update["$unset"];
|
|
486
|
+
}
|
|
745
487
|
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
await arrayObj.findOneAndUpdate(
|
|
749
|
-
{
|
|
750
|
-
_id: document._id
|
|
751
|
-
},
|
|
488
|
+
if (options.returnNewDocument) {
|
|
489
|
+
// returnDocument: 'after' replaces returnOriginal: false for v7.5
|
|
490
|
+
let updatedObj = await arrayObj.findOneAndUpdate(
|
|
491
|
+
{ _id: document._id },
|
|
752
492
|
update,
|
|
753
|
-
options
|
|
493
|
+
{ ...options, returnDocument: 'after' }
|
|
754
494
|
);
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
"[]",
|
|
772
|
-
`[${index}]`
|
|
773
|
-
)
|
|
774
|
-
] =
|
|
775
|
-
data[type][i][
|
|
776
|
-
newArrayKey
|
|
777
|
-
];
|
|
778
|
-
}
|
|
779
|
-
} else
|
|
780
|
-
result =
|
|
781
|
-
await arrayObj.updateOne(
|
|
782
|
-
{
|
|
783
|
-
_id: document._id
|
|
784
|
-
},
|
|
495
|
+
|
|
496
|
+
if (updatedObj && updatedObj.value) {
|
|
497
|
+
for (let key of Object.keys(updatedObj.value)) {
|
|
498
|
+
if (key === "_id" || !options.newArray) continue;
|
|
499
|
+
let newArrayKey = options.newArray[key];
|
|
500
|
+
if (newArrayKey) {
|
|
501
|
+
let idx = updatedObj.value[key].length - 1;
|
|
502
|
+
if (idx >= 0) {
|
|
503
|
+
data[type][i][newArrayKey.replace("[]", `[${idx}]`)] = data[type][i][newArrayKey];
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
} else {
|
|
509
|
+
result = await arrayObj.updateOne(
|
|
510
|
+
{ _id: document._id },
|
|
785
511
|
update,
|
|
786
512
|
options
|
|
787
513
|
);
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
await arrayObj.deleteOne({
|
|
514
|
+
}
|
|
515
|
+
} else if (method === "delete") {
|
|
516
|
+
result = await arrayObj.deleteOne({
|
|
792
517
|
_id: document._id
|
|
793
518
|
});
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
dataTransferedIn += getBytes(result);
|
|
522
|
+
documents.push({
|
|
523
|
+
...data[type][i],
|
|
524
|
+
...reference,
|
|
525
|
+
_id: document._id.toString()
|
|
526
|
+
});
|
|
794
527
|
}
|
|
528
|
+
}
|
|
795
529
|
|
|
796
|
-
|
|
797
|
-
|
|
530
|
+
// Upsert fallback if missing
|
|
531
|
+
if (!hasDocuments && method === "update" && (data.upsert || options.upsert)) {
|
|
532
|
+
let upsertId = data[type][i]._id ? new ObjectId(data[type][i]._id) : new ObjectId();
|
|
533
|
+
let result = await arrayObj.updateOne(
|
|
534
|
+
{ _id: upsertId },
|
|
535
|
+
update,
|
|
536
|
+
{ ...options, upsert: true }
|
|
537
|
+
);
|
|
538
|
+
dataTransferedIn += getBytes(result);
|
|
798
539
|
documents.push({
|
|
799
540
|
...data[type][i],
|
|
800
541
|
...reference,
|
|
801
|
-
_id:
|
|
542
|
+
_id: upsertId.toString()
|
|
802
543
|
});
|
|
803
|
-
// data[type].push({ ...data[type][i], ...reference, _id: document._id.toString() })
|
|
804
544
|
}
|
|
805
|
-
document = "";
|
|
806
545
|
}
|
|
807
546
|
} catch (error) {
|
|
808
547
|
errorHandler(data, error, database, array);
|
|
809
548
|
}
|
|
810
549
|
}
|
|
811
550
|
}
|
|
812
|
-
|
|
813
|
-
// if (method === 'create') {
|
|
814
|
-
// try {
|
|
815
|
-
// dataTransferedOut += getBytes(data[type])
|
|
816
|
-
// const result = await arrayObj.insertMany(data[type]);
|
|
817
|
-
// dataTransferedIn += getBytes(result)
|
|
818
|
-
|
|
819
|
-
// for (let i = 0; i < data[type].length; i++) {
|
|
820
|
-
// data[type][i]._id = data[type][i]._id.toString()
|
|
821
|
-
// }
|
|
822
|
-
// } catch (error) {
|
|
823
|
-
// errorHandler(data, error, database, array)
|
|
824
|
-
// }
|
|
825
|
-
// }
|
|
826
551
|
}
|
|
827
552
|
}
|
|
828
553
|
data = createData(
|
|
@@ -838,9 +563,6 @@ function object(method, data) {
|
|
|
838
563
|
console.log(method, "error", error);
|
|
839
564
|
resolve(data);
|
|
840
565
|
}
|
|
841
|
-
},
|
|
842
|
-
(error) => {
|
|
843
|
-
errorHandler(data, error);
|
|
844
566
|
}
|
|
845
567
|
);
|
|
846
568
|
}
|