How to bufferize efficiently in nodeJS on events from a stream to bulk insert instead of unique insert per record received from the stream. Here's pseudo code I've got in mind:
// Open MongoDB connection
mystream.on('data', (record) => {
// bufferize data into an array
// if the buffer is full (1000 records)
// bulk insert into MongoDB and empty buffer
})
mystream.on('end', () => {
// close connection
})
Does this look realistic? Is there any possible optimization? Existing libraries facilitaties that?
streamapi sounds like the perfect fit, you should look into using a Writable. The size of the buffer may be controlled by setting the highWaterMark. The writable class has afinal()function, which is called once the stream has completed. This could be used to close the db connection.stream.Readable.from()) and then piped into the buffer Writable. Thus, the script won't fetch more data than it can store in its writable buffer.