Avoiding race conditions in TypeORM

Viewed 896

I'm working on a code base that uses TypeORM and we have a table where we keep a user's balance information.

Here's what our code kind of looks like:

async updateAccountInfo(account: Account, tx: Transaction) {
  if (tx.type == 'credit') {
    account.balance = account.balance + amount
  }
  else if (tx.type == 'debit') {
    account.balance = account.balance - amount
  }

  await this.accountRepo.save(account)
}

The problem is we're starting to get race conditions when transactions come in at the same time and we end up getting the wrong balance (there is a lot more that the code does like updating the ledger, etc but I can't post it here and don't think it's pertinent to the question).

Is it possible to rewrite this so that I can leverage the database (mysql) instead to make the calculation so I can hopefully avoid the race condition but also without having to use the query builder.

ie. on sequelize, you may be able to do something like

account.udpate({ 
  balance: Sequelize.literal(`balance + ${amount}`), 
  where: { id: account.id } 
})

On typeORM, however, I'm not sure how to do this and I couldn't find anything about it in the documentation. Any help would be great.

0 Answers
Related