bdb

1.6.1 • Public • Published

bdb

Database for bcoin (leveldown backend).

Usage

const bdb = require('bdb');
const db = bdb.create('/path/to/my.db');

await db.open();

const myPrefix = bdb.key('r');
const myKey = bdb.key('t', ['hash160', 'uint32']);

const bucket = db.bucket(myPrefix.encode());
const batch = bucket.batch();

const hash = Buffer.alloc(20, 0x11);

// Write `foo` to `rt[1111111111111111111111111111111111111111][00000000]`.
batch.put(myKey.encode(hash, 0), Buffer.from('foo'));

await batch.write();

// Iterate:
// From: `rt[0000000000000000000000000000000000000000][00000000]`
// To: `rt[ffffffffffffffffffffffffffffffffffffffff][ffffffff]`
const iter = bucket.iterator({
  gte: myKey.min(),
  lte: myKey.max(),
  values: true
});

await iter.each((key, value) => {
  // Parse each key.
  const [hash, index] = myKey.decode(key);
  console.log('Hash: %s', hash);
  console.log('Index: %d', index);
  console.log('Value: %s', value.toString());
});

// Async Iterator is same, just used with for await syntax.:
// From: `rt[0000000000000000000000000000000000000000][00000000]`
// To: `rt[ffffffffffffffffffffffffffffffffffffffff][ffffffff]`
const asyncIter = bucket.iterator({
  gte: myKey.min(),
  lte: myKey.max(),
  values: true
});

for await (const {key, value} of asyncIter) {
  const [hash, index] = myKey.decode(key);
  console.log('Hash: %s', hash.toString('hex'));
  console.log('Index: %d', index);
  console.log('Value: %s', value.toString());
}

await db.close();

Contribution and License Agreement

If you contribute code to this project, you are implicitly allowing your code to be distributed under the MIT license. You are also implicitly verifying that all code is your original work. </legalese>

License

  • Copyright (c) 2017, Christopher Jeffrey (MIT License).

Parts of this software are based on leveldown:

  • Copyright (c) 2017, Rod Vagg (MIT License).

See LICENSE for more info.

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
1.6.114latest

Version History

VersionDownloads (Last 7 Days)Published
1.6.114
1.6.035
1.5.231
1.5.13
1.5.01
1.4.010
1.3.0164
1.2.21
1.2.11
1.1.719
1.1.61
1.1.51
1.1.42
1.1.31
1.1.21
1.1.11
1.1.01
1.0.01
0.2.321
0.2.21
0.2.11
0.2.01
0.1.01
0.0.41
0.0.31
0.0.22
0.0.11
0.0.01

Package Sidebar

Install

npm i bdb

Weekly Downloads

260

Version

1.6.1

License

MIT

Unpacked Size

864 kB

Total Files

127

Last publish

Collaborators

  • chjj
  • nodweber
  • pinheadmz