@e2fyi/streams
Version:
Nodejs stream library for various use cases: e.g. Auto-tagging object streams, streaming to mongoDb via mongoose models, etc.
35 lines (30 loc) • 703 B
JavaScript
;
const {PassThrough} = require('stream');
// utility functions for unit testing
module.exports = {
createStream,
createMongooseModel
};
function createStream(data, objectMode = false) {
// create PassThrough stream
var stream = new PassThrough({objectMode});
data.forEach(d => stream.write(d));
stream.end();
return stream;
}
/**
* Fake mongoose for testing
* @return {Object}
*/
function createMongooseModel() {
var ctx = {
outStream: [],
bulkWrite: function(data, opts, cb) {
cb = arguments.length == 2 ? opts : cb;
ctx.outStream.push(data);
cb(null, ctx.outStream);
return Promise.resolve(ctx.outStream);
}
};
return ctx;
}