Package Exports
- idb
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (idb) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
IndexedDB Promised
This is a tiny library that mirrors IndexedDB, but replaces the weird IDBRequest objects with promises, plus a couple of other small changes.
Limitations
Transaction lifetime
At time of writing, all browsers aside from Chrome don't treat promise callbacks as microtasks, or call microtasks incorrectly. This means transactions end by the time promise callbacks are called. In practice, this means you cannot perform transactions that involve waiting for a value, then using it within the same transaction.
let tx = db.transaction('store', 'readwrite');
let store = db.objectStore('store');
store.get('hello').then(val => store.put(val, 'foo'));The above will fail in browsers other than Chrome, because the transaction has closed by the time we get to the .put.
You can work around this in Firefox by using a promise polyfill that correctly uses microtasks, such as es6-promise.
Safari
This is a simple wrapper library, so you're exposed to bugs in the underlying implementation. Unfortunately Safari has a lot of these.
idb
This is your entry point to the API. It's exposed to the global scope unless you're using a module system such as browserify, in which case it's the exported object.
idb.open(name, version, upgradeCallback)
This method returns a promise that resolves to a DB.
name and version behave as they do in indexedDB.open.
upgradeCallback is called if version is greater than the version last opened. It's similar to IDB's onupgradeneeded. The callback receives an instance of UpgradeDB.
idb.open('my-database', 2, upgradeDB => {
// Note: we don't use 'break' in this switch statement,
// the fall-through behaviour is what we want.
switch (upgradeDB.oldVersion) {
case 0:
upgradeDB.createObjectStore('key-val');
case 1:
upgradeDB.createObjectStore('stuff', {keyPath: ''});
}
}).then(db => console.log("DB opened!", db));idb.delete(name)
Behaves like indexedDB.deleteDatabase, but returns a promise.
idb.delete('my-database').then(_ => console.log('done!'));DB
Properties:
name- asidbDatabase.nameversion- asidbDatabase.versionobjectStoreNames- asidbDatabase.objectStoreNames
Methods:
close- asidbDatabase.closetransaction- asidbDatabase.transaction, but returns aTransaction
UpgradeDB
As DB, except:
Properties:
transaction- this is a property rather than a method. It's aTransactionrepresenting the upgrade transactionoldVersion- the previous version of the DB seen by the browser, or 0 if it's new
Methods:
createObjectStore- asidbDatabase.createObjectStore, but returns anObjectStoredeleteObjectStore- asidbDatabase.deleteObjectStore
Transaction
Properties:
complete- a promise. Resolves when transaction completes, rejects if transaction aborts or errorsobjectStoreNames- asidbTransaction.objectStoreNamesmode- asidbTransaction.mode
Methods:
abort- asidbTransaction.abortobjectStore- asidbTransaction.objectStore, but returns anObjectStore
idb.open('my-database', 1, upgradeDB => {
switch (upgradeDB.oldVersion) {
case 0:
upgradeDB.createObjectStore('key-val');
}
}).then(db => {
let tx = db.transaction('key-val');
tx.objectStore('key-val').put('hello', 'world');
return tx.complete;
}).then(_ => console.log("Done!"));ObjectStore
Properties:
name- asidbObjectStore.namekeyPath- asidbObjectStore.keyPathindexNames- asidbObjectStore.indexNamesautoIncrement- asidbObjectStore.autoIncrement
Methods:
put- asidbObjectStore.putbut returns a promise that resolves/rejects based on operation success/failureadd- asidbObjectStore.addbut returns a promise that resolves/rejects based on operation success/failuredelete- asidbObjectStore.deletebut returns a promise that resolves/rejects based on operation success/failureclear- asidbObjectStore.clearbut returns a promise that resolves/rejects based on operation success/failureget- asidbObjectStore.getbut returns a promise that resolves/rejects based on operation success/failuregetAll- asidbObjectStore.getAllbut returns a promise that resolves/rejects based on operation success/failuregetAllKeys- asidbObjectStore.getAllKeysbut returns a promise that resolves/rejects based on operation success/failurecount- asidbObjectStore.countbut returns a promise that resolves/rejects based on operation success/failureopenCursor- asidbObjectStore.openCursorbut returns a promise that resolves with aCursoropenKeyCursor- asidbObjectStore.openKeyCursorbut returns a promise that resolves with aCursordeleteIndex- asidbObjectStore.deleteIndexcreateIndex- asidbObjectStore.createIndexbut returns anIndexindex- asidbObjectStore.indexbut returns anIndexiterateCursor- see belowiterateKeyCursor- see below
iterateCursor & iterateKeyCursor
Due to the microtask issues in most browsers, iterating over a cursor using promises doesn't really work:
// Currently, this only works in Chrome:
let tx = db.transaction('stuff');
tx.objectStore('stuff').openCursor().then(function cursorIterate(cursor) {
if (!cursor) return;
console.log(cursor.value);
return cursor.continue().then(cursorIterate);
});
tx.complete.then(_ => console.log('done'));So in the mean time, iterateCursor and iterateKeyCursor map to openCursor & openKeyCursor, take identical arguments, plus an additional callback that receives an IDBCursor, so the above example becomes:
let tx = db.transaction('stuff');
tx.objectStore('stuff').iterateCursor(cursor => {
if (!cursor) return;
console.log(cursor.value);
cursor.continue();
});
tx.complete.then(_ => console.log('done'));The intent is to remove iterateCursor and iterateKeyCursor from the library once browsers support promises and microtasks correctly.
Index
Properties:
name- asidbIndex.namekeyPath- asidbIndex.keyPathmultiEntry- asidbIndex.multiEntryunique- asidbIndex.unique
Methods:
get- asidbIndex.get, but returns a promise that resolves/rejects based on operation success/failuregetKey- asidbIndex.getKey, but returns a promise that resolves/rejects based on operation success/failuregetAll- asidbIndex.getAll, but returns a promise that resolves/rejects based on operation success/failuregetAllKeys- asidbIndex.getAllKeys, but returns a promise that resolves/rejects based on operation success/failurecount- asidbIndex.count, but returns a promise that resolves/rejects based on operation success/failureopenCursor- asidbIndex.openCursorbut returns a promise that resolves with aCursoropenKeyCursor- asidbIndex.openKeyCursorbut returns a promise that resolves with aCursoriterateCursor- asobjectStore.iterateCursorbut over the indexiterateKeyCursor- asobjectStore.iterateKeyCursorbut over the index
Cursor
Properties:
direction- asidbCursor.directionkey- asidbCursor.keyprimaryKey- asidbCursor.primaryKeyvalue- asidbCursor.value
Methods:
update- asidbCursor.updatebut returns a promise that resolves/rejects based on operation success/failuredelete- asidbCursor.deletebut returns a promise that resolves/rejects based on operation success/failureadvance- asidbCursor.advancebut returns a promise that resolves to aCursorcontinue- asidbCursor.continuebut returns a promise that resolves to aCursorcontinuePrimaryKey- asidbCursor.continuePrimaryKeybut returns a promise that resolves to aCursor