UNPKG

@sap/cds

Version:

SAP Cloud Application Programming Model - CDS for Node.js

50 lines (42 loc) 2.17 kB
const cds = require('../../index') const LOG = cds.log('draft') const { LinkedDefinitions, action, array } = cds.builtin.classes const { draft_new_action: dna } = cds.env.fiori const draftNew = dna === true ? 'draftNew' : dna const $self = new array ({ items: { type:'$self' }}) module.exports = function cds_compile_for_direct_crud(csn) { if (!dna) return const meta = csn.meta ??= {} if (meta.cds_compiled_for_direct_crud) return csn else meta.cds_compiled_for_direct_crud = true loop1: for (const entity of cds.linked(csn).definitions) { // Skip non-draft, and not served entities as well those with custom draftNew action if ('@Common.DraftRoot.NewAction' in entity) continue //> also supports `false` to skip if (!entity['@odata.draft.enabled']) continue //> skip non-draft entities if (entity['@cds.api.ignore']) continue //> skip not served entities if (!entity._service) continue //> e.g. @odata.draft.enabled base entities // Prepare parameters for the draftNew action from keys of the entity. const params = new LinkedDefinitions; params.in = $self for (let key of entity.keys) { if (key.name === 'IsActiveEntity') continue //> set by the client generically if (key.isComputed()) continue //> auto-generated by the service if (key.isAssociation) { LOG.warn(`Skipping adding draftNew action for "${entity.name}" due to association as key`) continue loop1 } params[key.name] = key //> gets Fiori popup to enter non-generated keys } // Add the draftNew action to the entity, if enabled, and link it via the // @Common.DraftRoot.NewAction annotation, if not already present if (!draftNew || '@Common.DraftRoot.NewAction' in entity) continue const dna = new action ({ name: draftNew, params, returns: { type: entity.name, __proto__:entity } }) LOG.debug(`Adding ${draftNew} action for entity "${entity.name}":`, dna) entity['@Common.DraftRoot.NewAction'] = `${entity._service.name}.${draftNew}` entity.actions ??= {} entity.actions[draftNew] = dna } }