Implement eventing

This commit is contained in:
Uwe Klinger
2021-05-05 08:07:48 +02:00
parent 8601bd8a46
commit 278258c436
5 changed files with 26 additions and 12 deletions

View File

@@ -1,5 +1,7 @@
const deploy = require("@sap/cds/lib/deploy");
// TODO: https://github.wdf.sap.corp/cdx/cds/pull/1949
const DEBUG = (...args) => console.log(...args);
deploy.exclude_external_entities_in = function (csn, _bound) {
@@ -12,8 +14,6 @@ deploy.exclude_external_entities_in = function (csn, _bound) {
DEBUG && DEBUG("excluding external entities for", service, "...");
const prefix = service + ".";
for (let each in csn.definitions) {
const def = csn.definitions[each];
if (def["@cds.persistence.table"] === true) continue;
if (each.startsWith(prefix)) {
DEBUG && DEBUG("excluding external entity", each);
_exclude(each);

View File

@@ -20,7 +20,14 @@ Content-Type: application/json
"supplier_ID": "PNG"
}
###
PATCH {{bpServer}}/api-business-partner/A_BusinessPartner('PNG')
Content-Type: application/json
{
"BusinessPartnerFullName": "Penguin Books (Changed)"
}
###

View File

@@ -1,11 +1,10 @@
const cds = require('@sap/cds');
module.exports = cds.service.impl(async function (srv) {
const messaging = await cds.connect.to('messaging')
const { A_BusinessPartner } = this.entities;
srv.after('UPDATE', A_BusinessPartner, data => {
console.log(`>>> BusinessPartner updated ${data.BusinessPartner}`);
messaging.emit("BusinessPartners/Changed", { businessPartners: [ data.BusinessPartner ] });
srv.emit("A_BusinessPartner.Changed", { businessPartners: [ data.BusinessPartner ] });
});
});

View File

@@ -66,3 +66,11 @@ extend service AdminService with {
extend projection CatalogService.ListOfBooks with {
supplier
}
// Extend S4 service with not modelled event
extend service S4 {
@topic: 'BusinessPartners/Changed'
event A_BusinessPartner.Changed {
BusinessPartners: array of S4.A_BusinessPartner:BusinessPartner;
}
}

View File

@@ -10,7 +10,6 @@ module.exports = async()=>{ // called by server.js
const S4bupa = await cds.connect.to('API_BUSINESS_PARTNER') //> external S4 service
const admin = await cds.connect.to('AdminService') //> local domain service
const db = await cds.connect.to('db') //> our primary database
const messaging = await cds.connect.to('messaging');
// Reflect CDS definition of the Suppliers entity
const Suppliers = db.entities["sap.capire.bookshop.Suppliers"];
@@ -41,7 +40,8 @@ module.exports = async()=>{ // called by server.js
// Subscribe to changes in the S4 origin of Suppliers data
// REVISIT: cds context is still from the UPDATE method when running in same programm, but should
// be a separate
messaging.on ('BusinessPartners/Changed', async msg => { //> would be great if we had batch events from S/4
// https://github.wdf.sap.corp/cap/matters/projects/44#card-196556
S4bupa.on ('A_BusinessPartner.Changed', async msg => { //> would be great if we had batch events from S/4
await new Promise( resolve => setTimeout( resolve, 1000 ));
const tx = cds.db.tx(msg);
let replicas = await tx.run(SELECT('ID').from (Suppliers) .where ('ID in', msg.data.businessPartners));
@@ -81,24 +81,24 @@ module.exports = async()=>{ // called by server.js
}
// TODO: remove test code
{
// one server: returns AdminSuppliers
// two servers: returns A_BusinessPartner
// one server: returns AdminService.Suppliers
// two servers: returns API_BUSINESS_PARTNER.A_BusinessPartner
const tx = S4bupa.tx({});
let result = await tx.run(SELECT('*').from ('AdminService.Suppliers') .where ('ID =', 'ACME'));
tx.commit();
console.log(result);
}
// TODO: remove test code
{
// one server: returns AdminSuppliers
// two servers: returns AdminSuppliers
// one server: returns AdminService.Suppliers
// two servers: returns AdminService.Suppliers
const tx = db.tx({});
let result = await db.run(SELECT('*').from ('AdminService.Suppliers') .where ('ID =', 'ACME'));
tx.commit();
console.log(result);
}
//process.exit(0);
}