diff --git a/index.js b/index.js index 020e346..2c7824e 100644 --- a/index.js +++ b/index.js @@ -161,14 +161,14 @@ function Batch (db, codec) { inherits(Batch, AbstractChainedBatch) -Batch.prototype._put = function (key, value) { - key = this.codec.encodeKey(key) - value = this.codec.encodeValue(value) +Batch.prototype._put = function (key, value, options) { + key = this.codec.encodeKey(key, options) + value = this.codec.encodeValue(value, options) this.batch.put(key, value) } -Batch.prototype._del = function (key) { - key = this.codec.encodeKey(key) +Batch.prototype._del = function (key, options) { + key = this.codec.encodeKey(key, options) this.batch.del(key) } diff --git a/test/index.js b/test/index.js index b7c8ce3..992c46a 100644 --- a/test/index.js +++ b/test/index.js @@ -205,6 +205,28 @@ test('chainedBatch.put() encodes key and value', function (t) { encdown(down).batch().put(1, 2) }) +test('chainedBatch.put() takes custom encoding', function (t) { + t.plan(4) + + const expectedKeys = ['"a"', 'a'] + const expectedValues = ['"b"', 'b'] + + const down = { + batch: function () { + return { + put: function (key, value) { + t.is(key, expectedKeys.shift()) + t.is(value, expectedValues.shift()) + } + } + } + } + + encdown(down).batch() + .put('a', 'b', { keyEncoding: 'json', valueEncoding: 'json' }) + .put('a', 'b') +}) + test('chainedBatch.del() encodes key', function (t) { t.plan(1) @@ -221,6 +243,26 @@ test('chainedBatch.del() encodes key', function (t) { encdown(down).batch().del(1) }) +test('chainedBatch.del() takes custom encoding', function (t) { + t.plan(2) + + const expectedKeys = ['"a"', 'a'] + + const down = { + batch: function () { + return { + del: function (key) { + t.is(key, expectedKeys.shift()) + } + } + } + } + + encdown(down).batch() + .del('a', { keyEncoding: 'json' }) + .del('a') +}) + test('chainedBatch.clear() is forwarded to underlying store', function (t) { t.plan(1)