Skip to content

Implicit flow control

Compare
Choose a tag to compare
@vkondratiuk482 vkondratiuk482 released this 08 Nov 22:04
· 4 commits to main since this release
82c9785

Implicit flow control

Now you don't need to manually call commit/rollback methods. The library will handle it for you. Just create your callback and pass it to the ptx.run method. That also means that you don't need to call ptx.start by yourself, now the package takes responsibility of picking a proper connection based on the AsyncLocalStorage context

async create(payload1, payload2) {
  const callback = async () => {
    const user = await userService.create(payload1);
    const wallet = await walletService.create(payload2);

    return user;
  };

  const user = await ptx.run(callback);

  return user;
}

Nested transactions

Now if we call updateBalance from inside of a create method, the updateBalance won't start a separate transaction, and will be treated as a part of create's transaction. However, if we call updateBalance directly, it will start its own transaction

async create(payload1, payload2) {
  const callback = async () => {
    const user = await userService.create(payload1);
    const wallet = await walletService.create(payload2);

    return user;
  };

  const user = await ptx.run(callback);

  return user;
}

async updateBalance(payload2) {
  const callback = async () => {
    await walletService.updateBalance(payload2);
  };

  return ptx.run(callback);
}

Isolation Levels

Since there is no ptx.start anymore, now you can specify transaction's isolation level as a second argument in ptx.run method

const { IsolationLevels } = require('@mokuteki/propagated-transactions')

// some code

async create(payload1, payload2) {
  const callback = async () => {
    const user = await userService.create(payload1);
    const wallet = await walletService.create(payload2);

    return user;
  };

  const user = await ptx.run(callback, IsolationLevels.Serializable);

  return user;
}