cancel
Showing results for 
Search instead for 
Did you mean: 

CAP Handler for all entities at once

cabraldaniel
Explorer
0 Kudos
118

Hi All!

I'm doing an Experiment for an 'audit log table' for CAP. My objective is log all create/update/delete transactions, something like this:

this.before(['CREATE', 'UPDATE', 'DELETE'], 'Users', async (req) => {
const { ID, firstName, lastName, Country, favouriteFood } = req.data;

// Capture change details
const changeDetails = {
firstName, lastName, Country, favouriteFood
};

const db = await cds.connect.to('db')

// Create an audit log entry
await cds.run(INSERT.into("pocaudit.AuditLogs").entries({
ID: cds.utils.uuid(),
entityName: 'Users',
entityID: ID,
action: req.event.toUpperCase(),
timestamp: new Date().toISOString(),
changedBy: req.user.id,
changes: JSON.stringify(changeDetails)
}));
});

But this event requires I mention the entity. Do you know any way to capture ALL entities so I can make only one handler?

Thanks!!

P.S.: I'm aware of Audit Log Service, but my context requires the audit to stay on a table.

View Entire Topic
gregorw
Active Contributor
0 Kudos

Hi Daniel,

you can achieve the requested functionality by using *. So adjust your before event registration to:

this.before(['CREATE', 'UPDATE', 'DELETE'], '*', async (req)