Skip to content

Latest commit

 

History

History
132 lines (100 loc) · 3.11 KB

models.md

File metadata and controls

132 lines (100 loc) · 3.11 KB
shorty synopsis status
Nature of Models
Introduces the fundamental principles of CDS models.
released

The Nature of Models

{{ $frontmatter.synopsis }}

Models in cds are plain JavaScript objects conforming to the Core Schema Notation (CSN). They can be parsed from .cds sources, read from .json or .yaml files or dynamically created in code at runtime.

The following ways and examples of creating models are equivalent:

In Plain Coding at Runtime

const cds = require('@sap/cds')

// define the model
var model = {definitions:{
    Products: {kind:'entity', elements:{
        ID: {type:'Integer', key:true},
        title: {type:'String', length:11, localized:true},
        description: {type:'String', localized:true},
    }},
    Orders: {kind:'entity', elements:{
        product: {type:'Association', target:'Products'},
        quantity: {type:'Integer'},
    }},
}}

// do something with it
console.log (cds.compile.to.yaml (model))

Parsed at Runtime

const cds = require('@sap/cds')

// define the model
var model = cds.parse (`
    entity Products {
        key ID: Integer;
        title: localized String(11);
        description: localized String;
    }
    entity Orders {
        product: Association to Products;
        quantity: Integer;
    }
`)

// do something with it
console.log (cds.compile.to.yaml (model))

From .cds Source Files

// some.cds source file
entity Products {
    key ID: Integer;
    title: localized String(11);
    description: localized String;
}
entity Orders {
    product: Association to Products;
    quantity: Integer;
}

Read/parse it, and do something with it, for example:

const cds = require('@sap/cds')
cds.get('./some.cds') .then (cds.compile.to.yaml) .then (console.log)

Which is equivalent to: cds ./some.cds -2 yaml using the CLI

From .json Files

{"definitions": {
    "Products": {
        "kind": "entity",
        "elements": {
            "ID": { "type": "Integer", "key": true },
            "title": { "type": "String", "length": 11, "localized": true },
            "description": { "type": "String", "localized": true }
        }
    },
    "Orders": {
        "kind": "entity",
        "elements": {
            "product": { "type": "Association", "target": "Products" },
            "quantity": { "type": "Integer" }
        }
    }
}}
const cds = require('@sap/cds')
cds.get('./some.json') .then (cds.compile.to.yaml) .then (console.log)

From Other Frontends

You can add any other frontend instead of using CDL; it's just about generating the respective CSN structures, most easily as .json. For example, different parties already added these frontends:

  • ABAP CDS 2 csn
  • OData EDMX 2 csn
  • Fiori annotation.xml 2 csn
  • i18n properties files 2 csn
  • Java/JPA models 2 csn

Processing Models

All model processing and compilation steps, which can be applied subsequently just work on the basis of plain CSN objects. There's no assumption about and no lock-in to a specific source format.