helpers.js (1576B)
1 /** 2 * @description - Function will create a database with the supplied name 3 * and also create an object store with the specified name. 4 * If a db with the name dbName exists, this will raze the 5 * existing DB beforehand. 6 * @param {string} dbName 7 * @param {string} objectStoreName 8 * @param {testCase} t 9 * @returns {Promise} - A promise that resolves to an indexedDB open request 10 */ 11 function createDB(dbName, objectStoreName, t) { 12 return new Promise((resolve, reject) => { 13 const openRequest = indexedDB.open(dbName); 14 t.add_cleanup(() => { 15 indexedDB.deleteDatabase(dbName); 16 }); 17 openRequest.onerror = () => { 18 reject(openRequest.error); 19 }; 20 openRequest.onsuccess = () => { 21 resolve(openRequest.result); 22 }; 23 openRequest.onupgradeneeded = (event) => { 24 openRequest.result.createObjectStore(objectStoreName); 25 }; 26 }); 27 } 28 29 /** 30 * @description - This function will wrap an IDBTransaction in a promise, 31 * resolving in the oncomplete() method and rejecting with the 32 * transaction error in the onabort() case. 33 * @param {IDBTransaction} transaction - The transaction to wrap in a promise. 34 * @returns {Promise} - A promise that resolves when the transaction is either 35 * aborted or completed. 36 */ 37 function transactionPromise(transaction) { 38 return new Promise((resolve, reject) => { 39 transaction.onabort = () => { 40 reject(transaction.error); 41 }; 42 transaction.oncomplete = () => { 43 resolve(); 44 }; 45 }); 46 }