Rammohan
Rammohan

Reputation: 619

Transaction support in nodejs

Is there any transaction support in nodejs similar to EJB for java. I don't seem to find any. Most of the posts suggest using EJB's in addition to nodejs.

Would vert.x be better in such a situation. I need to set up a payment gateway on an asynchronous model. Wondering whether to go for vert.x or nodejs.

Upvotes: 0

Views: 411

Answers (2)

bvdb
bvdb

Reputation: 24770

As @Wangot pointed out, there are transactions in various Node.js packages, and sequelize is a great package because it can connect to various SQL-based databases.

If you use sequelize, and if you come from a java background, then also take a look at the zb-sequelize npm package. It simplifies transaction management extremely by adding 2 decorators: @Transactional and @Tx

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) {
}

If you've worked with Spring before, then this will probably look familiar.

Upvotes: 0

Wangot
Wangot

Reputation: 91

You can use an ORM for Node.js that has support for transaction like Sequelize.js. Although there are some limitation but can support a simple implementation.

Here is a sample code from sequelize

 return sequelize.transaction().then(function (t) {
  return User.create({
    firstName: 'Homer',
    lastName: 'Simpson'
  }, {transaction: t}).then(function (user) {
    return user.addSibling({
      firstName: 'Lisa',
      lastName: 'Simpson'
    }, {transaction: t});
  }).then(function () {
    t.commit();
  }).catch(function (err) {
    t.rollback();
  });
});

Upvotes: 1

Related Questions