The transaction mode
The models to use in the transaction
The callback to execute in the transaction
await Transaction('readwrite', [User], async (tx) => {
const newUser = await User.create({ name: 'John Doe' }, tx)
const getUser = await User.get(newUser.id, tx)
// Any error thrown in the callback will abort the transaction, this will rollback any changes made
throw new Error('rollback')
// If no error is thrown, the transaction will be committed
})
Starts a db transaction. The tables concerned need to be passed as an array. Depending on the result of the transactionCallback, the transaction will be committed or aborted.
Because of a limitation in the IndexedDB API, the transaction will be automatically committed if we wait for any non transactional operation. (e.g. fetching some data from the network).
This means, that even though the transactionCallback is async, it should not await any non transactional operation.