Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Universal CSN #866

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/compile/unfold/assocs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = (d) => {
if (d.kind === 'element' && !d.on && d.keys) {
// return console.log ('unfold assoc:', d.parent.name, d.name)
// Managed Associations become unmanaged ones
d.on = d.keys.reduce((on,k) => {
return on.concat([ {ref:[ d.name, ...k.ref ]}, '=', {ref:[ k.as||k.ref[0] ]} ])
}, [])
Object.defineProperty (d, 'keys', {value:d.keys, configurable:true, enumerable:false})
}
}
21 changes: 21 additions & 0 deletions lib/compile/unfold/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// https://github.tools.sap/cap/sflight-ea/blob/main/doc/CSN.md
const cds = require ('@sap/cds/lib')

class Unfolds {
get structs() { return super.structs = require('./structs.js') }
get assocs() { return super.assocs = require('./assocs') }
}

/**
* Use this function to unfold compact CSN models like that:
* @example cds.unfold(m,'assocs','structs')
* @type Unfolds & <T>(csn:T,unfolds[]:string) => T
*/
const unfold = Object.setPrototypeOf((csn,...unfolds)=>{
const done = (csn.meta ??= {}).unfolded ??= []
const todo = unfolds.filter(u => !done.includes(u))
try { return cds.linked(csn).forall(d => todo.forEach(t => unfold[t](d))) }
finally { done.push(...todo) }
}, new Unfolds)

module.exports = unfold
43 changes: 43 additions & 0 deletions lib/compile/unfold/structs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
module.exports = (d) => {
if (d.kind === 'element' && (d.elements || d.foreignKeys)) {
// return console.log ('unfold struct', d.parent.name, d.name)
// add flattened elements for structs
for (let e in d.elements) {
let name = d.name+'_'+e
let flat = d.parent.elements[name] = {
__proto__: d.elements[e], key: d.key,
... d.elements[e] // REVISIT: remove that for productive code
}
_add_derived (flat,'name',name)
}
// structs stay in model, bot become non-enumerable elements
if (!d.isAssociation) _add_derived (d.parent.elements, d.name, d)
// the flattended elements became keys -> structs associations aren't
delete d.key
// console.log (d.parent.name, d.name, d)
}
}

const _add_derived = (o,p,v,enumerable=false) => {
Object.defineProperty (o,p, { value:v, enumerable, writable:true, configurable:true })
return v
}

;()=>{

let payload1 ={

Check failure on line 28 in lib/compile/unfold/structs.js

View workflow job for this annotation

GitHub Actions / build (20.x)

'payload1' is assigned a value but never used

Check failure on line 28 in lib/compile/unfold/structs.js

View workflow job for this annotation

GitHub Actions / build (18.x)

'payload1' is assigned a value but never used

Check failure on line 28 in lib/compile/unfold/structs.js

View workflow job for this annotation

GitHub Actions / build (16.x)

'payload1' is assigned a value but never used
struct_a: 6,
struct_b: 6
}

let payload2 ={

Check failure on line 33 in lib/compile/unfold/structs.js

View workflow job for this annotation

GitHub Actions / build (20.x)

'payload2' is assigned a value but never used

Check failure on line 33 in lib/compile/unfold/structs.js

View workflow job for this annotation

GitHub Actions / build (18.x)

'payload2' is assigned a value but never used

Check failure on line 33 in lib/compile/unfold/structs.js

View workflow job for this annotation

GitHub Actions / build (16.x)

'payload2' is assigned a value but never used
struct: { a: 6, b: 6 }
}

let payload3 ={

Check failure on line 37 in lib/compile/unfold/structs.js

View workflow job for this annotation

GitHub Actions / build (20.x)

'payload3' is assigned a value but never used

Check failure on line 37 in lib/compile/unfold/structs.js

View workflow job for this annotation

GitHub Actions / build (18.x)

'payload3' is assigned a value but never used

Check failure on line 37 in lib/compile/unfold/structs.js

View workflow job for this annotation

GitHub Actions / build (16.x)

'payload3' is assigned a value but never used
struct: { a: 6, b: 6 },
struct_a: 7,
struct_b: 8
}

}
15 changes: 15 additions & 0 deletions lib/monkey-patch-cds.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const cds = require("@sap/cds/lib")

const unfold = require("./compile/unfold")
cds.compile.for.nodejs
cds.compile.for.nodejs = csn => {
_plain_csn ??= cds.compile(cds.clone(csn))
return unfold(csn,'assocs','structs')
}

const _2edm = cds.compile.to.edm
const _2sql = cds.compile.to.sql
cds.compile.to.edm = (csn,o) => _2edm(_plain_csn||csn,o)
cds.compile.to.sql = (csn,o) => _2sql(_plain_csn||csn,o)

let _plain_csn
1 change: 1 addition & 0 deletions server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require('./lib/monkey-patch-cds.js')
Loading