<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>Question Re: Interact programmatically with SAP CAP framework's No... in Technology Q&amp;A</title>
    <link>https://community.sap.com/t5/technology-q-a/interact-programmatically-with-sap-cap-framework-s-node-js-apis-without/qaa-p/13853256#M4881749</link>
    <description>Great that it helped. Please vote for the influencing request I've created for this issue: &lt;A href="https://influence.sap.com/sap/ino/#/idea/330208" target="_blank"&gt;https://influence.sap.com/sap/ino/#/idea/330208&lt;/A&gt;</description>
    <pubDate>Tue, 03 Sep 2024 15:43:49 GMT</pubDate>
    <dc:creator>gregorw</dc:creator>
    <dc:date>2024-09-03T15:43:49Z</dc:date>
    <item>
      <title>Interact programmatically with SAP CAP framework's Node.js APIs without starting a server</title>
      <link>https://community.sap.com/t5/technology-q-a/interact-programmatically-with-sap-cap-framework-s-node-js-apis-without/qaq-p/13812289</link>
      <description>&lt;P&gt;Dear experts,&lt;/P&gt;&lt;P&gt;is this the correct approach to interact programmatically with the SAP CAP framework without starting a server?&lt;/P&gt;&lt;P&gt;I am working on a script to interact with the SAP Cloud Application Programming (CAP) framework programmatically without starting a server. Below is the code snippet I am using. Please note that the script is in a subfolder of my CAP project.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;const path = require('path')

// 1. Use the node process environment to specify the CDS profile to be used
//    or default to 'hybrid-dev'
process.env.CDS_ENV = process.env.CDS_ENV || 'hybrid-dev'
// 2. Require the CDS module which also initializes the whole CDS environment
const cds = require('@sap/cds')
// 3. Safely change `cds.root`, that is used by all CAP runtime file access as the root directory
//    see https://cap.cloud.sap/docs/node.js/cds-test#test-in-folder
cds.test.in(path.resolve(__dirname, '..'))

// 4. Verify the CDS environment
console.log(cds.root)
console.log(cds.env.profiles)
console.log(cds.env.requires.db)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My goal is to interact with the CAP framework for tasks such as database operations and service interactions without the need to start an HTTP server. Is this the recommended way to achieve this? Are there any best practices or potential pitfalls I should be aware of when using this approach?&lt;/P&gt;&lt;P&gt;Unfortunately I couldn't find anything related to my requirement in the documentation.&lt;/P&gt;&lt;P&gt;Thank you for your insights!&lt;/P&gt;&lt;P&gt;Cheers,&lt;BR /&gt;Michael&lt;/P&gt;</description>
      <pubDate>Fri, 30 Aug 2024 12:01:57 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/interact-programmatically-with-sap-cap-framework-s-node-js-apis-without/qaq-p/13812289</guid>
      <dc:creator>mlu2</dc:creator>
      <dc:date>2024-08-30T12:01:57Z</dc:date>
    </item>
    <item>
      <title>Re: Interact programmatically with SAP CAP framework's No...</title>
      <link>https://community.sap.com/t5/technology-q-a/interact-programmatically-with-sap-cap-framework-s-node-js-apis-without/qaa-p/13851425#M4881517</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;please check out the discussion&amp;nbsp;&lt;A href="https://community.sap.com/t5/technology-q-a/start-cap-without-express-to-run-a-cloud-foundry-task/qaq-p/13593040" target="_self"&gt;Start CAP without express to run a Cloud Foundry task&lt;/A&gt;&amp;nbsp;that I've posted in February. I came up with this solution:&amp;nbsp;&lt;A href="https://github.com/gregorwolf/sap-support-message-reporting/blob/main/srv/cap-startup.js" target="_blank" rel="noopener"&gt;srv/cap-startup.js&lt;/A&gt;. I wanted to avoid to use cds.test.&lt;/P&gt;&lt;P&gt;Best Regards&lt;BR /&gt;Gregor&lt;/P&gt;</description>
      <pubDate>Mon, 02 Sep 2024 11:04:26 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/interact-programmatically-with-sap-cap-framework-s-node-js-apis-without/qaa-p/13851425#M4881517</guid>
      <dc:creator>gregorw</dc:creator>
      <dc:date>2024-09-02T11:04:26Z</dc:date>
    </item>
    <item>
      <title>Re: Interact programmatically with SAP CAP framework's No...</title>
      <link>https://community.sap.com/t5/technology-q-a/interact-programmatically-with-sap-cap-framework-s-node-js-apis-without/qaa-p/13853035#M4881708</link>
      <description>&lt;P&gt;Thanks for your suggestion Gregor! It looked promising and I tried it, but unfortunately it didn't work for my use-case. Mine differs to yours in the following ways:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;I want to run the script interact-with-cap.js from a subfolder of my project, but I would like to use the settings from the CAP project in the root folder.&lt;/LI&gt;&lt;LI&gt;My settings include connections to bound cloud services, like a HANA DB. All the setups I tried so far, didn't resolve the bound services correctly.&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;I wanted to run my script in a subfolder with a given configuration profile:&lt;/P&gt;&lt;PRE&gt;&lt;SPAN class=""&gt;cd&lt;/SPAN&gt; migration
node interact-with-cap.js --profile my-profile&lt;/PRE&gt;&lt;P&gt;My project layout looks like this:&lt;/P&gt;&lt;PRE&gt;&amp;lt;project root&amp;gt;
├── app
├── db
├── migration
│&amp;nbsp;&amp;nbsp; └── interact-with-cap.js
└── srv&lt;/PRE&gt;&lt;P&gt;Currently I'm stuck and not sure if it makes sense to proceed with my approach. It seems to me the CAP startup process is rather entangled and not easy to get right without deep knowledge about inner workings and reading the framework's sourcecode. Especially when reading comments like these in several places:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;cds.env = cds.env.for('cds') // reload env // FIXME: This is too early!&lt;/LI&gt;&lt;LI&gt;const DEBUG = cds.debug('cli') // REVISIT: This loads cds.env too early!&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;My hope was that I could do something like const myenv = cds.init(__dirname + '../', { profile: 'my-profile' }) and interact with CAP like a configured library. But I looks like spinning up the whole server is the only official entry point to the framework.&lt;/P&gt;</description>
      <pubDate>Tue, 03 Sep 2024 13:50:51 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/interact-programmatically-with-sap-cap-framework-s-node-js-apis-without/qaa-p/13853035#M4881708</guid>
      <dc:creator>mlu2</dc:creator>
      <dc:date>2024-09-03T13:50:51Z</dc:date>
    </item>
    <item>
      <title>Re: Interact programmatically with SAP CAP framework's No...</title>
      <link>https://community.sap.com/t5/technology-q-a/interact-programmatically-with-sap-cap-framework-s-node-js-apis-without/qaa-p/13853075#M4881715</link>
      <description>Please check &lt;A href="https://cap.cloud.sap/docs/advanced/hybrid-testing#cds-bind-exec" target="_blank"&gt;https://cap.cloud.sap/docs/advanced/hybrid-testing#cds-bind-exec&lt;/A&gt;</description>
      <pubDate>Tue, 03 Sep 2024 13:37:58 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/interact-programmatically-with-sap-cap-framework-s-node-js-apis-without/qaa-p/13853075#M4881715</guid>
      <dc:creator>gregorw</dc:creator>
      <dc:date>2024-09-03T13:37:58Z</dc:date>
    </item>
    <item>
      <title>Re: Interact programmatically with SAP CAP framework's No...</title>
      <link>https://community.sap.com/t5/technology-q-a/interact-programmatically-with-sap-cap-framework-s-node-js-apis-without/qaa-p/13853233#M4881744</link>
      <description>&lt;P&gt;The cds bind command was the missing piece! Thanks Gregor!&lt;/P&gt;&lt;P&gt;Using it, profile selection and binding resolution don't have to be part of my script logic anymore. Only had to change how I call my script, not from within my subfolder anymore, but from my CAP modules' root folder.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;PRE&gt;&lt;SPAN class=""&gt;# Execute in CAP module root (where your package.json is)&lt;/SPAN&gt;
cds &lt;SPAN class=""&gt;bind&lt;/SPAN&gt; --profile hybrid-dev --&lt;SPAN class=""&gt;exec&lt;/SPAN&gt; -- node migration/cap-startup-test2.js&lt;/PRE&gt;&lt;LI-CODE lang="javascript"&gt;//file: migration/cap-startup-test2.js
const cds = require('@sap/cds')

async function capStartup() {
const app = cds.app
cds.emit('bootstrap', app)

// load model from all sources
const csn = await cds.load('*').then(cds.minify)
cds.model = cds.compile.for.nodejs(csn)
cds.emit('loaded', cds.model)

// connect to prominent required services
if (cds.requires.db) cds.db = await cds.connect.to('db')
if (cds.requires.messaging) await cds.connect.to('messaging')

// serve own services as declared in model
cds.serve('all').from(csn).in(app)
await cds.emit('served', cds.services)
}

async function main() {
// Startup CAP Framework
await capStartup()

// Do your thing
const { MyEntity } = cds.db.entities('my.namespace')
console.log(await SELECT.from(MyEntity).columns('count(ID) as cnt'))

// Shutdown CAP Framework
cds.exit()
}

main()&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 03 Sep 2024 15:36:14 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/interact-programmatically-with-sap-cap-framework-s-node-js-apis-without/qaa-p/13853233#M4881744</guid>
      <dc:creator>mlu2</dc:creator>
      <dc:date>2024-09-03T15:36:14Z</dc:date>
    </item>
    <item>
      <title>Re: Interact programmatically with SAP CAP framework's No...</title>
      <link>https://community.sap.com/t5/technology-q-a/interact-programmatically-with-sap-cap-framework-s-node-js-apis-without/qaa-p/13853256#M4881749</link>
      <description>Great that it helped. Please vote for the influencing request I've created for this issue: &lt;A href="https://influence.sap.com/sap/ino/#/idea/330208" target="_blank"&gt;https://influence.sap.com/sap/ino/#/idea/330208&lt;/A&gt;</description>
      <pubDate>Tue, 03 Sep 2024 15:43:49 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/interact-programmatically-with-sap-cap-framework-s-node-js-apis-without/qaa-p/13853256#M4881749</guid>
      <dc:creator>gregorw</dc:creator>
      <dc:date>2024-09-03T15:43:49Z</dc:date>
    </item>
    <item>
      <title>Re: Interact programmatically with SAP CAP framework's No...</title>
      <link>https://community.sap.com/t5/technology-q-a/interact-programmatically-with-sap-cap-framework-s-node-js-apis-without/qaa-p/13863887#M4883317</link>
      <description>Hi, great solution! I added this to the module and it works: command: npm i -g &lt;a href="https://community.sap.com/t5/user/viewprofilepage/user-id/1715738"&gt;@ashima_g&lt;/a&gt;/cds-dk &amp;amp;&amp;amp; npx cds bind --exec -- node script Does anyone have any idea how I can still receive the shutdown event?</description>
      <pubDate>Thu, 12 Sep 2024 13:02:14 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/interact-programmatically-with-sap-cap-framework-s-node-js-apis-without/qaa-p/13863887#M4883317</guid>
      <dc:creator>akuller</dc:creator>
      <dc:date>2024-09-12T13:02:14Z</dc:date>
    </item>
    <item>
      <title>Re: Interact programmatically with SAP CAP framework's No...</title>
      <link>https://community.sap.com/t5/technology-q-a/interact-programmatically-with-sap-cap-framework-s-node-js-apis-without/qaa-p/13914988#M4889238</link>
      <description>&lt;a href="https://community.sap.com/t5/user/viewprofilepage/user-id/14806"&gt;@akuller&lt;/a&gt;: I have `cds.exit()` in my script to shutdown the CDS framework.</description>
      <pubDate>Tue, 22 Oct 2024 06:22:45 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/interact-programmatically-with-sap-cap-framework-s-node-js-apis-without/qaa-p/13914988#M4889238</guid>
      <dc:creator>mlu2</dc:creator>
      <dc:date>2024-10-22T06:22:45Z</dc:date>
    </item>
  </channel>
</rss>

