Skip to content

Commit

Permalink
Support encoding options on chained batch put() and del()
Browse files Browse the repository at this point in the history
  • Loading branch information
vweevers committed Apr 9, 2021
1 parent 375b158 commit 9690e52
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
10 changes: 5 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
42 changes: 42 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)

Expand Down

0 comments on commit 9690e52

Please sign in to comment.