npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@dashevo/dapi-db-store

v2.5.3

Published

Base class for dapi-db data stores

Downloads

9

Readme

dapi-db-store

npm version

Base class for dapi-db data stores. You generally don't need to use this module if you want to use dapi-db. This module contains shared methods between all data stores in dapi-db and can be used as a base class for a new data model.

Used in

Requirements

  • Node.js >= 8.0.0

API

Public methods

load(amount)

Load the database using locally persisted state. Can specify how many entries to load with amount argument.

saveSnapshot()

Save the current state of the database locally. Returns a Promise that resolves to a IPFS Multihash as a Base58 encoded string. The the database can be loaded using this hash.

loadFromSnapshot(hash, onProgressCallback)

Load the state of the database from a snapshot. hash is the IPFS Multihash of the snapshot data. Returns a Promise that resolves when the database has been loaded.

close()

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

drop()

Remove the database locally. This doesn't remove or delete the database from peers who have replicated the database.

sync(heads)

Sync this database with entries from heads where heads is an array of ipfs-log Entries. Usually, you don't need to call this method manually as dapiDB takes care of this for you.

Properties

address

Get the address of this database. Returns an object { root: <manifestHash>, path: <path> }. Convert to a string with db.address.toString().

console.log(db.address)
// /dapidb/Qmd8TmZrWASypEp4Er9tgWP4kCNQnW4ncSnvjvyHQ3EVSU/databaseName
key

Key pair used with this store to sign and access entries. This key is the peer/node/user key.

console.log(db.key.toPublic('hex'))
// 042c07044e7ea51a489c02854db5e09f0191690dc59db0afd95328c9db614a2976e088cab7c86d7e48183191258fc59dc699653508ce25bf0369d67f33d5d77839
type

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

console.log(db.type) // "eventlog"
replicationStatus

Get database replication status information such as total number of entries and loading progress.

console.log(db.replicationStatus)
// { buffered: 0, queued: 0, progress: 2, max: 5 }

Events

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

  • load - (dbname, hash)

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

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

    Emitted after fully loading the database history.

    db.events.on('ready', (id) => ... )
    db.load()
  • load.progress - (id, hash, entry, progress, total)

    Emitted for each entry during load.

    Progress is the current load count. Total is the maximum load count (ie. length of the full database). These are useful eg. for displaying a load progress percentage.

    db.events.on('load', (id, hash, entry, progress, total) => ... )
    db.load()
  • replicated - (dbname)

    Emitted after the database was synced with an update from a peer database.

    db.events.on('replicated', (id) => ... )
  • write - (id, hash, entry)

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

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

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 dapi-db-store to your custom store and extend your store ckass from dapi-db-store's Store. Below is the dapi-db-kvstore which is a custom data store for dapi-db.

TODO: describe indices and how they work

const Store         = require('dapi-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 dapi-db's contributing guideline.

License

MIT ©️ 2016-2018 Protocol Labs Inc., Haja Networks Oy, 2018 Dash Core Group, Inc.