hyundeock
hyundeock

Reputation: 505

how to handle sequelize transaction properly

I wrote the code to commit and rollback using a sequelize transaction. But I do not know whether I need to declare it every time, by calling sequelize.transaction() at the top of every part, where transaction should be applied.

I want to find a more structured and reusable method. For example, should I use middleware or some design pattern? Please let me know.

--- update --- Sorry, previous question is wrong.

In sequelize, the question was what to do when resource access is restricted by transaction isolation level.

At a high level of isolation, if a resource is accessed at the same time, it throws an error. Is there a way to try this again or do something right?

In some cases even locks at the table level It directly accesses the database and releases the lock.

Upvotes: 1

Views: 1444

Answers (1)

bvdb
bvdb

Reputation: 24710

Please take a look at the zb-sequelize npm package. It simplifies transaction management extremely by adding 2 decorators.

import { Transactional, Tx } from 'zb-sequelize';

@Transactional
function fooBar(@Tx transaction) {
  foo(transaction);
  bar(transaction);
}

@Transactional
function foo(@Tx transaction) {
}

@Transactional
function bar(@Tx transaction) {
}

Upvotes: 1

Related Questions