Some Promise based wrapper is definitely handy. I wrote my own as well but it’s very simple one:
async function dbTransaction(tableName: DB_TABLE, transactionMode: IDBTransactionMode) {
const db = await _dbPromise;
return db.transaction([tableName], transactionMode).objectStore(tableName);
}
async function dbOperation<T>(transaction: IDBObjectStore | IDBIndex, fnName: KeysOfType<IDBObjectStore & IDBIndex, Function>, ...params: any[]): Promise<T> {
return new Promise((resolve, reject) => {
// @ts-ignore - I'm not sure this can be done "by the book" without loosing the flexibility
const idbRequest = transaction[fnName].apply(transaction, params);
idbRequest.onerror = reject;
idbRequest.onsuccess = () => {
resolve(idbRequest.result)
};
})
}
async function dbTransactionOperation<T>(tableName: DB_TABLE, transactionMode: IDBTransactionMode, fnName: KeysOfType<IDBObjectStore & IDBIndex, Function>, ...params: any[]) {
return dbOperation<T>(await dbTransaction(tableName, transactionMode), fnName, ...params)
}
It’s in TypeScript, but you can just remove types and it will work fine. Using these functions you can then easily query all your data with Promises.
Also as a user of IndexedDB, make sure to vote for some bugs!
https://bugzilla.mozilla.org/show_bug.cgi?id=944918 (this one is 6 years old!)
https://bugzilla.mozilla.org/show_bug.cgi?id=1522188
https://bugzilla.mozilla.org/show_bug.cgi?id=1478602