Loading repository data…
Loading repository data…
indexeddbshim / repository
A polyfill for IndexedDB using WebSql
(see also licenses for dev. deps.)
| 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.
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 install indexeddbshim
or
yarn add indexeddbshim
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>
const setGlobalVars = require('indexeddbshim');
global.window = global; // We'll allow ourselves to use `window.indexedDB` or `indexedDB` as a global
setGlobalVars(); // See signature below
import setGlobalVars from 'indexeddbshim';
import setGlobalVars from 'indexeddbshim/src/node-UnicodeIdentifiers';
// Or without Unicode support
// import setGlobalVars from 'indexeddbshim/src/node';
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.
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:
winObj object if definedwindow (for Node, define global.window = global;)self (for web workers)global (for Node)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.
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
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
specific connection to close.
The third argument msg will be appended to the AbortError that will be
triggered on the transactions of the connection.
Individual IDBDatabase database instances can also be force-closed
with a particular message:
db.__forceClose(msg);
Establishes a connectionQueue for the supplied (or current) origin.
The queue is otherwise only keyed to the detected origin on the loading of the IndexedDBShim script, though this is usually the desired behavior.
The IndexedDB polyfill has sourcemaps enabled, so the polyfill can be debugged even if the minified file is included.
To print out detailed debug messages, add this line to your script:
shimIndexedDB.__debug(true);
Rather than using globals, a method has been provided to share state across IndexedDBShim modules.
Configuration can be set early in the non-invasive browser and Node builds
via the second argument to setGlobalVars() (see its definition above).
Its signature (for setting configuration after shimIndexedDB is created) is:
shimIndexedDB.__setConfig({
property: value, property2: value2, ...otherProperties
});
or:
shimIndexedDB.__setConfig(property, value);
createDOMException(name, message)A utility for creating a DOMException instance. Attempts to use any
available native implementation.
The available properties relevant to browser or Node are:
shimIndexedDB.__debug(val))IDBFactory.open call to the same name and version (assuming
no deletes or aborts causing rollbacks) will reuse the same SQLite
openDatabase instance.IDBFactory
methods (open