Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

orbit-db-store

Package Overview
Dependencies
Maintainers
1
Versions
110
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

orbit-db-store

Base class for orbit-db data stores

  • 0.1.16
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
346
decreased by-45.34%
Maintainers
1
Weekly downloads
 
Created
Source

orbit-db-store

npm version

Base class for orbit-db data stores.

Used in

Requirements

  • Node.js >= 6.0
  • npm >= 3.0

events

Store has an events (EventEmitter) object that emits events that describe what's happening in the database.

  • data - (dbname, event)

    Emitted after an entry was added to the database

    db.events.on('data', (dbname, event) => ... )
    
  • sync - (dbname)

    Emitted before starting a database sync with a peer.

    db.events.on('sync', (dbname) => ... )
    
  • load - (dbname, hash)

    Emitted before loading the database history. hash is the hash from which the history is loaded from.

    db.events.on('load', (dbname, hash) => ... )
    
  • history - (dbname, entries)

    Emitted after loading the database history. entries is an Array of entries that were loaded.

    db.events.on('history', (dbname, entries) => ... )
    
  • ready - (dbname)

    Emitted after fully loading the database history.

    db.events.on('ready', (dbname) => ... )
    
  • write - (dbname, hash)

    Emitted after an entry was added locally to the database. hash is the IPFS hash of the latest state of the database.

    db.events.on('write', (dbname, hash) => ... )
    

API

Public methods
use()

Initialize the store by creating an operations log and loading the latest know local state of the log. Must be called before the store can be used. Emits load before init and ready after init. Returns a Promise.

sync(hash)

Merge the local store with another store. Takes an IPFS hash as an argument from which the other store is loaded from. Emits sync before loading the other store and updated after the merge IF new items were added to the store. Returns a Promise.

close()

Uninitialize the store. Emits close after the store has been uninitialized.

delete()

Remove all items from the local store. This doesn't remove or delete any entries in the distributed operations log.

Private methods
_addOperation(data)

Add an entry to the store. Takes data as a parameter which can be of any type.

this._addOperation({
  op: 'PUT',
  key: 'greeting',
  value: 'hello world!'
});

Creating Custom Data Stores

You can create a custom data stores that stores data in a way you need it to. To do this, you need to import orbit-db-store to your custom store and extend your store ckass from orbit-db-store's Store. Below is the orbit-db-kvstore which is a custom data store for orbit-db.

TODO: describe indices and how they work

const Store         = require('orbit-db-store');
const KeyValueIndex = require('./KeyValueIndex');

class KeyValueStore extends Store {
  constructor(ipfs, id, dbname, options) {
    Object.assign(options || {}, { Index: KeyValueIndex });
    super(ipfs, id, dbname, options)
  }

  get(key) {
    return this._index.get(key);
  }

  set(key, data) {
    this.put(key, data);
  }

  put(key, data) {
    return this._addOperation({
      op: 'PUT',
      key: key,
      value: data,
      meta: {
        ts: new Date().getTime()
      }
    });
  }

  del(key) {
    return this._addOperation({
      op: 'DEL',
      key: key,
      value: null,
      meta: {
        ts: new Date().getTime()
      }
    });
  }
}

module.exports = KeyValueStore;

Contributing

See orbit-db's contributing guideline.

License

MIT ©️ 2016 Haadcode

FAQs

Package last updated on 07 Feb 2017

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc