IndexedDB is the browser's asynchronous, transactional object database, one set of databases per origin.
- Versions.
indexedDB.open(name, version)opens a database. A higher version firesupgradeneededinside aversionchangetransaction, the only place to create or delete object stores and indexes. - Object stores hold structured clones (objects,
Date,Map,Blob…), keyed by akeyPathproperty, a key generator or an explicit key. Indexes query by another property:store.index('byPrice').getAll(IDBKeyRange.upperBound(8)). - Transactions. Every read and write is a request in a transaction over named stores,
readonlyorreadwrite. Read-write transactions over the same store run one at a time. A failed request you don't handle aborts the transaction and rolls back its writes.
Why it commits on its own. A transaction is active while the task that created it runs, and again while each of its requests' success or error events is dispatched. Requests can only be placed while it's active. When every request has finished and no new one was placed, the browser commits. There's no keep-alive: the transaction lasts exactly as long as you keep placing requests from its own callbacks.
So a fetch(), a timer or any other non-IndexedDB wait in the middle breaks it. When your code resumes, the transaction is inactive or already committed, and the next request throws TransactionInactiveError. Awaiting a promise that wraps an IndexedDB request is fine: the success handler resolves it, and the code after await runs in the microtask checkpoint inside that dispatch, while the transaction is still active.
The fix is ordering: fetch first, then open the transaction and write. tx.commit() commits as soon as the placed requests finish, without waiting for their handlers, and the durability: 'relaxed' option trades crash safety for speed.