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

nanoidb

v1.3.1

Published

fun wrapper around indexeddb

Downloads

15

Readme

nanoidb

npm version build status downloads js-standard-style

IndexedDB is a web-api for client-side storage, and although widely used, the API itself is at times confusing. Nanoidb is small wrapper to help standardize most useful methods in a callback based fashion.

IndexedDB is an async transactional database that lets you store objects as <key, value> pairs. The pairs get stored in object stores that are essentially a set of database tables, but in an indexedDB context.

Usage

var db = Nanoidb('catStore', 1)
db.on('upgrade', function (diffData) {
  diffData.db.createObjectStore('catStore')
})

db.on('open', function (stores) {
  putOp(stores.catStore)

  function putOp (store) {
    stores.catStore.put('key-12345', 'ChashuCat', function (err) {
      if (err) throw err
      console.log('put done')
      batchOp(stores.catStore)
      getOp(stores.catStore)
      getAllOp(stores.catStore)
    })
  }

  function getAllOp (store) {
    store.getAll('dang', 10, function (err, values) {
      if (err) throw err
      values.forEach(function (value) {
        console.log('new value', value)
      }) 
    })
  }

  function getOp (store) {
    store.get('key-12345', function (err, val) {
      if (err) throw err
      console.log('get value', val)
      deleteOp(store)
    })
  }

  function deleteOp (store) {
    store.del('catStore', function (err) {
      if (err) throw err
      console.log('deleted')
      batchOp(store)
    })
  }

  function batchOp (store) {
    store.batch()
      .put('coolThang', 'hell yea')
      .put('dang', 'no wayyy')
      .put('ding', 'whoaaa yea')
      .del('ding')
      .flush(function (err) {
        if (err) throw err
        console.log('it flushed successfully')
      })
  }
})

API

db = Nanoidb(name, version)

This creates an instance of IndexedDB. It takes in a database name and version. IndexedDB's versioning starts with 1, rather than 0.

db.upgrade(version)

Upgrades the current version of Nanoidb with the specified version. This is useful for when you need to create an extra object store under the same Nanoidb instance.

db.on('upgrade', callback(diffData))

Returns an object composed of a previously created indexedDB and a IDBVersionChangeEvent. This is where you should create your object store by calling diffData.db.createObjectStore('<name>').

diffData.event provides you with an oldVersion property to help with schema updates.

db.on('open', callback(stores))

Returns an instance of an object store that you can later use.

db.on('error', callback(error))

Returns an IndexedDB error object. This event will be sent when IndexedDB runs into an error creating a database.

store = stores.<name>

Instance of an Object Store.

store.put(key, val, callback(err))

Given the Object Store you previously created, you can add a value to the database via the put method. Takes an object key, val, and an error callback.

store.get(key, callback(err, val))

You can also get a value from the database with a get. Takes a key and an error callback.

store.getAll([query, count], callback(err, val))

Get all records in a given store. Can optionally take a query key or range, as well as a count for values to be returned if there are duplicates. Will return all records if neither query nor count are provided.

store.del(key, callback(err))

Delete method takes a key and an error callback.

batch = store.batch()

You can also batch chain del and get methods. When you're done, you have to call a .flush() to handle your callback.

batch.put(key, val)

Add an object within a batch operation. Takes a key and a val.

batch.del(key)

Delete an object within a batch operation. Takes a key.

batch.flush(callback(err))

When working with a batch() method, you have to call a flush to handle your errors. This just takes an error callback.

Install

npm install nanoidb

Related content

License

MIT