What is idb-keyval?
The idb-keyval npm package is a simple key-value store backed by IndexedDB. It provides a straightforward API for storing, retrieving, and managing data in the browser's IndexedDB, making it easier to work with compared to the native IndexedDB API.
What are idb-keyval's main functionalities?
Set a value
This feature allows you to store a value in the IndexedDB with a specified key. The code sample demonstrates how to set a value 'value' with the key 'key'.
import { set } from 'idb-keyval';
set('key', 'value').then(() => console.log('Value set!'));
Get a value
This feature allows you to retrieve a value from the IndexedDB using a specified key. The code sample demonstrates how to get the value associated with the key 'key'.
import { get } from 'idb-keyval';
get('key').then(val => console.log('Value:', val));
Delete a value
This feature allows you to delete a value from the IndexedDB using a specified key. The code sample demonstrates how to delete the value associated with the key 'key'.
import { del } from 'idb-keyval';
del('key').then(() => console.log('Value deleted!'));
Clear all values
This feature allows you to clear all values from the IndexedDB. The code sample demonstrates how to clear all stored values.
import { clear } from 'idb-keyval';
clear().then(() => console.log('All values cleared!'));
Get all keys
This feature allows you to retrieve all keys from the IndexedDB. The code sample demonstrates how to get all keys stored in the database.
import { keys } from 'idb-keyval';
keys().then(keys => console.log('Keys:', keys));
Other packages similar to idb-keyval
localforage
LocalForage is a fast and simple storage library for JavaScript. It improves the offline experience of your web app by using asynchronous storage (IndexedDB or WebSQL) with a simple, localStorage-like API. Compared to idb-keyval, LocalForage offers more flexibility in terms of storage backends and a more extensive API.
dexie
Dexie is a minimalistic wrapper for IndexedDB that provides a more powerful and developer-friendly API. It supports advanced querying, transactions, and versioning. Compared to idb-keyval, Dexie is more feature-rich and suitable for complex use cases involving IndexedDB.
idb
The idb library is a small, well-tested library that makes working with IndexedDB more pleasant. It provides a promise-based API and simplifies many common tasks. Compared to idb-keyval, idb offers a more comprehensive API for interacting with IndexedDB, while idb-keyval focuses on simplicity and ease of use.
IDB-Keyval
This is a super-simple-small promise-based keyval store implemented with IndexedDB, largely based on async-storage by Mozilla.
If you're looking for a more general-purpose promise-based IDB, try IDB on NPM. If you want to support older browsers with broken/absent IDB implementations, try localForage.
Usage
Setting:
idbKeyval.set('hello', 'world');
Since this is IDB-backed, you can also store numbers, arrays, objects and blobs. All methods return promises:
idbKeyval.set('hello', 'world')
.then(() => console.log('It worked!'))
.catch(err => console.log('It failed!', err));
Getting:
idbKeyval.get('hello').then(val => console.log(val));
Removing:
idbKeyval.remove('hello');
Clearing:
idbKeyval.clear();
That's it!