demo -> done

This commit is contained in:
Daniel
2021-02-16 18:43:11 +01:00
committed by Daniel Hutzel
parent a037d92c97
commit 77de0e445e
18 changed files with 131 additions and 29 deletions

View File

@@ -1,11 +0,0 @@
const express = require('express')
const cds = require('@sap/cds')
cds.once('bootstrap',(app)=>{
const {dirname} = require('path')
// serving the vue.js app imported from @capire/bookshop
const vue_app = dirname (require.resolve('@capire/bookshop/app/vue/index.html'))
app.use ('/vue', express.static(vue_app))
})
module.exports = cds.server

View File

View File

@@ -1,17 +0,0 @@
using { sap.capire, sap.capire.bookshop.Books } from '@capire/bookshop';
using { API_BUSINESS_PARTNER as external } from './external/API_BUSINESS_PARTNER.csn';
extend Books with {
supplier : Association to Suppliers;
}
extend service CatalogService with {
entity MySuppliers as projection on Suppliers;
}
@cds.persistence:{table,skip:false}
entity Suppliers as projection on external.A_BusinessPartner {
BusinessPartner as ID,
LastName,
AcademicTitle
}

View File

View File

@@ -7,6 +7,9 @@
"private": true,
"dependencies": {
"@capire/bookshop": "^1.0.0",
"@capire/common": "^1.0.0",
"@capire/orders": "^1.0.0",
"@capire/reviews": "^1.0.0",
"@sap/cds": "^4",
"express": "^4"
},
@@ -24,4 +27,4 @@
}
}
}
}
}

67
done/server.js Normal file
View File

@@ -0,0 +1,67 @@
const cds = require('@sap/cds')
module.exports = cds.server
cds.once('bootstrap',(app)=>{
app.use('/vue',_from('@capire/bookshop/app/vue'))
})
cds.once('served', async ()=>{
// Connect to services we want to mashup below...
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
// Reflect CDS definition of the Suppliers entity
const Suppliers = S4bupa.entities
// admin.prepend (()=>{
// Delegate Value Help reads for Suppliers to S4 backend
admin.on ('READ', 'Suppliers', req => {
console.log ('>> delegating to S4 service...')
return S4bupa.read(Suppliers) .where (req.query.where)
})
// Replicate Supplier data when Books are edited
admin.on (['CREATE','UPDATE'], 'Books', async req => {
const { supplier } = req.data
if (supplier) {
let cached = await db.read (Suppliers, supplier)
if (!cached) await replicate (supplier,'initial')
}
})
// })
// Subscribe to changes in the S4 origin of Suppliers data
S4bupa.on ('BusinessPartner/Changed', async msg => {
const ID = msg.businessPartner.KEYS
const cached = await db.read (Suppliers, sup => sup.ID) .where ({ ID })
for (let each of cached) replicate (each, 'update')
})
// Helper function to replicate Suppliers data
async function replicate (ID,_initial) {
let data = await S4bupa.read (Suppliers, ID)
if (_initial) return db.insert (data) .into (Suppliers)
else return db.update (Suppliers,ID) .with (data)
}
})
// -----------------------------------------------------------------------
// Helper for serving static content from npm-installed packages
const {static} = require('express')
const {dirname} = require('path')
const _from = target => static (dirname (require.resolve(`${target}/index.html`)))

View File

@@ -0,0 +1,5 @@
ID;name;city
ACME;A Company Making Everything;Massachusetts
B4U;Books for You;Philadelphia
S&C;Shakespeare & Co.;Paris
WSL;Waterstones;London
1 ID name city
2 ACME A Company Making Everything Massachusetts
3 B4U Books for You Philadelphia
4 S&C Shakespeare & Co. Paris
5 WSL Waterstones London

55
done/srv/services.cds Normal file
View File

@@ -0,0 +1,55 @@
/*
Optionally add projections to external entities, to capture what
you actually want to use from there.
*/
using { API_BUSINESS_PARTNER as S4 } from './external/API_BUSINESS_PARTNER.csn';
extend service S4 with {
entity Suppliers as projection on S4.A_BusinessPartner {
key BusinessPartner as ID,
BusinessPartnerFullName as name,
to_BusinessPartnerAddress.CityName as city
}
}
/*
You can mashup entities from external services, or projections
thereof, with your project's own entities
*/
using { sap.capire.bookshop.Books } from '@capire/bookshop';
extend Books with {
supplier : Association to S4.Suppliers;
}
/*
You can also expose external entities through your own services
For this to work, you need to delegate the respective calls
addressed to your services into calls to the external service.
*/
extend service AdminService with {
entity Suppliers as projection on S4.Suppliers;
}
/*
Optionally add a local persistence to keep replicas of external
entities to have data in fast access locally; much like a cache.
*/
annotate S4.Suppliers with @cds.persistence:{table,skip:false};
/**
Having locally cached replicas also allows us to display supplier
data in lists of books, which otherwise would generate unwanted
traffic on S4 backends.
*/
extend projection CatalogService.ListOfBooks with {
supplier
}
/*
The following using directives activate imported reuse packages.
*/
using from '@capire/common';
using from '@capire/orders';
using from '@capire/reviews';