'use strict'; var inherits = require('inherits'); var Writable = require('readable-stream').Writable; module.exports = WriteStream; inherits(WriteStream, Writable); function WriteStream(db, opts) { if (!(this instanceof WriteStream)) { return new WriteStream(db); } Writable.call(this, { objectMode: true }); this.db = db; this.opts = opts || {}; } WriteStream.prototype._write = function (chunk, _, next) { if (Buffer.isBuffer(chunk)) { chunk = chunk.toString(); } if (typeof chunk === 'string') { try { chunk = JSON.parse(chunk); } catch (e) { return next(e); } } if (Array.isArray(chunk)) { this.db.bulkDocs({docs: chunk}, this.opts).then(function () { next(); }, next); } else if ('_id' in chunk) { this.db.put(chunk, this.opts).then(function () { next(); }, next); } else { this.db.post(chunk, this.opts).then(function () { next(); }, next); } };