Promise-based wrapper around the IndexedDB API.
You would mostly want to make use of the IDB inside a ServiceWorker:
self.importScripts('idb.js');Open a new IDB connection and setup the tables you need for your project:
var idb = new IDB({
table1: {},
table2: {keyPath: 'id'},
table3: {autoIncrement: true},
}, config);The following settings can be passed in the optional config object:
namesets the Database name (defaults to "IDB")versionprovides the version of the table structure (defaults to 1)- An increment in version triggers a database upgrade
upgradedefines a function to perform when a database upgrade was performed
You can pass the follwing options for each table:
keyPathspecifies a "primary key" of inserted objectsautoIncrementdetermines, whether inserted objects get an incrementing numeric key
The defined tables will be available for operations in the newly created idb object like idb.table1. Remember that all operations return a Promise.
idb.<table>.put({id: 'foo', a: 'b'})- Inserts the object with key "foo" when using
keyPath: 'id' - Inserts the object with an automatically incremented key when using
autoIncrement: true
- Inserts the object with key "foo" when using
idb.<table>.put('value')- Inserts the value with an automatically incremented key when using
autoIncrement: true
- Inserts the value with an automatically incremented key when using
idb.<table>.put({id: 'foo', a: 'b'}, 'bar')- Inserts the object with key "bar" independent of
autoIncrementbut only withoutkeyPath
- Inserts the object with key "bar" independent of
idb.<table>.get('foo')- Selects the object or value with key "foo" (also works with numeric keys)
idb.<table>.all(filter)- Applies a provided filtering function on all entries to decide which ones will be selected
- Example:
idb.<table>.all(item => item.amount > 5)
idb.<table>.delete('foo')- Deletes the object or value with key "foo" (also works with numeric keys)
idb.<table>.clear()- Deletes all entries in the table