Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

changes put to return key and value passed #46

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
26 changes: 26 additions & 0 deletions test/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -407,3 +407,29 @@ tape('feed is unwrapped in getter', async t => {
t.same(feed, db.feed)
t.end()
})

tape('put returns key & value', async t => {

const db0 = create({ keyEncoding: 'utf8', valueEncoding: 'json' })
const k0 = 'key'
const v0 = { value: 'value' }
const res0 = await db0.put(k0, v0)
t.equals(res0.key, k0)
t.deepEquals(res0.value, v0)

const db1 = create({ keyEncoding: 'utf8', valueEncoding: 'utf8' })
const k1 = 'key'
const v1 = 'value'
const res1 = await db1.put(k1, v1)
t.equals(res1.key, k1)
t.equals(res1.value, v1)

const db2 = create({ keyEncoding: 'binary', valueEncoding: 'binary' })
const k2 = Buffer.from('key')
const v2 = Buffer.from('value')
const res2 = await db2.put(k2, v2)
t.equals(Buffer.compare(res2.key, k2), 0)
t.equals(Buffer.compare(res2.value, v2), 0)

t.end()
})