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.
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));
// logs: "world"
idbKeyval.get('hello').then(val => console.log(val));
If there is no 'hello' key, then val
will be undefined
.
// logs: ["hello", "foo"]
idbKeyval.keys().then(keys => console.log(keys));
idbKeyval.delete('hello');
idbKeyval.clear();
That's it!