Skip to content
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ typings/

# Yarn Integrity file
.yarn-integrity
yarn.lock

# dotenv environment variables file
.env
Expand Down
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# hyperbeedeebee

A MongoDB-like database built on top of Hyperbee with support for indexing

Based on [this design](https://gist.github.com/RangerMauve/ae271204054b62d9a649d70b7d218191)
Expand Down Expand Up @@ -59,6 +60,28 @@ const killbots = await db.collection('example')
const eggbert = await db.collection('example').findOne({name: 'Eggbert'})
```

### Usage with Autobase

```JavaScript
const input = new Hypercore(RAM)
const output = new Hypercore(RAM)
const inputs = [input]

const base = new Autobase({
inputs,
localOutput: firstOutput,
localInput: firstUser
})
const autobee = new Autodeebee(base)

// Create a new DB
const db = new DB(autobee)
// You can then use the DB the same way as you did above.
```

You also need to use the [Autodeebee class](./autodeebee.js):
This class redefines the functions of Hyperbee to be compatible with the DB.

## Data Types

HyperbeeDeeBee uses MongoDB's [BSON](https://github.com/mongodb/js-bson) data types for encoding data.
Expand Down Expand Up @@ -87,6 +110,7 @@ BSONRegExp,
BSONSymbol,
Timestamp
```

## Important Differences From MongoDB

- There is a single writer for a hyperbee and multiple readers
Expand Down
86 changes: 86 additions & 0 deletions autodeebee.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
const Hyperbee = require('hyperbee')
const b4a = require('b4a')

module.exports = class Autodeebee {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Do you think you could publish this as a separate module which we could link to in the README for folks to test and include in the devDependencies for the tests?

constructor (autobase, opts) {
this.autobase = autobase
this.opts = opts
if (!opts?.sub) {
this.autobase.start({
unwrap: true,
apply: applyAutobeeBatch,
view: (core) =>
new Hyperbee(core.unwrap(), {
...this.opts,
extension: false
})
})
this.bee = this.autobase.view
}
}

ready () {
return this.autobase.ready()
}

feed () {
return this.bee.feed
}

close () {
return this.bee.close()
}

sub (name) {
const opts = this.opts ?? {}
opts.sub = true
const auto = new Autodeebee(this.autobase, opts)
auto.bee = this.bee.sub(name)
return auto
}

batch () {
return this
}

flush () { }

async put (key, value, opts = {}) {
const op = b4a.from(
JSON.stringify({ type: 'put', key, value, prefix: this.bee.prefix })
)
return await this.autobase.append(op)
}

async del (key, opts = {}) {
const op = b4a.from(
JSON.stringify({ type: 'del', key, prefix: this.bee.prefix })
)
return await this.autobase.append(op)
}

async get (key) {
return await this.bee.get(key)
}

createReadStream (opts) {
return this.bee.createReadStream(opts)
}
}

function getKeyBufferWithPrefix (key, prefix) {
return prefix ? b4a.concat([b4a.from(prefix), b4a.from(key)]) : b4a.from(key)
}
// A real apply function would need to handle conflicts, beyond last-one-wins.
async function applyAutobeeBatch (bee, batch) {
const b = bee.batch({ update: false })
for (const node of batch) {
const op = JSON.parse(node.value.toString())
const bufKey = getKeyBufferWithPrefix(op.key, op.prefix)
if (op.type === 'put') {
await b.put(bufKey, b4a.from(op.value))
}
if (op.type === 'del') await b.del(bufKey)
}
await b.flush()
}
Loading