Socket
Socket
Sign inDemoInstall

idb-keyval

Package Overview
Dependencies
Maintainers
1
Versions
44
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

idb-keyval - npm Package Compare versions

Comparing version 3.2.0 to 4.0.0

.nvmrc

5

missing-types.d.ts
interface IDBObjectStore {
openKeyCursor(range?: IDBKeyRange | IDBValidKey, direction?: IDBCursorDirection): IDBRequest;
openKeyCursor(
range?: IDBKeyRange | IDBValidKey,
direction?: IDBCursorDirection,
): IDBRequest;
}

105

package.json
{
"name": "idb-keyval",
"version": "3.2.0",
"description": "A super-simple-small keyval store built on top of IndexedDB",
"main": "./dist/idb-keyval-cjs.js",
"module": "./dist/idb-keyval.mjs",
"types": "./dist/idb-keyval.d.ts",
"scripts": {
"build": "del dist && rollup -c && npm run compress-iife && npm run create-compat && npm run create-cjs-compat && npm run compress-amd",
"compress-iife": "uglifyjs --compress --mangle -o dist/idb-keyval-iife.min.js dist/idb-keyval-iife.js",
"create-compat": "babel dist/idb-keyval-iife.js | uglifyjs --compress --mangle > dist/idb-keyval-iife-compat.min.js",
"create-cjs-compat": "babel dist/idb-keyval-cjs.js | uglifyjs --compress --mangle > dist/idb-keyval-cjs-compat.min.js",
"compress-amd": "uglifyjs --compress --mangle -o dist/idb-keyval-amd.min.js dist/idb-keyval-amd.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/jakearchibald/idb-keyval.git"
},
"keywords": [
"idb",
"indexeddb",
"store",
"keyval",
"localstorage",
"storage",
"promise"
],
"author": "Jake Archibald",
"contributors": [
"Benny Powers"
],
"license": "Apache-2.0",
"bugs": {
"url": "https://github.com/jakearchibald/idb-keyval/issues"
},
"homepage": "https://github.com/jakearchibald/idb-keyval#readme",
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-preset-env": "^1.6.1",
"del-cli": "^1.1.0",
"rollup": "^0.56.5",
"rollup-plugin-typescript2": "^0.12.0",
"typescript": "^2.7.2",
"uglify-es": "^3.3.9"
"name": "idb-keyval",
"version": "4.0.0",
"description": "A super-simple-small keyval store built on top of IndexedDB",
"main": "./dist/cjs-compat/index.js",
"module": "./dist/esm/index.js",
"exports": "./dist/esm/index.js",
"types": "./dist/esm/index.d.ts",
"sideEffects": false,
"scripts": {
"build": "rollup -c && node --experimental-modules lib/size-report.mjs",
"dev": "rollup -cw & serve"
},
"repository": {
"type": "git",
"url": "git+https://github.com/jakearchibald/idb-keyval.git"
},
"keywords": [
"idb",
"indexeddb",
"store",
"keyval",
"localstorage",
"storage",
"promise"
],
"author": "Jake Archibald",
"license": "Apache-2.0",
"bugs": {
"url": "https://github.com/jakearchibald/idb-keyval/issues"
},
"homepage": "https://github.com/jakearchibald/idb-keyval#readme",
"devDependencies": {
"@babel/core": "^7.12.10",
"@babel/plugin-external-helpers": "^7.12.1",
"@babel/plugin-transform-runtime": "^7.12.10",
"@babel/preset-env": "^7.12.11",
"@babel/runtime": "^7.12.5",
"@rollup/plugin-babel": "^5.2.2",
"@rollup/plugin-commonjs": "^17.0.0",
"@rollup/plugin-node-resolve": "^11.0.1",
"@types/chai": "^4.2.14",
"@types/mocha": "^8.2.0",
"chai": "^4.2.0",
"del": "^6.0.0",
"filesize": "^6.1.0",
"glob": "^7.1.6",
"husky": "^4.3.7",
"lint-staged": "^10.5.3",
"mocha": "^8.2.1",
"prettier": "^2.2.1",
"rollup": "^2.36.1",
"rollup-plugin-terser": "^7.0.2",
"serve": "^11.3.2",
"typescript": "^4.1.3"
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"*.{js,css,md,ts,html}": "prettier --write"
}
}
# IDB-Keyval
[![npm](https://img.shields.io/npm/v/idb-keyval.svg)](https://www.npmjs.com/package/idb-keyval)
[![size](http://img.badgesize.io/https://cdn.jsdelivr.net/npm/idb-keyval/dist/idb-keyval-iife.min.js?compression=gzip)](http://img.badgesize.io/https://cdn.jsdelivr.net/npm/idb-keyval/dist/idb-keyval-iife.min.js)
This is a super-simple-small promise-based keyval store implemented with IndexedDB, largely based on [async-storage by Mozilla](https://github.com/mozilla-b2g/gaia/blob/master/shared/js/async_storage.js).
This is a super-simple promise-based keyval store implemented with IndexedDB, originally based on [async-storage by Mozilla](https://github.com/mozilla-b2g/gaia/blob/master/shared/js/async_storage.js).
[localForage](https://github.com/localForage/localForage) offers similar functionality, but supports older browsers with broken/absent IDB implementations. Because of that, it's 7.4k, whereas idb-keyval is < 600 bytes. Also, it's tree-shaking friendly, so you'll probably end up using fewer than 500 bytes. Pick whichever works best for you!
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 ~450 bytes.
This is only a keyval store. If you need to do more complex things like iteration & indexing, check out [IDB on NPM](https://www.npmjs.com/package/idb) (a little heavier at 1.7k). The first example in its README is how to recreate this library.
[localForage](https://github.com/localForage/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](https://www.npmjs.com/package/idb) (a little heavier at 1k). The first example in its README is how to create a keyval store.
## Installing
### Recommended: Via npm + webpack/rollup/parcel/etc
```sh
npm install idb-keyval
```
Now you can require/import `idb-keyval`:
```js
import { get, set } from 'idb-keyval';
```
If you're targeting older versions of IE, you may have more luck with:
```js
// Import a Promise polyfill
import 'es6-promise/auto';
import { get, set } from 'idb-keyval/dist/esm-compat';
```
### All bundles
- `dist/cjs/index.js` CommonJS module.
- `dist/cjs-compat/index.js` CommonJS module, transpiled for older browsers.
- `dist/esm/index.js` EcmaScript module.
- `dist/esm-compat/index.js` EcmaScript module, transpiled for older browsers.
- `dist/iife/index.js` Minified plain JS, which creates an `idbKeyval` global containing all methods.
- `dist/iife-compat/index.js` As above, but transpiled for older browsers.
These built versions are also available on jsDelivr, e.g.:
```html
<script src="https://cdn.jsdelivr.net/npm/idb-keyval@4/dist/iife/index.js"></script>
<!-- Or in modern browsers: -->
<script type="module">
import {
get,
set,
} from 'https://cdn.jsdelivr.net/npm/idb-keyval@4/dist/esm/index.js';
</script>
```
## Usage

@@ -20,6 +65,5 @@

set('hello', 'world');
set('foo', 'bar');
```
Since this is IDB-backed, you can store anything structured-clonable (numbers, arrays, objects, dates, blobs etc).
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).

@@ -33,3 +77,3 @@ All methods return promises:

.then(() => console.log('It worked!'))
.catch(err => console.log('It failed!', err));
.catch((err) => console.log('It failed!', err));
```

@@ -43,3 +87,3 @@

// logs: "world"
get('hello').then(val => console.log(val));
get('hello').then((val) => console.log(val));
```

@@ -49,13 +93,76 @@

### keys:
### setMany:
Set many keyval pairs at once. This is faster than calling `set` multiple times.
```js
import { keys } from 'idb-keyval';
import { set, setMany } from 'idb-keyval';
// logs: ["hello", "foo"]
keys().then(keys => console.log(keys));
// Instead of:
Promise.all([set(123, 456), set('hello', 'world')])
.then(() => console.log('It worked!'))
.catch((err) => console.log('It failed!', err));
// It's faster to do:
setMany([
[123, 456],
['hello', 'world'],
])
.then(() => console.log('It worked!'))
.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.
### getMany:
Get many keys at once. This is faster than calling `get` multiple times. Resolves with an array of values.
```js
import { get, getMany } from 'idb-keyval';
// Instead of:
Promise.all([get(123), get('hello')]).then(([firstVal, secondVal]) =>
console.log(firstVal, secondVal),
);
// It's faster to do:
getMany([123, 'hello']).then(([firstVal, secondVal]) =>
console.log(firstVal, secondVal),
);
```
### update:
Transforming a value (eg incrementing a number) using `get` and `set` is risky, as both `get` and `set` are async and non-atomic:
```js
// Don't do this:
import { get, set } from 'idb-keyval';
get('counter').then((val) =>
set('counter', (val || 0) + 1);
);
get('counter').then((val) =>
set('counter', (val || 0) + 1);
);
```
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 each across multiple pieces of code. Instead:
```js
// Instead:
import { update } from 'idb-keyval';
update('counter', (val) => (val || 0) + 1);
update('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`.
### del:
Delete a particular key from the store.
```js

@@ -69,2 +176,4 @@ import { del } from 'idb-keyval';

Clear all values in the store.
```js

@@ -76,56 +185,68 @@ import { clear } from 'idb-keyval';

### Custom stores:
### entries:
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:
Get all entries in the store. Each entry is an array of `[key, value]`.
```js
import { Store, set } from 'idb-keyval';
import { entries } from 'idb-keyval';
const customStore = new Store('custom-db-name', 'custom-store-name');
set('foo', 'bar', customStore);
// logs: [[123, 456], ['hello', 'world']]
entries().then((entries) => console.log(entries));
```
That's it!
### keys:
## Installing
Get all keys in the store.
### Via npm + webpack/rollup
```js
import { keys } from 'idb-keyval';
```sh
npm install idb-keyval
// logs: [123, 'hello']
keys().then((keys) => console.log(keys));
```
Now you can require/import `idb-keyval`:
### values:
Get all values in the store.
```js
import { get, set } from 'idb-keyval';
import { values } from 'idb-keyval';
// logs: [456, 'world']
values().then((values) => console.log(values));
```
If you're targeting older versions of IE, you may have more luck with:
### Custom stores:
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](./custom-stores.md).
## Updating
# Updating from 3.x
The changes between 3.x and 4.x related to custom stores.
Old way:
```js
const idb = require('idb-keyval/dist/idb-keyval-cjs-compat.min.js');
// This no longer works in 4.x
import { Store, set } from 'idb-keyval';
const customStore = new Store('custom-db-name', 'custom-store-name');
set('foo', 'bar', customStore);
```
### Via `<script>`
New way:
* `dist/idb-keyval.mjs` is a valid JS module.
* `dist/idb-keyval-iife.js` can be used in browsers that don't support modules. `idbKeyval` is created as a global.
* `dist/idb-keyval-iife.min.js` As above, but minified.
* `dist/idb-keyval-iife-compat.min.js` As above, but works in older browsers such as IE 10.
* `dist/idb-keyval-amd.js` is an AMD module.
* `dist/idb-keyval-amd.min.js` As above, but minified.
```js
import { createStore, set } from 'idb-keyval';
These built versions are also available on jsDelivr, e.g.:
```html
<script src="https://cdn.jsdelivr.net/npm/idb-keyval@3/dist/idb-keyval-iife.min.js"></script>
<!-- Or in modern browsers: -->
<script type="module">
import { get, set } from 'https://cdn.jsdelivr.net/npm/idb-keyval@3/dist/idb-keyval.mjs';
</script>
const customStore = createStore('custom-db-name', 'custom-store-name');
set('foo', 'bar', customStore);
```
## Updating from 2.x
For more details, see [custom stores](./custom-stores.md).
# Updating from 2.x
2.x exported an object with methods:

@@ -132,0 +253,0 @@

@@ -1,20 +0,162 @@

import typescript from 'rollup-plugin-typescript2';
import { promises as fsp } from 'fs';
import { basename } from 'path';
import { promisify } from 'util';
import simpleTS from './lib/simple-ts';
import del from 'del';
import { terser } from 'rollup-plugin-terser';
import commonjs from '@rollup/plugin-commonjs';
import resolve from '@rollup/plugin-node-resolve';
import babel, { getBabelOutputPlugin } from '@rollup/plugin-babel';
import glob from 'glob';
export default {
input: 'idb-keyval.ts',
plugins: [typescript()],
output: [{
file: 'dist/idb-keyval-iife.js',
format: 'iife',
name: 'idbKeyval'
}, {
file: 'dist/idb-keyval-cjs.js',
format: 'cjs'
}, {
file: 'dist/idb-keyval.mjs',
format: 'es'
}, {
file: 'dist/idb-keyval-amd.js',
format: 'amd',
}]
};
const globP = promisify(glob);
function removeDefs() {
return {
generateBundle(_, bundle) {
for (const key of Object.keys(bundle)) {
if (key.includes('.d.ts')) delete bundle[key];
}
},
};
}
const babelPreset = [['@babel/preset-env', { targets: { ie: '10' } }]];
function getBabelPlugin() {
return getBabelOutputPlugin({
presets: babelPreset,
allowAllFormats: true,
});
}
export default async function ({ watch }) {
const devBuild = watch;
await del('dist');
if (devBuild)
return {
input: 'test/index.ts',
plugins: [
simpleTS('test', { watch }),
commonjs(),
// When testing IE10
// babel({
// presets: babelPreset,
// babelHelpers: 'runtime',
// extensions: ['.js', '.jsx', '.es6', '.es', '.mjs', '.ts'],
// plugins: [
// [
// '@babel/plugin-transform-runtime',
// {
// useESModules: true,
// },
// ],
// ],
// exclude: /node_modules/,
// }),
resolve(),
// Copy HTML file
{
async generateBundle() {
this.emitFile({
type: 'asset',
source: await fsp.readFile(`test/index.html`),
fileName: 'index.html',
});
},
},
],
output: [
{
file: 'dist/test/index.js',
format: 'es',
},
],
watch: {
clearScreen: false,
// Don't watch the ts files. Instead we watch the output from the ts compiler.
exclude: ['**/*.ts', '**/*.tsx'],
},
};
return [
// Main builds
{
input: 'src/index.ts',
plugins: [simpleTS('src')],
output: [
{
file: 'dist/esm/index.js',
format: 'es',
},
{
file: 'dist/cjs/index.js',
format: 'cjs',
},
{
file: 'dist/iife/index-min.js',
format: 'iife',
name: 'idbKeyval',
esModule: false,
plugins: [
terser({
compress: { ecma: 2020 },
}),
removeDefs(),
],
},
],
},
// Compat builds
{
input: 'src/index.ts',
external: (id) => {
if (id.startsWith('@babel/runtime')) return true;
},
plugins: [simpleTS('src', { noBuild: true })],
output: [
{
file: 'dist/esm-compat/index.js',
format: 'es',
plugins: [getBabelPlugin()],
},
{
file: 'dist/cjs-compat/index.js',
format: 'cjs',
plugins: [getBabelPlugin()],
},
{
file: 'dist/iife-compat/index-min.js',
format: 'iife',
name: 'idbKeyval',
esModule: false,
plugins: [
getBabelPlugin(),
terser({
compress: { ecma: 5 },
}),
removeDefs(),
],
},
],
},
// Size tests
...(await globP('size-tests/*.js').then((paths) =>
paths.map((path) => ({
input: path,
plugins: [
terser({
compress: { ecma: 2020 },
}),
],
output: [
{
file: `dist/size-tests/${basename(path)}`,
format: 'es',
},
],
})),
)),
];
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc