@vbarbarosh/node-helpers 3.14.0 → 3.15.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/package.json +1 -1
- package/src/mongo_stream_upsert.js +5 -25
- package/src/mongo_stream_write.js +51 -0
package/package.json
CHANGED
|
@@ -3,9 +3,9 @@ const stream = require('stream');
|
|
|
3
3
|
const wait_while = require('./wait_while');
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
|
-
* Returns a writable stream for inserting
|
|
6
|
+
* Returns a writable stream for inserting or replacing documents in mongo collection.
|
|
7
7
|
*/
|
|
8
|
-
function mongo_stream_upsert({collection,
|
|
8
|
+
function mongo_stream_upsert({collection, concurrency = 1})
|
|
9
9
|
{
|
|
10
10
|
const errors = [];
|
|
11
11
|
return new stream.Writable({
|
|
@@ -15,12 +15,10 @@ function mongo_stream_upsert({collection, update_if, concurrency = 1})
|
|
|
15
15
|
next();
|
|
16
16
|
},
|
|
17
17
|
destroy: async function (error, next) {
|
|
18
|
-
// console.log('mongo_stream_upsert.destroy', errors);
|
|
19
18
|
await wait_while(() => this.running > 0);
|
|
20
19
|
next();
|
|
21
20
|
},
|
|
22
21
|
write: async function (items, enc, next) {
|
|
23
|
-
// console.log('mongo_stream_upsert.write', errors);
|
|
24
22
|
if (errors.length) {
|
|
25
23
|
next(errors[0]);
|
|
26
24
|
return;
|
|
@@ -32,33 +30,16 @@ function mongo_stream_upsert({collection, update_if, concurrency = 1})
|
|
|
32
30
|
}
|
|
33
31
|
await wait_while(() => this.running >= concurrency);
|
|
34
32
|
const operations = items.map(function (item) {
|
|
35
|
-
if (!update_if) {
|
|
36
|
-
return {
|
|
37
|
-
replaceOne: {
|
|
38
|
-
filter: {_id: item._id},
|
|
39
|
-
replacement: item,
|
|
40
|
-
upsert: true,
|
|
41
|
-
},
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
33
|
return {
|
|
45
|
-
|
|
34
|
+
replaceOne: {
|
|
46
35
|
filter: {_id: item._id},
|
|
47
|
-
|
|
48
|
-
{
|
|
49
|
-
$replaceRoot: {
|
|
50
|
-
newRoot: {
|
|
51
|
-
$cond: [update_if, item, '$$ROOT'],
|
|
52
|
-
},
|
|
53
|
-
},
|
|
54
|
-
},
|
|
55
|
-
]],
|
|
36
|
+
replacement: item,
|
|
56
37
|
upsert: true,
|
|
57
38
|
},
|
|
58
39
|
};
|
|
59
40
|
});
|
|
60
41
|
this.running++;
|
|
61
|
-
Promise.resolve(collection.bulkWrite(operations
|
|
42
|
+
Promise.resolve(collection.bulkWrite(operations)).catch(e => errors.push(e)).finally(() => this.running--);
|
|
62
43
|
next();
|
|
63
44
|
}
|
|
64
45
|
catch (error) {
|
|
@@ -66,7 +47,6 @@ function mongo_stream_upsert({collection, update_if, concurrency = 1})
|
|
|
66
47
|
}
|
|
67
48
|
},
|
|
68
49
|
final: async function (next) {
|
|
69
|
-
// console.log('mongo_stream_upsert.final', errors);
|
|
70
50
|
if (errors.length) {
|
|
71
51
|
next(errors[0]);
|
|
72
52
|
return;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
const Promise = require('bluebird');
|
|
2
|
+
const stream = require('stream');
|
|
3
|
+
const wait_while = require('./wait_while');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Returns a writable stream for `bulkWrite` operations.
|
|
7
|
+
*/
|
|
8
|
+
function mongo_stream_write({collection, concurrency = 1})
|
|
9
|
+
{
|
|
10
|
+
const errors = [];
|
|
11
|
+
return new stream.Writable({
|
|
12
|
+
objectMode: true,
|
|
13
|
+
construct(next) {
|
|
14
|
+
this.running = 0;
|
|
15
|
+
next();
|
|
16
|
+
},
|
|
17
|
+
destroy: async function (error, next) {
|
|
18
|
+
await wait_while(() => this.running > 0);
|
|
19
|
+
next();
|
|
20
|
+
},
|
|
21
|
+
write: async function (operations, enc, next) {
|
|
22
|
+
if (errors.length) {
|
|
23
|
+
next(errors[0]);
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
try {
|
|
27
|
+
if (!Array.isArray(operations)) {
|
|
28
|
+
next(new Error('An array of objects is expected.'));
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
await wait_while(() => this.running >= concurrency);
|
|
32
|
+
this.running++;
|
|
33
|
+
Promise.resolve(collection.bulkWrite(operations)).catch(e => errors.push(e)).finally(() => this.running--);
|
|
34
|
+
next();
|
|
35
|
+
}
|
|
36
|
+
catch (error) {
|
|
37
|
+
next(error);
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
final: async function (next) {
|
|
41
|
+
if (errors.length) {
|
|
42
|
+
next(errors[0]);
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
await wait_while(() => this.running > 0);
|
|
46
|
+
next();
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
module.exports = mongo_stream_write;
|