Created
September 13, 2016 16:47
-
-
Save marcbachmann/217d57171b5c5f1188f8725e0ce837ed to your computer and use it in GitHub Desktop.
Knex update stream
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var stream = require('stream') | |
var util = require('util') | |
module.exports = UpdateStream | |
util.inherits(UpdateStream, stream.Writable) | |
function UpdateStream (trx, tableName, transformMethod) { | |
stream.Writable.call(this, {objectMode: true}) | |
this.trx = trx | |
this.tableName = tableName | |
this.transoformMethod = transformMethod || function (chunk, cb) { cb(null, chunk) } | |
} | |
UpdateStream.prototype._write = function write (chunk, encoding, done) { | |
var self = this | |
this.transformMethod(chunk, function (err, chunk) { | |
self.trx(self.tableName) | |
.transacting(self.trx) | |
.update(chunk.update) | |
.where(chunk.where) | |
.asCallback(done) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment