Gathering detailed insights and metrics for idb-keyval
Gathering detailed insights and metrics for idb-keyval
Gathering detailed insights and metrics for idb-keyval
Gathering detailed insights and metrics for idb-keyval
@isomorphic-git/idb-keyval
A super-simple-small keyval store built on top of IndexedDB
@se-ng/swapi
This is a helper project to be able to use the [Star Wars API](https://swapi.co/) in my Angular samples. It fetches the entirety of SWAPI and caches this into indexedDB by utilizing [idb-keyval](https://github.com/jakearchibald/idb-keyval) from Jake Archi
gatsby-plugin-gdpr
Gatsby plugin to add google tag manager GDPR form to your site using idb-keyval.
localstorage-idb-keyval
A super-simple-small keyval store built on top of IndexedDB with localStorage API
A super-simple-small promise-based keyval store implemented with IndexedDB
npm install idb-keyval
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
2,780 Stars
119 Commits
146 Forks
24 Watching
4 Branches
22 Contributors
Updated on 27 Nov 2024
TypeScript (67.65%)
JavaScript (31.88%)
HTML (0.46%)
Cumulative downloads
Total Downloads
Last day
4%
201,018
Compared to previous day
Last week
6.5%
1,051,166
Compared to previous week
Last month
9.9%
4,337,823
Compared to previous month
Last year
110.4%
41,508,271
Compared to previous year
23
This is a super-simple promise-based keyval store implemented with IndexedDB, originally based on async-storage by Mozilla.
It's small and tree-shakeable. If you only use get/set, the library is ~250 bytes (brotli'd), if you use all methods it's ~534 bytes.
localForage offers similar functionality, but supports older browsers with broken/absent IDB implementations. Because of that, it's orders of magnitude bigger (~7k).
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 1k). The first example in its README is how to create a keyval store.
1npm install idb-keyval
Now you can require/import idb-keyval
:
1import { get, set } from 'idb-keyval';
If you're targeting IE10/11, use the compat version, and import a Promise
polyfill.
1// Import a Promise polyfill 2import 'es6-promise/auto'; 3import { get, set } from 'idb-keyval/dist/esm-compat';
A well-behaved bundler should automatically pick the ES module or the CJS module depending on what it supports, but if you need to force it either way:
idb-keyval/dist/index.js
EcmaScript module.idb-keyval/dist/index.cjs
CommonJS module.Legacy builds:
idb-keyval/dist/compat.js
EcmaScript module, transpiled for older browsers.idb-keyval/dist/compat.cjs
CommonJS module, transpiled for older browsers.idb-keyval/dist/umd.js
UMD module, also transpiled for older browsers.These built versions are also available on jsDelivr, e.g.:
1<script src="https://cdn.jsdelivr.net/npm/idb-keyval@6/dist/umd.js"></script> 2<!-- Or in modern browsers: --> 3<script type="module"> 4 import { get, set } from 'https://cdn.jsdelivr.net/npm/idb-keyval@6/+esm'; 5</script>
1import { set } from 'idb-keyval'; 2 3set('hello', 'world');
Since this is IDB-backed, you can store anything structured-clonable (numbers, arrays, objects, dates, blobs etc), although old Edge doesn't support null
. Keys can be numbers, strings, Date
s, (IDB also allows arrays of those values, but IE doesn't support it).
All methods return promises:
1import { set } from 'idb-keyval'; 2 3set('hello', 'world') 4 .then(() => console.log('It worked!')) 5 .catch((err) => console.log('It failed!', err));
1import { get } from 'idb-keyval'; 2 3// logs: "world" 4get('hello').then((val) => console.log(val));
If there is no 'hello' key, then val
will be undefined
.
Set many keyval pairs at once. This is faster than calling set
multiple times.
1import { set, setMany } from 'idb-keyval'; 2 3// Instead of: 4Promise.all([set(123, 456), set('hello', 'world')]) 5 .then(() => console.log('It worked!')) 6 .catch((err) => console.log('It failed!', err)); 7 8// It's faster to do: 9setMany([ 10 [123, 456], 11 ['hello', 'world'], 12]) 13 .then(() => console.log('It worked!')) 14 .catch((err) => console.log('It failed!', err));
This operation is also atomic – if one of the pairs can't be added, none will be added.
Get many keys at once. This is faster than calling get
multiple times. Resolves with an array of values.
1import { get, getMany } from 'idb-keyval'; 2 3// Instead of: 4Promise.all([get(123), get('hello')]).then(([firstVal, secondVal]) => 5 console.log(firstVal, secondVal), 6); 7 8// It's faster to do: 9getMany([123, 'hello']).then(([firstVal, secondVal]) => 10 console.log(firstVal, secondVal), 11);
Transforming a value (eg incrementing a number) using get
and set
is risky, as both get
and set
are async and non-atomic:
1// Don't do this: 2import { get, set } from 'idb-keyval'; 3 4get('counter').then((val) => 5 set('counter', (val || 0) + 1); 6); 7 8get('counter').then((val) => 9 set('counter', (val || 0) + 1); 10);
With the above, both get
operations will complete first, each returning undefined
, then each set operation will be setting 1
. You could fix the above by queuing the second get
on the first set
, but that isn't always feasible across multiple pieces of code. Instead:
1// Instead: 2import { update } from 'idb-keyval'; 3 4update('counter', (val) => (val || 0) + 1); 5update('counter', (val) => (val || 0) + 1);
This will queue the updates automatically, so the first update
set the counter
to 1
, and the second update
sets it to 2
.
Delete a particular key from the store.
1import { del } from 'idb-keyval'; 2 3del('hello');
Delete many keys at once. This is faster than calling del
multiple times.
1import { del, delMany } from 'idb-keyval'; 2 3// Instead of: 4Promise.all([del(123), del('hello')]) 5 .then(() => console.log('It worked!')) 6 .catch((err) => console.log('It failed!', err)); 7 8// It's faster to do: 9delMany([123, 'hello']) 10 .then(() => console.log('It worked!')) 11 .catch((err) => console.log('It failed!', err));
Clear all values in the store.
1import { clear } from 'idb-keyval'; 2 3clear();
Get all entries in the store. Each entry is an array of [key, value]
.
1import { entries } from 'idb-keyval'; 2 3// logs: [[123, 456], ['hello', 'world']] 4entries().then((entries) => console.log(entries));
Get all keys in the store.
1import { keys } from 'idb-keyval'; 2 3// logs: [123, 'hello'] 4keys().then((keys) => console.log(keys));
Get all values in the store.
1import { values } from 'idb-keyval'; 2 3// logs: [456, 'world'] 4values().then((values) => console.log(values));
By default, the methods above use an IndexedDB database named keyval-store
and an object store named keyval
. If you want to use something different, see custom stores.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 4/30 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
12 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More