Skip to content

Instantly share code, notes, and snippets.

@marcbachmann
Created September 13, 2016 16:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcbachmann/217d57171b5c5f1188f8725e0ce837ed to your computer and use it in GitHub Desktop.
Save marcbachmann/217d57171b5c5f1188f8725e0ce837ed to your computer and use it in GitHub Desktop.
Knex update stream
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