@d34d/w-orm - v0.6.0
    Preparing search index...

    Function Transaction

    • 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.

      Type Parameters

      • T

      Parameters

      • mode: IDBTransactionMode

        The transaction mode

      • models: Constructor<Model>[]

        The models to use in the transaction

      • transactionCallback: TransactionCallback<T>

        The callback to execute in the transaction

      Returns Promise<T>

      • The result of the transactionCallback
      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
      })