@jacobmarshall/kv
Version:
A super simple key value store backed by IndexedDB
66 lines (46 loc) • 1.21 kB
Markdown
This is a fork of [`idk-kv`](https://github.com/jakearchibald/idb-keyval).
A super simple key value store backed by IndexedDB.
Since it is backed by IndexedDB, you can store anything structured-clonable (numbers, arrays, objects, dates, blobs etc - but beware of browser support).
It's best used with a module loader (webpack, browserify, etc...) so that the global scope isn't poluted with `kv`.
Creates a custom kv store with a given name.
```js
const store = kv('sw-storage');
await store.set('hello', 'world');
console.log(await kv.get('hello'));
// -> undefined
```
```js
await kv.set('hello', 'world');
await kv.set('foo', 'bar');
```
```js
try {
await kv.set('hello', 'world')
console.log('It worked!');
} catch(err) {
console.log('It failed!', err);
}
```
```js
console.log(await kv.get('hello'));
// -> "world"
```
```js
console.log(await kv.keys());
// -> ["hello", "foo"]
```
```js
await kv.remove('hello');
```
```js
await kv.clear();
```
That's it!