Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,9 @@ class Batch {
}

_append (root, seq, key, value) {
const ret = { seq, key: null, value: null }
if (key) ret.key = (this.keyEncoding) ? this.keyEncoding.decode(key) : key
if (value) ret.value = (this.valueEncoding) ? this.valueEncoding.decode(value) : value
const index = []
root.indexChanges(index, seq)
index[0] = new Child(seq, 0, root)
Expand All @@ -771,14 +774,14 @@ class Batch {
this.root = root
this.length++
this.blocks.set(seq, block)
return
return ret
}

return this._appendBatch(Node.encode({
key,
value,
index: deflate(index)
}))
})).then(() => ret)
}

async _appendBatch (raw) {
Expand Down
41 changes: 41 additions & 0 deletions test/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -407,3 +407,44 @@ tape('feed is unwrapped in getter', async t => {
t.same(feed, db.feed)
t.end()
})

tape('put returns key & value', async t => {
const tests = [
{
opts: {},
key: Buffer.from('key'),
value: Buffer.from('value'),
t: (result, key, value) => {
t.equals(Buffer.compare(result.key, key), 0)
t.equals(Buffer.compare(result.value, value), 0)
}
},
{
opts: { keyEncoding: 'utf8', valueEncoding: 'utf8' },
key: 'key',
value: 'value',
t: (result, key, value) => {
t.equals(result.key, key)
t.equals(result.value, value)
}
},
{
opts: { keyEncoding: 'utf8', valueEncoding: 'json' },
key: 'key',
value: { value: 'value' },
t: (result, key, value) => {
t.equals(result.key, key)
t.deepEquals(result.value, value)
}
}
]
const Hypercore = require('hypercore')
for (const test of tests) {
const feed = new Hypercore(require('random-access-memory'))
const db = new Hyperbee(feed, test.opts)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use the create helper

await db.ready()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not needed

const result = await db.put(test.key, test.value)
test.t(result, test.key, test.value)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually this loop doesn't help much with clarity. just duplicate the creation between three independent tests instead and use the create helper :)

t.end()
})