bkv is browser persistent key/value storage with simple interface. It supports actual IndexedDB / localStorage. With silent fallback to memory store for error-less work (optional).
Install
npm install bkv --save
const bkv = new window.BKV()
await bkv.set('dolorem', { lorem: 'ipsum' })
const data = await bkv.get('dolorem')
console.log(`Loaded: ${data}`)
await bkv.remove('dolorem'));
Note, all methods with optional callbacks will return promises if callback is not set.
Options:
prefix
- Data namespace. Default -bkv
. Used to separate data for multiple instances, if required.stores
- Array of storage names to use, ordered by preference. Default['indexeddb', 'localstorage', 'zero']
. Set to['indexeddb', 'localstorage']
if no fallback to memory required.
* zero
is simple memory store for silent fallback when no persistent storages available.
Load data by key
name. If not exists - returns default
(if passed) or undefined
.
Load all data from storage as [ { key, value }, { key, value }, ... ]
.
Put data into storage under key
name.
key
- String to address data.data
- serializable JS object to store.ttl
- Expiration time in seconds. Default = 0 (don't expire).
Remove key
data from store. If Array
passed - remove all listed keys.
Clear all storage data (in your namespace), or just expired objects when called
with true
param.
Auto-executed on first call of any method. Usually not needed. But may be used, if you need exact info about availability of persistent storage, prior to start.
Alias of new BKV(options)
Similar to .create()
but returns the same (cached) instance for the same
options. Useful if you need BKV in multiple modules, and don't wish to
initialize it every time.