原文链接: IndexedDB 包装库 idb
上一篇: win10 更新到2004 安装常用环境
下一篇: vue3 watch 数组
功能比较全的
https://github.com/jakearchibald/idb
只作为k-v存储简单使用的
https://github.com/jakearchibald/idb-keyval
使用很简单
Usage
set:
import { set } from 'idb-keyval'; set('hello', 'world'); set('foo', 'bar');
Since this is IDB-backed, you can store anything structured-clonable (numbers, arrays, objects, dates, blobs etc).
All methods return promises:
import { set } from 'idb-keyval'; set('hello', 'world') .then(() => console.log('It worked!')) .catch(err => console.log('It failed!', err));
get:
import { get } from 'idb-keyval'; // logs: "world" get('hello').then(val => console.log(val));
If there is no 'hello' key, then val
will be undefined
.
keys:
import { keys } from 'idb-keyval'; // logs: ["hello", "foo"] keys().then(keys => console.log(keys));
del:
import { del } from 'idb-keyval'; del('hello');
clear:
import { clear } from 'idb-keyval'; clear();
Custom stores:
By default, the methods above use an IndexedDB database named keyval-store
and an object store named keyval
. You can create your own store, and pass it as an additional parameter to any of the above methods:
import { Store, set } from 'idb-keyval'; const customStore = new Store('custom-db-name', 'custom-store-name'); set('foo', 'bar', customStore);
That's it!