8.6.2 Configure Node.js App as Transaction Participant

Before you begin, ensure that you have configured the property values for the MicroTx library.

  1. Add the MicroTx library for Node.js as a dependency in the package.json file. The library file is located in the installation_directory/otmm-RELEASE/otmm/nodejs folder.
    "dependencies": {
        "tmmlib-node": "file:oracle-microtx-1.0.0.tgz"
      }
  2. Configure the property values for the MicroTx library. See Configure Library Properties. To enable logging for Node.js applications, you must set additional properties. See Enable Logs for MicroTx Node.js Library.
  3. 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');
  4. Import the MicroTx libraries and the express module files.
    import {HttpMethod, TrmConfig} from "tmmlib-node/util/trmutils";
    import {TCCConfig} from "tmmlib-node/tcc/tcc";
    import {NextFunction, request, Request, Response, Router} from 'express';
  5. Create a router object.
    Use the following code to create a router object named svcRouter2.
    const svcRouter2 = Router();
  6. Add the following code to initialize the TCCConfig object for the confirm and cancel REST API endpoints of the transaction participant service.

    In the following code sample, the transaction participant application exposes the /bookings REST API endpoint. The svcRouter2 is the router object that you have created in the previous step. Replace these values with the values specific to your environment.

    //Initialize TCCConfig object for all the endpoints which need to participant in the TCC transaction
    const tccConfig: TCCConfig = new TCCConfig("/bookings", svcRouter2, HttpMethod.POST, 30);

    Where,

    • /bookings is the REST API endpoint that the transaction participant service exposes.

    • svcRouter2 is the router object that you have created previously.

  7. In the following code sample, the transaction participant service exposes the /bookings/:bookingId REST API endpoint to confirm or cancel the transaction. Replace these values with the values specific to your environment. Also ensure that these endpoints are present in the transaction participant service and the confirm and cancel logic is implemented in the code. The dohotelBooking(), doConfirmBooking(), and doCancelBooking() methods contain the business logic for creating a resource, confirming the transaction, and canceling the transaction respectively. Ensure that the business logic is implemented in the code of the transaction participant service and the endpoints are present.

    You'll also mention the HTTP method that the REST API endpoint uses. MicroTx uses the PUT method to confirm the transaction and the DELETE method to cancel the transaction and release the resources that were reserved for the specified resource URI.

    svcRouter.post('/bookings', asyncHandler(async (req: Request, res: Response) => {
        dohotelBooking(req, res); //app-specific code to create a resource
    }));
    
    svcRouter.put('/bookings/:bookingId', asyncHandler(async (req: Request, res: Response) => {
        doConfirmBooking(req, res); //app-specific code to confirm the transaction
    }));
    
    svcRouter.delete('/bookings/:bookingId', asyncHandler(async (req: Request, res: Response) => {
        doCancelBooking(req, res); //app-specific code to cancel the transaction
    }));
  8. Use the TCCConfig object that you have created earlier to register participants (reserved resource URI) to an existing TCC transaction by calling the addTccParticipant method with the resource URI.
    const bookingUri;
    tccConfig.addTccParticipant(bookingUri);

    When this code is executed, the participant service joins an existing TCC transaction when the initiator service calls the participant service. Also the MicroTx library enlists the participant service with the URIs you provide for the confirm and cancel endpoints.