This is a tiny library that mirrors IndexedDB, but replaces the weird IDBRequest objects with promises, plus a couple of other small changes.
This is very similar to localStorage, but async. If this is all you need, you may be interested in idb-keyval, you can always upgrade to this library later.
const dbPromise = idb; const idbKeyval = { return dbPromise; } { return dbPromise; } { return dbPromise; } { return dbPromise; } { return dbPromise; };keyValStore; // logs: {hello: 'world'} keyValStore;Imagine we had a set of objects like…
const dbPromise = idb;dbPromise;dbPromise;dbPromise;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.
const tx = db;const store = tx;store;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.
This is a simple wrapper library, so you're exposed to bugs in the underlying implementation. Unfortunately Safari has a lot of these.
idbThis 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;idb.delete(name)Behaves like indexedDB.deleteDatabase, but returns a promise.
idb;DBProperties:
IDBDatabase:
nameversionobjectStoreNamesMethods:
close - as idbDatabase.closetransaction - as idbDatabase.transaction, but returns a TransactionUpgradeDBAs DB, except:
Properties:
transaction - this is a property rather than a method. It's a Transaction representing the upgrade transactionoldVersion - the previous version of the DB seen by the browser, or 0 if it's newMethods:
createObjectStore - as idbDatabase.createObjectStore, but returns an ObjectStoredeleteObjectStore - as idbDatabase.deleteObjectStoreTransactionProperties:
complete - a promise. Resolves when transaction completes, rejects if transaction aborts or errorsIDBTransaction:
objectStoreNamesmodeMethods:
abort - as idbTransaction.abortobjectStore - as idbTransaction.objectStore, but returns an ObjectStoreidb;ObjectStoreProperties:
IDBObjectStore:
namekeyPathindexNamesautoIncrementMethods:
IDBObjectStore, but returns a promise that resolves/rejects based on operation success/failure:
putadddeletecleargetgetAllgetAllKeyscountIDBObjectStore, but returns a promise that resolves with a Cursor:
openCursoropenKeyCursordeleteIndex - as idbObjectStore.deleteIndexIDBObjectStore, but returns an Index:
createIndexindexiterateCursor - see belowiterateKeyCursor - see belowiterateCursor & iterateKeyCursorDue to the microtask issues in some browsers, iterating over a cursor using promises doesn't always work:
const tx = db;tx;txcomplete;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:
const tx = db;tx;txcomplete;The intent is to remove iterateCursor and iterateKeyCursor from the library once browsers support promises and microtasks correctly.
IndexProperties:
IDBIndex:
namekeyPathmultiEntryuniqueMethods:
IDBIndex, but returns a promise that resolves/rejects based on operation success/failure:
getgetKeygetAllgetAllKeyscountIDBIndex, but returns a promise that resolves with a Cursor:
openCursoropenKeyCursoriterateCursor - as objectStore.iterateCursor but over the indexiterateKeyCursor - as objectStore.iterateKeyCursor but over the indexProperties:
IDBCursor:
directionkeyprimaryKeyvalueMethods:
IDBCursor, but returns a promise that resolves/rejects based on operation success/failure:
updatedeleteIDBCursor, but returns a promise that resolves with a Cursor:
advancecontinuecontinuePrimaryKey