Socket
Socket
Sign inDemoInstall

bee-id

Package Overview
Dependencies
3
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    bee-id

Incremental ID for Hyperbee


Version published
Maintainers
1
Install size
61.1 kB
Created

Readme

Source

bee-id

Incremental ID for Hyperbee

npm i bee-id

Usage

const { idEncoding, nextId, putWithId } = require('bee-id')
const beeTransaction = require('bee-transaction')

const db = new Hyperbee(...)

// Single put
const id = await putWithId(db, '/users', { email, password })

// Single callback put
const id2 = await putWithId(db, '/users', async function (b, userId) {
  await b.put('/users/' + userId, { email, password }, { keyEncoding: idEncoding })
})

// Multiple puts
const ids = await beeTransaction(db, async function (b) {
  const userId = await nextId(b, '/users')
  const profileId = await nextId(b, '/profiles')

  await b.put('/users/' + userId, { email, password }, { keyEncoding: idEncoding })
  await b.put('/profiles/' + profileId, { name, age }, { keyEncoding: idEncoding })

  return { userId, profileId }
})

API

idEncoding

Helper for encoding and decoding lexicographical id keys for ordering.

const id = await putWithId(db, sub, dataOrCallback)

Insert a new entry with an incremental ID.

Single put with value:

const { putWithId, idEncoding } = require('bee-id')

const id = await putWithId(db, '/users', { username: 'abc' })

console.log(await db.get('/users/1', { keyEncoding: idEncoding }))

Single put with callback:

const { putWithId, idEncoding } = require('bee-id')

const id = await putWithId(db, '/users', async function (b, userId) {
  if (await b.get('/index/email/' + email)) throw new Error('Already exists')

  await b.put('/users/' + userId, { email, password }, { keyEncoding: idEncoding })
  await b.put('/index/email/' + email, userId)
})

console.log(await db.get('/users/2', { keyEncoding: idEncoding }))

Note: Returning in any callback is optional.

const id = await nextId(batch, sub)

Finds the last entry to calculate and return the incremented ID.

Multiple puts:

const { nextId, idEncoding } = require('bee-id')
const beeTransaction = require('bee-transaction')

const ids = await beeTransaction(db, async function (b) {
  if (await b.get('/index/email/' + email)) throw new Error('Already exists')

  const userId = await nextId(b, '/users')
  const profileId = await nextId(b, '/profiles')

  await b.put('/users/' + userId, { email, password }, { keyEncoding: idEncoding })
  await b.put('/profiles/' + profileId, { name, age }, { keyEncoding: idEncoding })
  await b.put('/index/email/' + email, userId)

  return { userId, profileId }
})

console.log(await db.get('/users/3', { keyEncoding: idEncoding }))
console.log(await db.get('/profiles/1', { keyEncoding: idEncoding }))

Notes

Don't reuse the sub like /users/... for other purposes because it will mess up the internals.

License

MIT

FAQs

Last updated on 18 Jan 2024

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc