Budibase is an open-source low-code platform for creating internal apps in minutes. Supports PostgreSQL, MySQL, MSSQL, MongoDB, Rest API, Docker, K8s 🚀
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

43 lines
961 B

'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);
}
};