@sap/cds
Version:
SAP Cloud Application Programming Model - CDS for Node.js
58 lines (49 loc) • 2.26 kB
JavaScript
const { unfold_csn } = require ('../etc/_localized')
const cds = require ('../../index')
const TRACE = cds.debug('trace')
function _compile_for_nodejs (csn, o) {
let min = cds.minify (csn)
let dsn = cds.compile.for.odata(min, o) //> creates a partial copy -> avoid any cds.linked() before
dsn = unfold_csn(dsn)
dsn = cds.linked(dsn)
cds.compile.for.lean_drafts(dsn)
// @Common.Text → @cds.search
text_to_search: for (const name in dsn.definitions) {
const def = dsn.definitions[name]
if (!def.associations) continue
// if any @cds.search.* === true, @Common.Text should be ignored
for (const key in def) if (key.startsWith('@cds.search') && def[key]) continue text_to_search
let searched = false
for (const name in def.associations) {
const assoc = def.associations[name]
if (assoc['@Common.Text']?.['='] && def[`@cds.search.${assoc['@Common.Text']['=']}`] !== false) {
def[`@cds.search.${assoc['@Common.Text']['=']}`] = true
if (!searched) {
// add all string elements to @cds.search, if not annotated with @cds.search = false
for (const el in def.elements) {
const search_el = def.elements[el]
if (search_el._type === 'cds.String' && def[`@cds.search.${search_el.name}`] !== false) {
def[`@cds.search.${search_el.name}`] = true
}
}
searched = true
}
}
}
}
Object.defineProperty(csn, '_4nodejs', { value: dsn })
Object.defineProperty(dsn, '_4nodejs', { value: dsn })
Object.assign (dsn.meta, csn.meta, dsn.meta) // merge meta data, as it may have been enhanced
return dsn
}
module.exports = function cds_compile_for_nodejs (csn,o) {
if ('_4nodejs' in csn) return csn._4nodejs
// REVISIT: remove _compat_texts_entities with cds^10
if (cds.env.features.compat_texts_entities) Object.defineProperty(csn, '_compat_texts_entities', { value: true, enumerable: true })
TRACE?.time('cds.compile 4nodejs'.padEnd(22)); try {
let result, next = ()=> result ??= _compile_for_nodejs (csn,o)
cds.emit ('compile.for.runtime', csn, o, next)
return next() //> in case no handler called next
}
finally { TRACE?.timeEnd('cds.compile 4nodejs'.padEnd(22)) }
}