Skip to content
This repository was archived by the owner on Dec 1, 2024. It is now read-only.

Commit 0765808

Browse files
authored
Support encoding options on chained batch put() and del() (#717)
Ref #633
1 parent d81a249 commit 0765808

File tree

4 files changed

+55
-13
lines changed

4 files changed

+55
-13
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -289,27 +289,27 @@ db.batch()
289289
.write(function () { console.log('Done!') })
290290
```
291291

292-
<b><code>batch.put(key, value)</code></b>
292+
**`batch.put(key, value[, options])`**
293293

294-
Queue a _put_ operation on the current batch, not committed until a `write()` is called on the batch.
294+
Queue a _put_ operation on the current batch, not committed until a `write()` is called on the batch. The `options` argument, if provided, must be an object and is passed on to the underlying store.
295295

296296
This method may `throw` a `WriteError` if there is a problem with your put (such as the `value` being `null` or `undefined`).
297297

298-
<b><code>batch.del(key)</code></b>
298+
**`batch.del(key[, options])`**
299299

300-
Queue a _del_ operation on the current batch, not committed until a `write()` is called on the batch.
300+
Queue a _del_ operation on the current batch, not committed until a `write()` is called on the batch. The `options` argument, if provided, must be an object and is passed on to the underlying store.
301301

302302
This method may `throw` a `WriteError` if there is a problem with your delete.
303303

304-
<b><code>batch.clear()</code></b>
304+
**`batch.clear()`**
305305

306306
Clear all queued operations on the current batch, any previous operations will be discarded.
307307

308-
<b><code>batch.length</code></b>
308+
**`batch.length`**
309309

310310
The number of queued operations on the current batch.
311311

312-
<b><code>batch.write(\[options]\[, callback])</code></b>
312+
**`batch.write([options][, callback])`**
313313

314314
Commit the queued operations for this batch. All operations not _cleared_ will be written to the underlying store atomically, that is, they will either all succeed or fail with no partial commits.
315315

lib/batch.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,27 @@ function Batch (levelup) {
1212
this.length = 0
1313
}
1414

15-
Batch.prototype.put = function (key, value) {
15+
Batch.prototype.put = function (key, value, options) {
1616
try {
17-
this.batch.put(key, value)
17+
this.batch.put(key, value, options)
1818
} catch (e) {
1919
throw new WriteError(e)
2020
}
2121

22-
this.ops.push({ type: 'put', key, value })
22+
this.ops.push({ ...options, type: 'put', key, value })
2323
this.length++
2424

2525
return this
2626
}
2727

28-
Batch.prototype.del = function (key) {
28+
Batch.prototype.del = function (key, options) {
2929
try {
30-
this.batch.del(key)
30+
this.batch.del(key, options)
3131
} catch (err) {
3232
throw new WriteError(err)
3333
}
3434

35-
this.ops.push({ type: 'del', key })
35+
this.ops.push({ ...options, type: 'del', key })
3636
this.length++
3737

3838
return this

test/chained-batch-encoding-test.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
'use strict'
2+
3+
const concatIterator = require('level-concat-iterator')
4+
const discardable = require('./util/discardable')
5+
6+
module.exports = function (test, testCommon) {
7+
test('chained batch with per-operation options', function (t) {
8+
discardable(t, testCommon, function (db, done) {
9+
let ops
10+
11+
db.once('batch', function (o) {
12+
ops = o
13+
})
14+
15+
db.batch()
16+
.put('a', 'a', { valueEncoding: 'json' })
17+
.put('b', 'b')
18+
.put('"c"', 'c')
19+
.del('c', { keyEncoding: 'json', arbitraryOption: true })
20+
.write(function (err) {
21+
t.ifError(err, 'no write error')
22+
23+
t.same(ops, [
24+
{ type: 'put', key: 'a', value: 'a', valueEncoding: 'json' },
25+
{ type: 'put', key: 'b', value: 'b' },
26+
{ type: 'put', key: '"c"', value: 'c' },
27+
{ type: 'del', key: 'c', keyEncoding: 'json', arbitraryOption: true }
28+
])
29+
30+
concatIterator(db.iterator(), function (err, entries) {
31+
t.ifError(err)
32+
t.same(entries, [
33+
{ key: 'a', value: '"a"' },
34+
{ key: 'b', value: 'b' }
35+
])
36+
done()
37+
})
38+
})
39+
})
40+
})
41+
}

test/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ function suite (options) {
1717
require('./init-test')(test, testCommon)
1818
if (testCommon.encodings) require('./custom-encoding-test')(test, testCommon)
1919
if (testCommon.encodings) require('./json-encoding-test')(test, testCommon)
20+
if (testCommon.encodings) require('./chained-batch-encoding-test')(test, testCommon)
2021
if (testCommon.streams) require('./key-value-streams-test')(test, testCommon)
2122
require('./maybe-error-test')(test, testCommon)
2223
require('./no-encoding-test')(test, testCommon)

0 commit comments

Comments
 (0)