SkillAgentSearch skills...

IndexedDBShim

A polyfill for IndexedDB using WebSql

Install / Use

/learn @indexeddbshim/IndexedDBShim
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

IndexedDB Polyfill

Build Status Dependencies devDependencies npm CDNJS

Tests badge Coverage badge

Known Vulnerabilities Total Alerts Code Quality: Javascript

License

Licenses badge

<!-- [![Licenses dev badge](https://raw.githubusercontent.com/indexeddbshim/indexeddbshim/master/badges/licenses-badge-dev.svg?sanitize=true)](badges/licenses-badge-dev.svg) -->

(see also licenses for dev. deps.)

<!--[![issuehunt-to-marktext](https://issuehunt.io/static/embed/issuehunt-button-v1.svg)](https://issuehunt.io/r/indexeddbshim/indexeddbshim)-->

|Live Demo (stable)! | Live Demo (master)! | | -------------- | ----------------- |

Use a single, indexable, offline storage API across all desktop and mobile browsers and Node.js.

Even if a browser natively supports IndexedDB, you may still want to use this shim. Some native IndexedDB implementations are very buggy. Others are missing certain features. There are also many minor inconsistencies between different browser implementations of IndexedDB, such as how errors are handled, how transaction timing works, how records are sorted, how cursors behave, etc. Using this shim will ensure consistent behavior across all browsers.

Features

Installation

You can download the development or production (minified) script, or install it using NPM.

For Mac, you may need to have CMake installed for the SQLite3 install to work (See Tools->How to Install For Command Line Use) as well as build SQLite3 from source via npm install --build-from-source in the node-sqlite3 directory. Also make sure Python (2.7) is installed.

npm

npm install indexeddbshim

or

yarn add indexeddbshim

Browser set-up

Add the following scripts to your page:

<script src="./node_modules/core-js-bundle/minified.js"></script>
<script src="./node_modules/indexeddbshim/dist/indexeddbshim.min.js"></script>

If you need full Unicode compliance (handling special non-alphanumeric identifiers in store and index names), use the following instead:

<script src="./node_modules/core-js-bundle/minified.js"></script>
<script src="./node_modules/indexeddbshim/dist/indexeddbshim-UnicodeIdentifiers.min.js"></script>

Node set-up

const setGlobalVars = require('indexeddbshim');

global.window = global; // We'll allow ourselves to use `window.indexedDB` or `indexedDB` as a global
setGlobalVars(); // See signature below

ES6 Modules

Bundler for Browser

import setGlobalVars from 'indexeddbshim';

Bundler for Node

import setGlobalVars from 'indexeddbshim/src/node-UnicodeIdentifiers';

// Or without Unicode support
// import setGlobalVars from 'indexeddbshim/src/node';

Usage/API

For the browser scripts, if the browser already natively supports IndexedDB and is not known to be buggy, then the script won't do anything.

Otherwise, assuming WebSQL is available, the script will add the IndexedDB API to the browser (unless you use one of the non-invasive files, in which case setGlobalVars can be used to optionally add the API to an object of your choosing; if you also wish Unicode support, you will need to add it yourself).

Either way, you can use IndexedDB just like normal. Here's an example.

setGlobalVars(<winObj or null>, initialConfig)

In the non-invasive builds (and Node.js), globals are not automatically set. You have the choice to set globals when you wish as well as to set the API on an object of your choosing in place of setting globals.

This is done through setGlobalVars() (which is otherwise called in the browser builds automatically with no arguments).

This function defines shimIndexedDB, indexedDB, IDBFactory, etc. on one of the following objects in order of precedence:

  1. The passed in winObj object if defined
  2. window (for Node, define global.window = global;)
  3. self (for web workers)
  4. global (for Node)
  5. A new empty object

The initialConfig argument, if present, should be an object whose keys are the config properties to set and its values are the config values (see shimIndexedDB.__setConfig below).

If you are adding your own window.openDatabase implementation, supplying it within initialConfig (keyed as openDatabase) will ensure that shimIndexedDB.__useShim() is auto-invoked for you if poor IndexedDB support is detected.

shimIndexedDB.__useShim();

To force IndexedDBShim to shim the browser's native IndexedDB (if our code is not already auto-shimming your browser when detecting poor browser support), add this method call to your script.

On browsers that support WebSQL, this line will completely replace the native IndexedDB implementation with the IndexedDBShim-to-WebSQL implementation.

On browsers that don't support WebSQL, but do support IndexedDB, this line will patch many known problems and add missing features. For example, on Internet Explorer, this will add support for compound keys.

If CFG.addNonIDBGlobals has been set (e.g., on the initialConfig argument of setGlobalVars), the other non-IndexedDB shims necessitated by this library will be polyfilled as possible on the chosen "global" (i.e., ShimEvent, ShimCustomEvent, ShimEventTarget, ShimDOMException, and ShimDOMStringList). Mostly useful for testing.

If CFG.replaceNonIDBGlobals is used, it will instead attempt to add, or if already present, overwrite these globals.

If CFG.fullIDLSupport has been set, the slow-performing Object.setPrototypeOf calls required for full WebIDL compliance will be used. Probably only needed for testing or environments where full introspection on class relationships is required. See this SO topic

shimIndexedDB.__forceClose([dbName], [connIdx], [msg])

The spec anticipates the closing of a database connection with a forced flag.

The spec also mentions some circumstances where this may occur:

A connection may be closed by a user agent in exceptional circumstances, for example due to loss of access to the file system, a permission change, or clearing of the origin’s storage.

Since the latter examples are under the browser's control, this method may be more useful on the server or for unit-testing.

If the first argument, dbName is missing (or null or undefined), all connections to all databases will be force-closed.

If the second argument, connIdx is missing (or null or undefined), all connections with the given name will be force-closed. It can alternatively be an integer representing a 0-based index to indicate a

Related Skills

View on GitHub
GitHub Stars978
CategoryData
Updated19d ago
Forks189

Languages

JavaScript

Security Score

85/100

Audited on Feb 28, 2026

No findings