https://cap.cloud.sap/docs/cds/common#aspect-cuid
https://cap.cloud.sap/docs/node.js/databases#insertresult-beta
const result = await INSERT(payload).into(table)
const newEntries = [...result]
console.log("New ID: " + newEntries[0].ID)
https://cap.cloud.sap/docs/cds/common#aspect-cuid
https://cap.cloud.sap/docs/node.js/databases#insertresult-beta
const result = await INSERT(payload).into(table)
const newEntries = [...result]
console.log("New ID: " + newEntries[0].ID)
const cds = require('@sap/cds');
module.exports = async srv => {
const { Objects } = srv.entities // entities from myService.cds
srv.on("myAction", async req => {
const query = SELECT.one.from(Objects).where({ id: req.data.myId })
const srv = await cds.connect.to('myService')
const data = await srv.run(query)
console.log(data)
return data
})
srv.on("READ", Objects, async req => {
console.log("Objects called")
// Select data from db or forward query to external system
// ...
// return data
})
}
# check if ssh is enabled
cf ssh-enabled myapp
# if it's not, enable it and restart app
cf enable-ssh myapp
cf restart myapp
# access with
cf ssh myapp
Create your script file, make it executeable and add it to your .gitignore as it contains sensitive information:
touch login.sh
chmod +x login.sh
echo login.sh >> .gitignore
Open the file and paste the following:
#! /bin/bash
cf login <<!
myemail@mail.com
mypassword
1
!
With “1” you select your target space. Save your script and run it using:
./login.sh
After some time, it can happen that the default identity provider of the SAP BTP (SAP ID service) is asking for a password change. I don’t know exactly, but it seems to be every 90 days?!
The login process will fail with the following output:
$ ./scripts/login.sh
API endpoint: https://api.cf.eu10.hana.ondemand.com
Email: myemail@mail.com
Password:
Authenticating...
{"error":"invalid_grant","error_description":"User authentication failed: PASSWORD_CHANGE_REQUIRED"}
To change your password, just go to https://account.sap.com or https://accounts.sap.com/, and it should directly open the password change screen.
Update 06.09.2024: The login can now also be done by completely using the cf command.
cf login -a https://api.cf.eu10.hana.ondemand.com -o myOrg -s mySpace -u myEmail@mail.com -p myPassword
Since CDS 5.9.2 this is the quickest way of creating and deploying the bookshop sample:
cds init bookshop
cd bookshop
cds add samples
cds add hana
cds add xsuaa
cds add mta
npm install
mbt build
cf deploy mta_archives/bookshop_1.0.0.mtar
https://sap.github.io/cloud-sdk/docs/js/features/connectivity/destination
const { getDestination } = require("@sap-cloud-sdk/connectivity")
const myDestination= await getDestination("myDestination")
if (myDestination === null) throw Error(`Destination "myDestination" not found`)
for (let key in myDestination) {
console.log(key, myDestination[key])
}
If the default port 4004 is already open and you want to see what is bound to it, select View -> Find Command -> Ports Preview
Solution to kill another “watch.js” process using/blocking the port: https://answers.sap.com/questions/13016130/sap-business-application-studio-stop-running-serve.html
To archive the same in a single command use:
lwctl -c basic-tools kill -9 $(lwctl -c basic-tools ps aux | grep watch.js | awk '{print $2}')
As alternative, change the default port by adding a new port in package.json to the start script, for example: “start”: “cds run –port 4003” and use npm run start instead of cds watch.
Update 20.02.2023: Just had the problem again due to a VPN disconnect. But this time I had an application running using cds run
. Therefore, I had to change the command from watch.js
to cds.js
:
lwctl -c basic-tools kill -9 $(lwctl -c basic-tools ps aux | grep cds.js | awk '{print $2}')
Update 10.05.2023: A better approach seems to be killing the node process. This should work in both situation.
lwctl -c basic-tools kill -9 $(lwctl -c basic-tools ps aux | grep node | awk '{print $2}')
Update 29.02.2024: With the BAS migration to Code – OSS the previous commands were not working anymore, but this new command seems to work:
kill -9 $(ps aux | grep cds.js | awk '{print $2}')
# or
kill -9 $(ps aux | grep cds-dk | awk '{print $2}')
const sfsfSrv = await cds.connect.to('sfsf')
// Option 1: Query Notation
const response = await sfsfSrv.run(SELECT`teamMembersSize`.from`User`.where`userId = ${req.user.id}`)
console.log("option 1: " + response[0].teamMembersSize)
// Option 2: HTTP method-style
const response2 = await sfsfSrv.get(`/User('${req.user.id}')/teamMembersSize/$value`)
console.log("option 2: " + response2)
An easy way to test your approuter is using the user-api-service. For that add the following route to your xs-app.json
{
"source": "^/user-api(.*)",
"target": "$1",
"service": "sap-approuter-userapi"
}
And after deployment, open the application router URL and add /user-api/currentUser
to it. You should see your Email and other User details. This is testing that the application router is actually getting the security token from the UAA instance.
These three entries in a .env file
cds.requires.ECEmploymentInformation.[development].credentials.authentication=BasicAuthentication
cds.requires.ECEmploymentInformation.[development].credentials.username=myUsername
cds.requires.ECEmploymentInformation.[development].credentials.password=myPassword
are equal to line 12, 13, and 14 in this package.json snippet:
{
"cds": {
"ECEmploymentInformation": {
"kind": "odata-v2",
"model": "srv/external/ECEmploymentInformation",
"credentials": {
"[production]": {
"destination": "sfsf"
},
"[development]": {
"url": "https://apisalesdemo2.successfactors.eu/odata/v2",
"authentication": "BasicAuthentication",
"username": "myUsername",
"password": "myPassword",
}
}
}
}
}