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

View File

@@ -20,7 +20,14 @@ Content-Type: application/json
"supplier_ID": "PNG" "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'); const cds = require('@sap/cds');
module.exports = cds.service.impl(async function (srv) { module.exports = cds.service.impl(async function (srv) {
const messaging = await cds.connect.to('messaging')
const { A_BusinessPartner } = this.entities; const { A_BusinessPartner } = this.entities;
srv.after('UPDATE', A_BusinessPartner, data => { srv.after('UPDATE', A_BusinessPartner, data => {
console.log(`>>> BusinessPartner updated ${data.BusinessPartner}`); 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 { extend projection CatalogService.ListOfBooks with {
supplier 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 S4bupa = await cds.connect.to('API_BUSINESS_PARTNER') //> external S4 service
const admin = await cds.connect.to('AdminService') //> local domain service const admin = await cds.connect.to('AdminService') //> local domain service
const db = await cds.connect.to('db') //> our primary database const db = await cds.connect.to('db') //> our primary database
const messaging = await cds.connect.to('messaging');
// Reflect CDS definition of the Suppliers entity // Reflect CDS definition of the Suppliers entity
const Suppliers = db.entities["sap.capire.bookshop.Suppliers"]; 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 // 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 // REVISIT: cds context is still from the UPDATE method when running in same programm, but should
// be a separate // 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 )); await new Promise( resolve => setTimeout( resolve, 1000 ));
const tx = cds.db.tx(msg); const tx = cds.db.tx(msg);
let replicas = await tx.run(SELECT('ID').from (Suppliers) .where ('ID in', msg.data.businessPartners)); 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 // one server: returns AdminService.Suppliers
// two servers: returns A_BusinessPartner // two servers: returns API_BUSINESS_PARTNER.A_BusinessPartner
const tx = S4bupa.tx({}); const tx = S4bupa.tx({});
let result = await tx.run(SELECT('*').from ('AdminService.Suppliers') .where ('ID =', 'ACME')); let result = await tx.run(SELECT('*').from ('AdminService.Suppliers') .where ('ID =', 'ACME'));
tx.commit(); tx.commit();
console.log(result); console.log(result);
} }
// TODO: remove test code
{ {
// one server: returns AdminSuppliers // one server: returns AdminService.Suppliers
// two servers: returns AdminSuppliers // two servers: returns AdminService.Suppliers
const tx = db.tx({}); const tx = db.tx({});
let result = await db.run(SELECT('*').from ('AdminService.Suppliers') .where ('ID =', 'ACME')); let result = await db.run(SELECT('*').from ('AdminService.Suppliers') .where ('ID =', 'ACME'));
tx.commit(); tx.commit();
console.log(result); console.log(result);
} }
//process.exit(0);
} }