Web APIs

IDBDatabase.transaction()

Try the free JavaScript compiler

Creates a scoped transaction with a mode such as readonly or readwrite. The example intentionally catches a prohibited write.

Formal Syntax & Structure

javascript syntaxjavascript
db.transaction(storeNames, mode?, options?)

Parameters & Attributes

NameTypeDescription
storeNamesstring | string[]Store scope.
modestringTransaction mode.
optionsobjectTransaction options.

Return Value

IDBTransaction

New transaction.

Working Example

Example Snippetjavascript
(async () => {const name="bytemind-v12-fixture";const requestValue=r=>new Promise((resolve,reject)=>{r.onsuccess=()=>resolve(r.result);r.onerror=()=>reject(r.error);});const finished=tx=>new Promise((resolve,reject)=>{tx.oncomplete=resolve;tx.onabort=()=>reject(tx.error||new Error("aborted"));});await requestValue(indexedDB.deleteDatabase(name));const open=indexedDB.open(name,1);open.onupgradeneeded=()=>{const s=open.result.createObjectStore("items",{keyPath:"id"});s.createIndex("byCategory","category");};const db=await requestValue(open);try{const tx=db.transaction("items","readonly");try{tx.objectStore("items").put({id:1});}catch(e){console.log(e.name);}}finally{db.close();await requestValue(indexedDB.deleteDatabase(name));}})()
JavaScript Reference