UNPKG

@e2fyi/streams

Version:

Nodejs stream library for various use cases: e.g. Auto-tagging object streams, streaming to mongoDb via mongoose models, etc.

117 lines (95 loc) 3.48 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JSDoc: Source: src/mongoose_stream.js</title> <script src="scripts/prettify/prettify.js"> </script> <script src="scripts/prettify/lang-css.js"> </script> <!--[if lt IE 9]> <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css"> <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css"> </head> <body> <div id="main"> <h1 class="page-title">Source: src/mongoose_stream.js</h1> <section> <article> <pre class="prettyprint source linenums"><code>`use strict`; const {Transform} = require('stream'); const defaultOpts = {objectMode: true, itemWaterMark: 50}; /** * Settings for MongooseStream. * @memberof module:@e2fyi/streams * @alias MongooseStreamSettings * @typedef {Object} MongooseStreamSettings * @property {Number} [itemWaterMark=50] - The number of item collected before writing to mongodb. * @property {mongoose.Model} model - [mongoose Model]{@link http://mongoosejs.com/docs/models.html}. */ /** * A custom NodeJS Transform stream to mongo via mongoose. * @memberof module:@e2fyi/streams * @extends stream.Transform * @example * var stream2mongo = new MongooseStream({mode: SomeMongooseModel}); * someReadableStreamFromArray([{text: 'abc'}, {text: 'efg'}]) * .pipe(stream2mongo) // writes to mongo (while stream are also passthrough) * .pipe(response); // stream same results back to some request */ class MongooseStream extends Transform { /** * Create a Transform stream which bulkWrite to mongo based on the itemWaterMark. * model (mongoose Model) is a required field. * @param {MongooseStreamSettings} opts Configuration for MongoStream. Default value for `itemWaterMark` is 50. */ constructor(opts = defaultOpts) { opts = Object.assign(opts, defaultOpts); super(opts); this.itemWaterMark = opts.itemWaterMark; this.model = opts.model; this._stack_ = []; if (!opts.model) { throw new Error('Mongoose Model must be defined!'); } } _transform(chunk, encoding, callback) { if (chunk) { this._stack_.push({insertOne: {document: chunk}}); if (this._stack_.length >= this.itemWaterMark) { let job = this._stack_; this._stack_ = []; return this.model.bulkWrite(job, err => { callback(err, chunk); }); } } callback(null, chunk); } _flush(callback) { if (this._stack_.length > 0) { let job = this._stack_; this._stack_ = []; return this.model.bulkWrite(job, err => { callback(err, '\0'); }); } callback(null, '\0'); } } module.exports = MongooseStream; </code></pre> </article> </section> </div> <nav> <h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-@e2fyi_streams.html">@e2fyi/streams</a></li></ul><h3>Classes</h3><ul><li><a href="module-@e2fyi_streams.DocumentTagger.html">DocumentTagger</a></li><li><a href="module-@e2fyi_streams.MongooseStream.html">MongooseStream</a></li></ul> </nav> <br class="clear"> <footer> Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.0</a> on Wed Dec 13 2017 18:11:13 GMT+0800 (+08) </footer> <script> prettyPrint(); </script> <script src="scripts/linenumber.js"> </script> </body> </html>