{"name":"hyperbee","version":"1.2.0","author":{"url":"@mafintosh","name":"Mathias Buus"},"license":"MIT","_id":"hyperbee@1.2.0","homepage":"https://github.com/mafintosh/hyperbee","bugs":{"url":"https://github.com/mafintosh/hyperbee/issues"},"dist":{"shasum":"4471de8004606d56d0bb23f1dcc15a9e2ad23260","tarball":"http://registry-npm.ywwl.com/hyperbee/download/hyperbee-1.2.0.tgz","fileCount":23,"integrity":"sha512-wdOhyt1PXQ79Bwz8wL6aqtBol5++NSqSYB17jwN5iwMepF6zo3It0eppn9c5DsanzyEvZ5Axcn8PD23ZtdeUjA==","signatures":[{"sig":"MEYCIQCILm+lZQoHvNDylPatErPS75+QUd6KpKmy3UPLFy9o2AIhAI1GahNb4smoAoClKx7A8LrM3jO9voSp3k5fqpGuWSje","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":93036,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJf+caqCRA9TVsSAnZWagAApnsP/3wIshCy02WZrmD7AERd\nLTb9U9vmQLa5w4qlv6fLyuPWORMG3VEZyXQVMWH/Vvn332Pbijb5NK0v8+VE\nexg88xBPn6X0LZNOsj+bOQVuPiXd39XNnHRCogYRL2Q/JK+IgkrpH9LvYCju\ngkmH/CyGAOhybJUP/oZrx75fjWy2Emu8zUepqM0uDv74agx2c5UiHgX8fIUL\n5gE+OaIhDBdOk5SfvAl4AH+vu9GqIVqqcby3uxy5TcKXZYTYn9ptbPDtPffb\nxIFVkuUeysL5k3X/100dYK9F3g/aIlZSUHe0J1F+evsF5YYTssaIgYEtH77y\nhXl2Y4MUkVL7m458u3qmxY9c8P4gmP6xpsrTmuMKztLiaeI12ZytY/+Z596D\nch5OB4LenGZqcECyOeg3T+GpGF1269oFWbZfmsJi3aQ2e5eBvSuNc47Tpz8J\nCbzr5nnjMMIPGH22RR4SZbvfN+YL2mXIHD7qAYApkoep2Yi0+34TNgurObcl\nsCkEKxiRLrJFBoo1rS0FvUangodFu2Vho2rvAhMV39i4q1ejquqlQkNIk1QU\n7Q7Sih1CXMzvIEWccBfdfSUIA8jrx/AXYP7Vxb1qeqGbJf05NXiAzbiY+Ys9\nCbu+8m4VRP8GYmzdRZnfV8wWLD7Ie7DWZQhzw8JwDWu1He+1154gc2yl9Qi0\n/28k\r\n=9Cvn\r\n-----END PGP SIGNATURE-----\r\n","size":20019,"noattachment":false,"key":"/hyperbee/-/hyperbee-1.2.0.tgz"},"main":"index.js","gitHead":"08e02e737c42db9582ed4ce647d1732888620cc3","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.8","description":"An append-only Btree running on a Hypercore.","directories":{},"_nodeVersion":"14.15.1","dependencies":{"codecs":"^2.1.0","streamx":"^2.6.6","mutexify":"^1.3.1","hypercore-promisifier":"^1.0.1","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_1.2.0_1610204842015_0.2833944352739737","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":1610204842162,"maintainers":[{"name":"mafintosh","email":"mathiasbuus@gmail.com"},{"name":"andrewosh","email":"andrewosh@gmail.com"}],"_cnpm_publish_time":1610204842162,"dist-tags":{"latest":"2.27.3"}}