Skip to main content

transactionMiddleware

Overview

The transaction middleware allows you to mark model actions/flows as transactions, this is, if such action/flow throws then any changes performed during such will be reverted before the exception is actually thrown.

There are two ways to mark an action/flow as a transaction. As a decorator and programmatically.

As a decorator:

@model("MyApp/MyBalance")
class MyBalance extends Model({
balance: prop<number>(),
}) {
@transaction
@modelAction
addMoney(cents: number) {
this.balance += cents
// imagine that something else goes wrong
// in this case balance will be reverted to the value that
// was there before the action started
throw new Error("...")
}
}

Programmatically:

@model("MyApp/MyModel")
class MyBalance extends Model({
balance: prop<number>(),
}) {
@modelAction
addMoney(cents: number) {
this.balance += cents
// imagine that something else goes wrong
// in this case balance will be reverted to the value that
// was there before the action started
throw new Error("...")
}

// we could for example add it on init (for all instances)
onInit() {
transactionMiddleware({
model: this,
actionName: "addMoney",
})
}
}

// or for a particular instance
const myBalance = new MyBalance({ balance: 100 })
transactionMiddleware({
model: myBalance,
actionName: "addMoney",
})