7.4.2 Configure Node.js Apps as Transaction Initiator or Participant

  1. Add the MicroTx library for Node.js as a dependency in the package.json file.
    "dependencies": {
        "@oracle/microtx": "<version>"
      }

    Where, <version> is the latest version of the MicroTx library for Node.js.

  2. Configure the property values for the MicroTx library. See Configure Library Properties for Node.js Apps.
  3. To enable logging for Node.js applications, you must set additional properties. See Enable Logs for MicroTx Node.js Library.
  4. Configure the MicroTx library properties for the microservice by passing the tmm.properties file in which you have defined the values.
    TrmConfig.init('./tmm.properties');
  5. Import the MicroTx and Express libraries.
    import { Request, Response, Router } from 'express';
    import { getLRAId, LRA, LRAConfig, LRAType, ParticipantStatus, cancelLRA, LRA_HTTP_CONTEXT_HEADER, LRA_HTTP_ENDED_CONTEXT_HEADER } from "@oracle/microtx/lra/lra";
    import { getHeaderValue } from '@oracle/microtx/util/trmutils';
  6. Create a router object to handle requests in your program.
    Use the following code to create a router object named flightSvcRouter.
    const flightSvcRouter = Router();
  7. Enter the URL of the MicroTx Saga coordinator. To get this attribute value, append /lra-coordinator to the URL that you use to access MicroTx. For example, if https://tmm-app:9000/api/v1 is the MicroTx URL, then lraCoordinatorUrl is https://tmm-app:9000/api/v1/lra-coordinator.
    const lraCoordinateUrl = process.env.ORACLE_TMM_TCS_URL
  8. Add the following code to initialize the LRAConfig object for the REST endpoints of the transaction initiator and transaction participant services. The services may expose many REST API endpoints, but you have to initialize LRAConfig object only for the REST API endpoints which need to participate in the Saga transaction.
    const lra: LRA = new LRA("/flight", LRAType.REQUIRES_NEW);
    lra.end = false;
    lra.timeLimitInMilliSeconds = 100000;
    new LRAConfig(lraCoordinateUrl, flightSvcRouter, "/flightService/api", lra, "/complete", "/compensate", "/status", "/after", "", "", "");

    Where,

    • /flight is the REST API endpoint which the transaction initiator application exposes to participate in the Saga transaction.
    • LRAType.REQUIRES_NEW determines if the service participates in an existing Saga transaction or creates a new one. When you set LRAType as REQUIRES_NEW, a new transaction is created. When you set LRAType as MANDATORY, the service participates in an existing transaction. For details about the LRAType values, see Transaction Manager for Microservices TypeScript API Reference.
    • flightSvcRouter is the router object that you have created previously.
    • /flightService/api is the mount point of the flightSvcRouter router. This is the value for the applRouterMountPath field of the LRAConfig object.
    • timeLimitInMilliSeconds is the time period, in milliseconds, within which the transaction must be completed or compensated. If the transaction is not completed within the specified time period, MicroTx compensates the transaction. Decide the time limit based on your business requirement.
    • "/complete", "/compensate", "/status", "/after" are the REST API endpoints for which you want to define your application's business logic.

    During the Saga transaction, MicroTx adds a link header for all the outgoing requests from the REST API endpoints that you have specified.

  9. Define the business logic for all the REST API endpoints that you have mentioned while creating the LRAConfig object.
    flightSvcRouter.put('/complete', async (req, resp) => {
    //application business logic
    });
    
    flightSvcRouter.put('/compensate', async (req, resp) => {
    //application business logic
    });
    
    flightSvcRouter.put('/status', async (req, resp) => {
    //application business logic
    });
    
    flightSvcRouter.put('/after', (req, resp) => {
    //application business logic
    });

    Where, flightSvcRouter is the router object that you have created previously. Although the sample code mentions only put, you can use any HTTP verb based on your business logic.

  10. Save your changes.
Source code of a sample Node.js transaction participant application which uses the MicroTx library is available in the flight folder in the microtx-samples GitHub repository. You can use this as a reference while integrating the MicroTx libraries with your application.