<?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: CAP - Problem with dynamic entity generation and content population in Technology Q&amp;A</title>
    <link>https://community.sap.com/t5/technology-q-a/cap-problem-with-dynamic-entity-generation-and-content-population/qaa-p/12428646#M4653423</link>
    <description>&lt;P&gt;Hi &lt;SPAN class="mention-scrubbed"&gt;martin.stenzig3&lt;/SPAN&gt; &lt;/P&gt;&lt;P&gt;Entity kind of remains undefined for me.&lt;/P&gt;&lt;P&gt;Not sure if it deploys and while doing a read at the entity it gives me an error invalid table name: Could not find table/view V_TEST in schema CAC93ACB4A8A470AA92DEF91AE7BF8EC.&lt;/P&gt;
 const db = await 
&lt;A rel="nofollow" href="http://cds.connect.to" target="_blank"&gt;cds.connect.to("db");&lt;/A&gt;&lt;BR /&gt;&lt;P&gt;let myNewCsn = cds.compile(cdlString) &lt;/P&gt;&lt;P&gt;await cds.deploy(myNewCsn).to(db) &lt;/P&gt;&lt;P&gt;console.log('NewCDL: ', JSON.stringify(myNewCsn)) &lt;/P&gt;&lt;P&gt;let entity = myNewCsn.definitions[entityName] &lt;/P&gt;&lt;P&gt;cds.model.definitions[entityName] = entity; &lt;/P&gt;&lt;P&gt;let t = await db.read(entity) 
&lt;/P&gt;&lt;STRONG&gt;&lt;BR /&gt;console.log('Entity (user) is:', entity)&lt;BR /&gt;&lt;/STRONG&gt; 
&lt;STRONG&gt;Entity (user) is: entity {&lt;BR /&gt;&lt;/STRONG&gt; 
&lt;STRONG&gt; kind: 'entity',&lt;/STRONG&gt;&lt;STRONG&gt;&lt;BR /&gt;'@cds.persistence.exists': true,&lt;/STRONG&gt;&lt;STRONG&gt;&lt;BR /&gt;elements: [Object: null prototype]&lt;/STRONG&gt;&lt;STRONG&gt;&lt;BR /&gt; { CODE: string { '@title': 'CODE: CODE', key: true, type: 'cds.String', length: 3 },&lt;/STRONG&gt;&lt;STRONG&gt;&lt;BR /&gt; TEXT: string { '@title': 'TEXT: TEXT', key: true, type: 'cds.String', length: 10 }&lt;/STRONG&gt;&lt;STRONG&gt;&lt;BR /&gt;} }&lt;/STRONG&gt; 
&lt;BR /&gt; &lt;P&gt;Also how this statement mentioned in your previous comment helps?&lt;/P&gt; 
&lt;PRE&gt;&lt;CODE&gt;cds.model.definitions[entityName] = myNewCsn[entityName]&lt;/CODE&gt;&lt;/PRE&gt;
 &lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;Kanika&lt;/P&gt;</description>
    <pubDate>Fri, 22 Jul 2022 10:12:02 GMT</pubDate>
    <dc:creator>KM11</dc:creator>
    <dc:date>2022-07-22T10:12:02Z</dc:date>
    <item>
      <title>CAP - Problem with dynamic entity generation and content population</title>
      <link>https://community.sap.com/t5/technology-q-a/cap-problem-with-dynamic-entity-generation-and-content-population/qaq-p/12428638</link>
      <description>&lt;P&gt;I am trying to dynamically create a cds entity in the SQLITE database and then populate it with values. &lt;/P&gt;
  &lt;UL&gt; 
   &lt;LI&gt;create an entity based on String and compile it into model - &lt;STRONG&gt;Works&lt;/STRONG&gt;&lt;/LI&gt; 
   &lt;LI&gt;create a table in SQL Lite - &lt;STRONG&gt;Also works, but really ugly at the moment&lt;/STRONG&gt;&lt;/LI&gt; 
   &lt;LI&gt;When I try to prepare the Insert with "INSERT.into(User,..." It's missing an object somewhere. (See code and error below&lt;/LI&gt; 
  &lt;/UL&gt;
  &lt;P&gt;Anybody any ideas. I am probably missing something simple, but I just don't see it. &lt;/P&gt;
  &lt;P&gt;I would also appreciate if anybody has a better way of creating the entities in SQLlite.&lt;/P&gt; 
  &lt;PRE&gt;&lt;CODE&gt;  // Establish connection to DB
      let dbSqlLite = await cds.connect('db');

      // Create a new entity
      let userEntity = `
      namespace riz.inno;
      entity User {
        userId: String(100);
        payGrade: LargeString;
        dateOfBirth :Date;
        promotionAmount: Decimal(10,2);
      }`

      // Compile model
      let cdsModel = cds.compile(userEntity)

      // --- There might be an easier way to persist the cds model in the sqllite db, but it's not transparent to me at this point
      // Export Model to SQL String
      let table2Create = await cds.compile(cdsModel).to.sql({ dialect: 'sqlite', as: 'str' })

      // Split string into table specific statements
      let myTables = table2Create.split(';')

      // Loop through each table and execute the create table statement 
      
      for (let z=0; z &amp;lt; myTables.length; z++) {
         let singleTableCreate = myTables[z]
       
        // Trim the whitespace
        singleTableCreate = singleTableCreate.trim()

        // Check if create exists
        if (singleTableCreate.length &amp;gt; 0) {

          // Concatenate the table create with semicolon
          singleTableCreate += ';'
          // Run the create table
          let dbResult = await dbSqlLite.run(singleTableCreate)
          return dbResult;
        }
        else {
          return 0;
        }

      }

      // --- Just check to see if table was created successfully
      let sqlliteAllTables = "SELECT  name FROM  sqlite_master WHERE type ='table' AND name NOT LIKE 'sqlite_%'";
      let dbReturnSelectAll = await dbSqlLite.run(sqlliteAllTables)
      console.log('List of SQL Server tables: ', dbReturnSelectAll)

      // determine custom meta data 
      let {User} = cdsModel.entities

      // Begin transaction
      let tx = dbSqlLite.tx(req)

      let myInsert = INSERT.into(User, [
        { userId: 'test', payGrade: 'Wuthering Heights', promotionAmount: 12.12 }
      ])

      let test = await tx.run(myInsert)
      console.log("Db insert Result ", test)

      let commitResponse = await tx.commit(req);
      console.log("Commit Response: ", commitResponse)
&lt;/CODE&gt;&lt;/PRE&gt;
  &lt;P&gt;when I run this inside of my handler, I receive the following error&lt;/P&gt; 
  &lt;PRE&gt;&lt;CODE&gt;[cds] - TypeError: Cannot read property 'elements' of undefined
    at SQLiteDatabase._handler [as _input] (C:\Users\MartinStenzig\AppData\Roaming\npm\node_modules\@sap\cds-dk\node_modules\@sap\cds\libx\_runtime\db\generic\input.js:189:16)
    at SQLiteDatabase.dispatch (C:\Users\MartinStenzig\AppData\Roaming\npm\node_modules\@sap\cds-dk\node_modules\@sap\cds\lib\serve\Service-dispatch.js:38:53)
    at async AdminService.&amp;lt;anonymous&amp;gt; (C:\_projects\capLinkedExample\srv\handlers\admin.js:73:18)
    at async next (C:\Users\MartinStenzig\AppData\Roaming\npm\node_modules\@sap\cds-dk\node_modules\@sap\cds\lib\serve\Service-dispatch.js:55:17)
    at async AdminService.dispatch (C:\Users\MartinStenzig\AppData\Roaming\npm\node_modules\@sap\cds-dk\node_modules\@sap\cds\lib\serve\Service-dispatch.js:53:10)
    at async _invokeFunction (C:\Users\MartinStenzig\AppData\Roaming\npm\node_modules\@sap\cds-dk\node_modules\@sap\cds\libx\_runtime\cds-services\adapter\odata-v4\handlers\read.js:44:18)
    at async C:\Users\MartinStenzig\AppData\Roaming\npm\node_modules\@sap\cds-dk\node_modules\@sap\cds\libx\_runtime\cds-services\adapter\odata-v4\handlers\read.js:441:16 {
  id: '1105573',
  level: 'ERROR',
  timestamp: 1632329111256
}
[INTERNAL ERROR] TypeError: Cannot read property 'elements' of undefined
    at SQLiteDatabase._handler [as _input] (C:\Users\MartinStenzig\AppData\Roaming\npm\node_modules\@sap\cds-dk\node_modules\@sap\cds\libx\_runtime\db\generic\input.js:189:16)
    at SQLiteDatabase.dispatch (C:\Users\MartinStenzig\AppData\Roaming\npm\node_modules\@sap\cds-dk\node_modules\@sap\cds\lib\serve\Service-dispatch.js:38:53)
    at async AdminService.&amp;lt;anonymous&amp;gt; (C:\_projects\capLinkedExample\srv\handlers\admin.js:73:18)
    at async next (C:\Users\MartinStenzig\AppData\Roaming\npm\node_modules\@sap\cds-dk\node_modules\@sap\cds\lib\serve\Service-dispatch.js:55:17)
    at async AdminService.dispatch (C:\Users\MartinStenzig\AppData\Roaming\npm\node_modules\@sap\cds-dk\node_modules\@sap\cds\lib\serve\Service-dispatch.js:53:10)
    at async _invokeFunction (C:\Users\MartinStenzig\AppData\Roaming\npm\node_modules\@sap\cds-dk\node_modules\@sap\cds\libx\_runtime\cds-services\adapter\odata-v4\handlers\read.js:44:18)
    at async C:\Users\MartinStenzig\AppData\Roaming\npm\node_modules\@sap\cds-dk\node_modules\@sap\cds\libx\_runtime\cds-services\adapter\odata-v4\handlers\read.js:441:16

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 22 Sep 2021 16:58:05 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/cap-problem-with-dynamic-entity-generation-and-content-population/qaq-p/12428638</guid>
      <dc:creator>martinstenzig</dc:creator>
      <dc:date>2021-09-22T16:58:05Z</dc:date>
    </item>
    <item>
      <title>Re: CAP - Problem with dynamic entity generation and content population</title>
      <link>https://community.sap.com/t5/technology-q-a/cap-problem-with-dynamic-entity-generation-and-content-population/qaa-p/12428639#M4653416</link>
      <description>&lt;P&gt;Just reproduced that → the error seems to be a bug in  @sap/cds 5.4.&lt;/P&gt;&lt;P&gt;It does work with 5.5 which was released 3 days ago, with this important change:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;&lt;STRONG&gt;Never do a &lt;EM&gt;tx.commit()&lt;/EM&gt; &lt;/STRONG&gt;on a managed tx, i.e. one obtained through &lt;EM&gt;cds/srv.tx(req)&lt;/EM&gt;&lt;/LI&gt;&lt;LI&gt;I know that's not well documented → we're working on it; to be released next week&lt;/LI&gt;&lt;LI&gt;Good news: you don't need to call &lt;EM&gt;cds/srv.tx(req)&lt;/EM&gt; anymore ... actually your code could be reduced to: &lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;Create tables:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;let ddl = cds.compile(cdsModel).to.sql({ dialect: 'sqlite' })
await dbSqlite.run (ddl) //&amp;gt; runs all create statements&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Fill in data:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;await db.create(User, { 
  userId: 'test', payGrade: 'Wuthering Heights', promotionAmount: 12.12 
})&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 27 Sep 2021 15:19:24 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/cap-problem-with-dynamic-entity-generation-and-content-population/qaa-p/12428639#M4653416</guid>
      <dc:creator>Daniel7</dc:creator>
      <dc:date>2021-09-27T15:19:24Z</dc:date>
    </item>
    <item>
      <title>Re: CAP - Problem with dynamic entity generation and content population</title>
      <link>https://community.sap.com/t5/technology-q-a/cap-problem-with-dynamic-entity-generation-and-content-population/qaa-p/12428640#M4653417</link>
      <description>&lt;P&gt;Btw. you can also use &lt;EM&gt;cds.deploy()&lt;/EM&gt;. For example, try this in &lt;EM&gt;cds repl:&lt;/EM&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;let m = CDL`entity Foo { key ID:UUID; bar : Association to Bar; }; entity Bar { key ID:UUID; }`
let db = await cds.connect.to('sqlite::memory:')
await cds.deploy(m).to(db)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;&lt;CODE&gt;await db.read('sqlite_master')&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;&lt;CODE&gt;let [ bar ] = await db.create('Bar',{})
await db.create ('Foo',{ bar })
await db.read('Foo')&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 27 Sep 2021 15:34:49 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/cap-problem-with-dynamic-entity-generation-and-content-population/qaa-p/12428640#M4653417</guid>
      <dc:creator>Daniel7</dc:creator>
      <dc:date>2021-09-27T15:34:49Z</dc:date>
    </item>
    <item>
      <title>Re: CAP - Problem with dynamic entity generation and content population</title>
      <link>https://community.sap.com/t5/technology-q-a/cap-problem-with-dynamic-entity-generation-and-content-population/qaa-p/12428641#M4653418</link>
      <description>&lt;P&gt;Fantastic! I will keep my eyes open for the new documentation. &lt;/P&gt;</description>
      <pubDate>Tue, 28 Sep 2021 05:10:00 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/cap-problem-with-dynamic-entity-generation-and-content-population/qaa-p/12428641#M4653418</guid>
      <dc:creator>martinstenzig</dc:creator>
      <dc:date>2021-09-28T05:10:00Z</dc:date>
    </item>
    <item>
      <title>Re: CAP - Problem with dynamic entity generation and content population</title>
      <link>https://community.sap.com/t5/technology-q-a/cap-problem-with-dynamic-entity-generation-and-content-population/qaa-p/12428642#M4653419</link>
      <description>&lt;P&gt; &lt;SPAN class="mention-scrubbed"&gt;daniel.hutzel&lt;/SPAN&gt; , the deployment of the DB works great now, but I still have issues with the create. I tried the following:&lt;/P&gt; 
&lt;PRE&gt;&lt;CODE&gt;   this.on('tryDbAccess', async (req) =&amp;gt; {
      
      console.log('-- Start Test ---')

      let entityName = 'User'
    
      let cdlString = `entity ${entityName} {
        key userId : String(100);
        payGrade : LargeString;
        dateOfBirth : Date;
        promotionAmount : Double;
      };`
    
      let entityList = [
        {
          userId: 'abc1',
          payGrade: 'Test',
          promotionAmount: 2313.23
        },
        {
          userId: 'abc2',
          payGrade: 'Test 2',
          promotionAmount: 1213.43
        }
      ]
    
      let myNewCsn = cds.compile(cdlString)
    
      const db = await cds.connect.to('db')
    
      // Create tables based on CSN
      await cds.deploy(myNewCsn).to(db)
    
      console.log('NewCDL: ', JSON.stringify(myNewCsn))
    
      let entity = myNewCsn.definitions[entityName]
    
      console.log('Entity (user) is:', entity)
    
      await db.create(entity, entityList)
      let t = await db.read(entity)
      console.log('Content of user is', t)
    

    })
&amp;lt;br&amp;gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;and receive a "[cds] - TypeError: Cannot read property 'userId' of undefined". &lt;/P&gt;&lt;P&gt;What am I missing? &lt;/P&gt;</description>
      <pubDate>Thu, 07 Oct 2021 17:08:15 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/cap-problem-with-dynamic-entity-generation-and-content-population/qaa-p/12428642#M4653419</guid>
      <dc:creator>martinstenzig</dc:creator>
      <dc:date>2021-10-07T17:08:15Z</dc:date>
    </item>
    <item>
      <title>Re: CAP - Problem with dynamic entity generation and content population</title>
      <link>https://community.sap.com/t5/technology-q-a/cap-problem-with-dynamic-entity-generation-and-content-population/qaa-p/12428643#M4653420</link>
      <description>&lt;P&gt;Ok, I found a solution, but think there is probably a better way. At the moment I am adding the custom entity level cds to the global model by doing the following&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;cds.model.definitions[entityName] = myNewCsn[entityName]  
&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Is there a more 'proper' way of doing this or is this 'the" way? &lt;/P&gt;</description>
      <pubDate>Thu, 07 Oct 2021 22:18:15 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/cap-problem-with-dynamic-entity-generation-and-content-population/qaa-p/12428643#M4653420</guid>
      <dc:creator>martinstenzig</dc:creator>
      <dc:date>2021-10-07T22:18:15Z</dc:date>
    </item>
    <item>
      <title>Re: CAP - Problem with dynamic entity generation and content population</title>
      <link>https://community.sap.com/t5/technology-q-a/cap-problem-with-dynamic-entity-generation-and-content-population/qaa-p/12428644#M4653421</link>
      <description>&lt;P&gt;  &lt;SPAN class="mention-scrubbed"&gt;martin.stenzig3&lt;/SPAN&gt; &lt;/P&gt;&lt;P&gt;Hi Martin&lt;/P&gt;&lt;P&gt;Could you help me with the GIT project of the dynamic entity generation, since you have worked on it,&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;Kanika&lt;/P&gt;</description>
      <pubDate>Fri, 15 Jul 2022 09:30:31 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/cap-problem-with-dynamic-entity-generation-and-content-population/qaa-p/12428644#M4653421</guid>
      <dc:creator>KM11</dc:creator>
      <dc:date>2022-07-15T09:30:31Z</dc:date>
    </item>
    <item>
      <title>Re: CAP - Problem with dynamic entity generation and content population</title>
      <link>https://community.sap.com/t5/technology-q-a/cap-problem-with-dynamic-entity-generation-and-content-population/qaa-p/12428645#M4653422</link>
      <description>&lt;P&gt;Anything specific you are looking for as I don't have a built out example but use it in a bigger context. Happy to setup a call and talk through it.&lt;/P&gt;</description>
      <pubDate>Wed, 20 Jul 2022 07:08:27 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/cap-problem-with-dynamic-entity-generation-and-content-population/qaa-p/12428645#M4653422</guid>
      <dc:creator>martinstenzig</dc:creator>
      <dc:date>2022-07-20T07:08:27Z</dc:date>
    </item>
    <item>
      <title>Re: CAP - Problem with dynamic entity generation and content population</title>
      <link>https://community.sap.com/t5/technology-q-a/cap-problem-with-dynamic-entity-generation-and-content-population/qaa-p/12428646#M4653423</link>
      <description>&lt;P&gt;Hi &lt;SPAN class="mention-scrubbed"&gt;martin.stenzig3&lt;/SPAN&gt; &lt;/P&gt;&lt;P&gt;Entity kind of remains undefined for me.&lt;/P&gt;&lt;P&gt;Not sure if it deploys and while doing a read at the entity it gives me an error invalid table name: Could not find table/view V_TEST in schema CAC93ACB4A8A470AA92DEF91AE7BF8EC.&lt;/P&gt;
 const db = await 
&lt;A rel="nofollow" href="http://cds.connect.to" target="_blank"&gt;cds.connect.to("db");&lt;/A&gt;&lt;BR /&gt;&lt;P&gt;let myNewCsn = cds.compile(cdlString) &lt;/P&gt;&lt;P&gt;await cds.deploy(myNewCsn).to(db) &lt;/P&gt;&lt;P&gt;console.log('NewCDL: ', JSON.stringify(myNewCsn)) &lt;/P&gt;&lt;P&gt;let entity = myNewCsn.definitions[entityName] &lt;/P&gt;&lt;P&gt;cds.model.definitions[entityName] = entity; &lt;/P&gt;&lt;P&gt;let t = await db.read(entity) 
&lt;/P&gt;&lt;STRONG&gt;&lt;BR /&gt;console.log('Entity (user) is:', entity)&lt;BR /&gt;&lt;/STRONG&gt; 
&lt;STRONG&gt;Entity (user) is: entity {&lt;BR /&gt;&lt;/STRONG&gt; 
&lt;STRONG&gt; kind: 'entity',&lt;/STRONG&gt;&lt;STRONG&gt;&lt;BR /&gt;'@cds.persistence.exists': true,&lt;/STRONG&gt;&lt;STRONG&gt;&lt;BR /&gt;elements: [Object: null prototype]&lt;/STRONG&gt;&lt;STRONG&gt;&lt;BR /&gt; { CODE: string { '@title': 'CODE: CODE', key: true, type: 'cds.String', length: 3 },&lt;/STRONG&gt;&lt;STRONG&gt;&lt;BR /&gt; TEXT: string { '@title': 'TEXT: TEXT', key: true, type: 'cds.String', length: 10 }&lt;/STRONG&gt;&lt;STRONG&gt;&lt;BR /&gt;} }&lt;/STRONG&gt; 
&lt;BR /&gt; &lt;P&gt;Also how this statement mentioned in your previous comment helps?&lt;/P&gt; 
&lt;PRE&gt;&lt;CODE&gt;cds.model.definitions[entityName] = myNewCsn[entityName]&lt;/CODE&gt;&lt;/PRE&gt;
 &lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;Kanika&lt;/P&gt;</description>
      <pubDate>Fri, 22 Jul 2022 10:12:02 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/cap-problem-with-dynamic-entity-generation-and-content-population/qaa-p/12428646#M4653423</guid>
      <dc:creator>KM11</dc:creator>
      <dc:date>2022-07-22T10:12:02Z</dc:date>
    </item>
    <item>
      <title>Re: CAP - Problem with dynamic entity generation and content population</title>
      <link>https://community.sap.com/t5/technology-q-a/cap-problem-with-dynamic-entity-generation-and-content-population/qaa-p/12428647#M4653424</link>
      <description>&lt;P&gt;Kanika, &lt;/P&gt;&lt;P&gt;can you refresh my memory, what are you trying to do? &lt;/P&gt;&lt;P&gt;In my case, I wanted to utilize CAP tools at runtime as I did not know what the exact definition of my entity is when I start up my CAP application. My specific case is a connection to SF. In that case you might have different meta data depending on the SF instance. &lt;/P&gt;&lt;P&gt;Regards, &lt;/P&gt;</description>
      <pubDate>Tue, 09 Aug 2022 03:43:04 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/cap-problem-with-dynamic-entity-generation-and-content-population/qaa-p/12428647#M4653424</guid>
      <dc:creator>martinstenzig</dc:creator>
      <dc:date>2022-08-09T03:43:04Z</dc:date>
    </item>
  </channel>
</rss>

