Skip to Content
2
Jul 06, 2021 at 01:14 PM

How to handle changeset processing in SAP Cloud Application Programming Model in NodeJS

647 Views

Hi,

I am developing CAP application in Node.js and typescript. I have a requirement to process multiple requests (Post/Merge) in a batch call in a changeset and if any one of the requests fail, the complete request should result in an error and nothing should be updated in DB.

Example $batch call:

--batch
Content-Type: multipart/mixed; boundary=changeset
--changeset
Content-Type: application/http
Content-Transfer-Encoding: binary

PUT /MarketPartner/468456462 HTTP/1.1
Content-Type: application/json
Content-ID: 1
Content-Length: 100
{
"mpID":"468456462",
"comments":"comment#1"
}

--changeset
Content-Type: application/http
Content-Transfer-Encoding: binary

POST /MarketPartner HTTP/1.1
Content-Type: application/json
Content-ID: 2
Content-Length: 100
{
"mpID":"468456457"
}
--changeset--
--batch--

I have provided my implementation of server.ts which is like this:

import { createCombinedHandler } from 'cds-routing-handlers';
class Server{
  public static async run() {
    const app = express();
    const hdl = createCombinedHandler({
      handler: [
        `${__dirname}/entities/**/*.js`,
        `${__dirname}/functions/**/*.js`,
      ],
    });
    const port = process.env.PORT || 4004;
    app.listen(port, async () => {
      console.info(`Server is listing at http://localhost:${port}`);
    });
    await cds.connect('db');
    // Start cds server
    await cds
      .serve('all')
      .at('odata')
      .in(app)
      .with((srv) => hdl(srv));
  }
}

export { Server as default };
Server.run();

I have provided my additional handlers(BeforeUpdate(),OnUpdate() etc) in the entities folder. The server with individual requests work correctly.
I am facing issue when doing the batch calls in which one of the call fails. From the above mentioned example $batch call, if the 2nd request fails and the 1st request is successful, in the response, I am getting 200 OK for the overall request, and in the response body, I get the success of the 1st request and the failure of the 2nd request individually. Also the data in the DB is updated for the 1st request. We want that if in the same changeset, if any request fails, all the requests in that changeset should fail(both from UI response and DB update)

Are there any handlers/implementation hooks where i can change the behavior and write custom implementation of how $batch calls are handled? Any suggestions are welcome.
Thanks in advance.

PS: I am new to CAP & typescript and have referred typescript implementation mostly from Bookshop Example Typescript