banksJeremy / idb-keyval

A super-simple-small promise-based keyval store implemented with IndexedDB

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

IDB-Keyval

This is a super-simple-small promise-based keyval store implemented with IndexedDB, largely based on async-storage by Mozilla.

localForage offers similar functionality, but supports older browsers with broken/absent IDB implementations. Because of that, it's 6k, whereas idb-keyval is less than 500 bytes. Pick whichever works best for you!

This is only a keyval store. If you need to do more complex things like iteration & indexing, check out IDB on NPM (a little heavier at 1.7k). The first example in its README is how to recreate this library.

Usage

set:

idbKeyval.set('hello', 'world');
idbKeyval.set('foo', 'bar');

Since this is IDB-backed, you can store anything structured-clonable (numbers, arrays, objects, dates, blobs etc).

All methods return promises:

idbKeyval.set('hello', 'world')
  .then(() => console.log('It worked!'))
  .catch(err => console.log('It failed!', err));

get:

// logs: "world"
idbKeyval.get('hello').then(val => console.log(val));

keys:

// logs: ["hello", "foo"]
idbKeyval.keys().then(keys => console.log(keys));

delete:

idbKeyval.delete('hello');

clear:

idbKeyval.clear();

That's it!

About

A super-simple-small promise-based keyval store implemented with IndexedDB

License:Other


Languages

Language:JavaScript 100.0%