Improvements for Supplier replication

This commit is contained in:
Uwe Klinger
2021-05-03 12:52:11 +02:00
parent de806a1e38
commit eb4bc703dd
6 changed files with 106 additions and 5 deletions

View File

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

View File

@@ -16,10 +16,22 @@ extend service S4 with {
// CityName as name
// }
// to_BusinessPartnerAddress
// REVISIT: following is not supported so far in cqn2odata...
// to_BusinessPartnerAddress.CityCode as city,
// to_BusinessPartnerAddress.CityName as city_name,
}
// REVISIT: Alternative idea to use a specific replication view, but request data from
// a different view and manual map values.
// entity ReplicatedSuppliers as projection on Suppliers {
// ID,
// name,
// to_BusinessPartnerAddress.CityCode as city,
// to_BusinessPartnerAddress.CityName as city_name
// }
}

View File

@@ -23,13 +23,21 @@ module.exports = async()=>{ // called by server.js
})
// Replicate Supplier data when edited Books have suppliers
admin.on (['CREATE','UPDATE'], 'Books', ({data:{supplier}}, next) => {
admin.on (['CREATE','UPDATE'], 'Books', async ({data:{supplier_ID: supplierId}}, next) => {
// Using Promise.all(...) to parallelize local write, i.e. next(), and replication
if (supplier) return Promise.all ([ next(), async()=>{
let replicated = await db.exists (Suppliers, supplier)
if (!replicated) await replicate (supplier, 'initial')
/*
// ERROR: Reference integrity is violated for association "supplier"
if (supplierId) return Promise.all ([ next(), async()=>{
let replicated = await db.exists (Suppliers, supplierId)
if (!replicated) await replicate (supplierId, 'initial')
}])
else return next() //> don't forget to pass down the interceptor stack
*/
let replicated = await db.exists (Suppliers, supplierId);
if (!replicated) await replicate (supplierId, 'initial');
return next();
})
})
@@ -47,7 +55,10 @@ module.exports = async()=>{ // called by server.js
*/
async function replicate (IDs,_initial) {
if (!Array.isArray(IDs)) IDs = [ IDs ]
let suppliers = await S4bupa.read (Suppliers).where('ID in',IDs)
// TODO: Doesn't work when running in same process with mocked API_BUSINESS_PARTNER
let suppliers = await S4bupa.read (Suppliers).where(...([[]].concat(IDs).reduce( (where, id, index ) => { where.push(`${index>1 ? "OR ":""}ID = `, id); return where })));
//let suppliers = await S4bupa.read (Suppliers).where('ID in',IDs)
if (_initial) return db.insert (suppliers) .into (Suppliers) //> using bulk insert
else return Promise.all(suppliers.map ( //> parallelizing updates
each => db.update (Suppliers,each.ID) .with (each)