{"name":"hyperbee","version":"0.0.3","author":{"url":"@mafintosh","name":"Mathias Buus"},"license":"MIT","_id":"hyperbee@0.0.3","homepage":"https://github.com/mafintosh/hyperbee","bugs":{"url":"https://github.com/mafintosh/hyperbee/issues"},"dist":{"shasum":"e064e15e70a87bfe19e618435c0e50b4679693e0","tarball":"http://registry-npm.ywwl.com/hyperbee/download/hyperbee-0.0.3.tgz","fileCount":16,"integrity":"sha512-3RlqZOU782WOh+wANlUMIcXzxRSKti3FyCs/hXfMupjMyLiBKYaiPKtEMM9JXMtJSb7iQG8VgfkvCE/y0CAbxA==","signatures":[{"sig":"MEUCIQCMBz1xcL8NXgOcpHDsu8hOIuGN1+amrzHwcAEUqSzhVwIgWLjVLaUoJLC/1Z/Qtvy4eaP+Wh9ZDQ4XO5z6MWI6Xg0=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":54528,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfM70OCRA9TVsSAnZWagAAdcsP/3LQcJAh2Cjeo/2TSIUD\n0zwow0YGJOUcyeon4O34PIQpzGiU2kDQWs6UXyg3bVJ1M9u7Ve2TuRt8JUSD\nzszF83H3RVnGvV7FFQ/lyk0K3E5Dkj2Kd9VwsKCPwsX0qi+VGZgZstb6/xA7\n2iHlx6INk/D/TWbpigwWW6RSwgrdv3aQSCC0vPCNzZ88Jv90+TbOFZp8XpJo\nqWpRI4/EhoEU9k7942X2vY5Y7LFl4WMjXxyU+i3kre2UZUfOPqinDMuBRAIY\nWxGDw1ZdTwZQrD0wls0oh/6MAtyiFV8jqZy1MGJhiSQGOfkfbKX3IPI81pKN\n41kdVodWyHun+RtMuwEMSrHjlm8utW2lWLoS/BkfpwqjnBjSdtLVBiSEmyg3\nDaukXCCqOLKBpNEiath01/pvc2jvScmVed33S9w8w+HUvIT272HbmWtSvqH5\nD/+QrERcpZ8ikaahHbYUF6mG7/4/pu0TB6Id4Kr16BVDQztr1mSsH+F6zng6\nJEaGBtdXBMQyowI2h7d+U6u/KDgop3wUf4oatvcUg9XDzKlEjXLeJiCCSGT6\nX9Yats+h68VOehxcE0TJ93iSO0wHLjo+s3mRmniAEs9Z84pcdi2x7mYx7aSx\n6jJ73L8uiXGVIkFEBivmlPJUqDi/AD+Pn7813/LmgRDJLnF0JF5m73oc5+QL\ndhrG\r\n=FFwq\r\n-----END PGP SIGNATURE-----\r\n","size":12270,"noattachment":false,"key":"/hyperbee/-/hyperbee-0.0.3.tgz"},"main":"index.js","gitHead":"68b5b0cf0c4102326c872e4d8bf2a68253ea7bc9","scripts":{"test":"standard && tape test/*.js","protobuf":"protocol-buffers schema.proto -o ./lib/messages.js"},"_npmUser":{"name":"mafintosh","email":"mathiasbuus@gmail.com"},"repository":{"url":"git+https://github.com/mafintosh/hyperbee.git","type":"git"},"_npmVersion":"6.14.5","description":"An append-only Btree running on a Hypercore.","directories":{},"_nodeVersion":"14.4.0","dependencies":{"codecs":"^2.1.0","streamx":"^2.6.6","protocol-buffers-encodings":"^1.1.0"},"_hasShrinkwrap":false,"devDependencies":{"tape":"^5.0.1","standard":"^14.3.4","hypercore":"^9.5.0","tree-to-string":"^1.1.1","protocol-buffers":"^4.2.0","random-access-memory":"^3.1.1"},"_npmOperationalInternal":{"tmp":"tmp/hyperbee_0.0.3_1597226253668_0.6280249545423036","host":"s3://npm-registry-packages"},"readme":"# Hyperbee 🐝\n\n[See API docs at docs.holepunch.to](https://docs.holepunch.to/building-blocks/hyperbee)\n\nAn append-only B-tree running on a Hypercore. Allows sorted iteration and more.\n\n```\nnpm install hyperbee\n```\n\n## Usage\n\n```js\nconst Hyperbee = require('hyperbee')\nconst Hypercore = require('hypercore')\nconst RAM = require('random-access-memory')\n\nconst core = new Hypercore(RAM)\nconst db = new Hyperbee(core, { keyEncoding: 'utf-8', valueEncoding: 'binary' })\n\n// If you own the core\nawait db.put('key1', 'value1')\nawait db.put('key2', 'value2')\nawait db.del('some-key')\n\n// If you want to insert/delete batched values\nconst batch = db.batch()\n\nawait batch.put('key', 'value')\nawait batch.del('some-key')\nawait batch.flush() // Execute the batch\n\n// Query the core\nconst entry = await db.get('key') // => null or { key, value }\n\n// Read all entries\nfor await (const entry of db.createReadStream()) {\n  // ..\n}\n\n// Read a range\nfor await (const entry of db.createReadStream({ gte: 'a', lt: 'd' })) {\n  // Anything >=a and <d\n}\n\n// Get the last written entry\nfor await (const entry of db.createHistoryStream({ reverse: true, limit: 1 })) {\n  // ..\n}\n```\n\nIt works with sparse cores, only a small subset of the full core is downloaded to satisfy your queries.\n\n## API\n\n#### `const db = new Hyperbee(core, [options])`\n\nMake a new Hyperbee instance. `core` should be a [Hypercore](https://github.com/holepunchto/hypercore).\n\n`options` include:\n```js\n{\n  keyEncoding: 'binary', // \"binary\" (default), \"utf-8\", \"ascii\", \"json\", or an abstract-encoding\n  valueEncoding: 'binary' // Same options as keyEncoding like \"json\", etc\n}\n```\n\nNote that currently read/diff streams sort based on the encoded value of the keys.\n\n#### `await db.ready()`\n\nWaits until internal state is loaded.\n\nUse it once before reading synchronous properties like `db.version`, unless you called any of the other APIs.\n\n#### `await db.close()`\n\nFully close this bee, including its core.\n\n#### `db.core`\n\nThe underlying Hypercore backing this bee.\n\n#### `db.version`\n\nNumber that indicates how many modifications were made, useful as a version identifier.\n\n#### `db.id`\n\nString containing the id (z-base-32 of the public key) identifying this bee.\n\n#### `db.key`\n\nBuffer containing the public key identifying this bee.\n\n#### `db.discoveryKey`\n\nBuffer containing a key derived from `db.key`.\n\nThis discovery key does not allow you to verify the data, it's only to announce or look for peers that are sharing the same bee, without leaking the bee key.\n\n#### `db.writable`\n\nBoolean indicating if we can put or delete data in this bee.\n\n#### `db.readable`\n\nBoolean indicating if we can read from this bee. After closing the bee this will be `false`.\n\n#### `await db.put(key, [value], [options])`\n\nInsert a new key. Value can be optional.\n\nIf you're inserting a series of data atomically or want more performance then check the `db.batch` API.\n\n`options` includes:\n```js\n{\n  cas (prev, next) { return true }\n}\n```\n\n##### Compare And Swap (cas)\n`cas` option is a function comparator to control whether the `put` succeeds.\n\nBy returning `true` it will insert the value, otherwise it won't.\n\nIt receives two args: `prev` is the current node entry, and `next` is the potential new node.\n\n```js\nawait db.put('number', '123', { cas })\nconsole.log(await db.get('number')) // => { seq: 1, key: 'number', value: '123' }\n\nawait db.put('number', '123', { cas })\nconsole.log(await db.get('number')) // => { seq: 1, key: 'number', value: '123' }\n// Without cas this would have been { seq: 2, ... }, and the next { seq: 3 }\n\nawait db.put('number', '456', { cas })\nconsole.log(await db.get('number')) // => { seq: 2, key: 'number', value: '456' }\n\nfunction cas (prev, next) {\n  // You can use same-data or same-object lib, depending on the value complexity\n  return prev.value !== next.value\n}\n```\n\n#### `const { seq, key, value } = await db.get(key)`\n\nGet a key's value. Returns `null` if key doesn't exists.\n\n`seq` is the Hypercore index at which this key was inserted.\n\n#### `await db.del(key, [options])`\n\nDelete a key.\n\n`options` include:\n```js\n{\n  cas (prev, next) { return true }\n}\n```\n\n##### Compare And Swap (cas)\n`cas` option is a function comparator to control whether the `del` succeeds.\n\nBy returning `true` it will delete the value, otherwise it won't.\n\nIt only receives one arg: `prev` which is the current node entry.\n\n```js\n// This won't get deleted\nawait db.del('number', { cas })\nconsole.log(await db.get('number')) // => { seq: 1, key: 'number', value: 'value' }\n\n// Change the value so the next time we try to delete it then \"cas\" will return true\nawait db.put('number', 'can-be-deleted')\n\nawait db.del('number', { cas })\nconsole.log(await db.get('number')) // => null\n\nfunction cas (prev) {\n  return prev.value === 'can-be-deleted'\n}\n```\n\n#### `const { key, value } = await db.getBySeq(seq, [options])`\n\nGet the key and value from a block number.\n\n`seq` is the Hypercore index. Returns `null` if block doesn't exists.\n\n#### `const stream = db.replicate(isInitiatorOrStream)`\n\nSee more about how replicate works at [core.replicate][core-replicate-docs].\n\n#### `const batch = db.batch()`\n\nMake a new atomic batch that is either fully processed or not processed at all.\n\nIf you have several inserts and deletions then a batch can be much faster.\n\n#### `await batch.put(key, [value], [options])`\n\nInsert a key into a batch.\n\n`options` are the same as `db.put` method.\n\n#### `const { seq, key, value } = await batch.get(key)`\n\nGet a key, value out of a batch.\n\n#### `await batch.del(key, [options])`\n\nDelete a key into the batch.\n\n`options` are the same as `db.del` method.\n\n#### `await batch.flush()`\n\nCommit the batch to the database, and releases any locks it has acquired.\n\n#### `await batch.close()`\n\nDestroy a batch, and releases any locks it has acquired on the db.\n\nCall this if you want to abort a batch without flushing it.\n\n#### `const stream = db.createReadStream([range], [options])`\n\nMake a read stream. Sort order is based on the binary value of the keys.\n\nAll entries in the stream are similar to the ones returned from `db.get`.\n\n`range` should specify the range you want to read and looks like this:\n\n```js\n{\n  gt: 'only return keys > than this',\n  gte: 'only return keys >= than this',\n  lt: 'only return keys < than this',\n  lte: 'only return keys <= than this'\n}\n```\n\n`options` include:\n\n```js\n{\n  reverse: false // Set to true to get them in reverse order,\n  limit: -1 // Set to the max number of entries you want\n}\n```\n\n#### `const { seq, key, value } = await db.peek([range], [options])`\n\nSimilar to doing a read stream and returning the first value, but a bit faster than that.\n\n#### `const stream = db.createHistoryStream([options])`\n\nCreate a stream of all entries ever inserted or deleted from the db.\n\nEach entry has an additional `type` property indicating if it was a `put` or `del` operation.\n\n`options` include:\n```js\n{\n  live: false, // If true the stream will wait for new data and never end\n  reverse: false, // If true get from the newest to the oldest\n  gte: seq, // Start with this seq (inclusive)\n  gt: seq, // Start after this index\n  lte: seq, // Stop after this index\n  lt: seq, // Stop before this index\n  limit: -1 // Set to the max number of entries you want\n}\n```\n\nIf any of the `gte`, `gt`, `lte`, `lt` arguments are `< 0` then\nthey'll implicitly be added with the version before starting so\ndoing `{ gte: -1 }` makes a stream starting at the last index.\n\n#### `const stream = db.createDiffStream(otherVersion, [options])`\n\nEfficiently create a stream of the shallow changes between two versions of the db.\n\n`options` are the same as `db.createReadStream`, except for `reverse`.\n\nEach entry is sorted by key and looks like this:\n```js\n{\n  left: Object, // The entry in the `db`\n  right: Object // The entry in `otherVersion`\n}\n```\n\nIf an entry exists in db but not in the other version, then `left` is set\nand `right` will be null, and vice versa.\n\nIf the entries are causally equal (i.e. the have the same seq), they are not\nreturned, only the diff.\n\n#### `const entryWatcher = await db.getAndWatch(key, [options])`\n\nReturns a watcher which listens to changes on the given key.\n\n`entryWatcher.node` contains the current entry in the same format as the result of `bee.get(key)`,  and will be updated as it changes.\n\nBy default, the node will have the bee's key- and value encoding, but you can overwrite it by setting the `keyEncoding` and `valueEncoding` options.\n\nYou can listen to `entryWatcher.on('update')` to be notified when the value of node has changed.\n\nCall `await watcher.close()` to stop the watcher.\n\n#### `const watcher = db.watch([range])`\n\nListens to changes that are on the optional `range`.\n\n`range` options are the same as `db.createReadStream` except for `reverse`.\n\nBy default, the yielded snapshots will have the bee's key- and value encoding, but you can overwrite them by setting the `keyEncoding` and `valueEncoding` options.\n\nUsage example:\n```js\nfor await (const [current, previous] of watcher) {\n  console.log(current.version)\n  console.log(previous.version)\n}\n```\n\nReturns a new value after a change, `current` and `previous` are snapshots that are auto-closed before next value.\n\nDon't close those snapshots yourself because they're used internally, let them be auto-closed.\n\nWatchers on subs and checkouts are not supported. Instead, use the range option to limit scope.\n\n`await watcher.ready()`\n\nWaits until the watcher is loaded and detecting changes.\n\n`await watcher.close()`\n\nStops the watcher. You could also stop it by using `break` in the loop.\n\n#### `const snapshot = db.checkout(version)`\n\nGet a read-only snapshot of a previous version.\n\n#### `const snapshot = db.snapshot()`\n\nShorthand for getting a checkout for the current version.\n\n#### `const sub = db.sub('sub-prefix', options = {})`\n\nCreate a sub-database where all entries will be prefixed by a given value.\n\nThis makes it easy to create namespaces within a single Hyperbee.\n\n`options` include:\n```js\n{\n  sep: Buffer.alloc(1), // A namespace separator\n  valueEncoding, // Optional sub valueEncoding (defaults to the parents)\n  keyEncoding // Optional sub keyEncoding (defaults to the parents)\n}\n```\n\nFor example:\n```js\nconst root = new Hyperbee(core)\nconst sub = root.sub('a')\n\n// In root, this will have the key ('a' + separator + 'b')\nawait sub.put('b', 'hello')\n\n// Returns => { key: 'b', value: 'hello')\nawait sub.get('b')\n```\n\n#### `const header = await db.getHeader([options])`\n\nReturns the header contained in the first block. Throws if undecodable.\n\n`options` are the same as the `core.get` method.\n\n#### `const isHyperbee = await Hyperbee.isHyperbee(core, [options])`\n\nReturns `true` if the core contains a Hyperbee, `false` otherwise.\n\nThis requests the first block on the core, so it can throw depending on the options.\n\n`options` are the same as the `core.get` method.\n\n[core-replicate-docs]: https://github.com/holepunchto/hypercore#const-stream--corereplicateisinitiatororreplicationstream\n","publish_time":1597226253879,"maintainers":[{"name":"mafintosh","email":"mathiasbuus@gmail.com"},{"name":"andrewosh","email":"andrewosh@gmail.com"}],"_cnpm_publish_time":1597226253879,"dist-tags":{"latest":"2.27.3"}}