通用

  • 导入API接口定义:cds import

数据库

  • 编译CDS模型为不同格式的输出:cds compile

  • 部署至SQLite来持久化数据:cds deploy --to sqlite

  • 添加HANA的配置:cds add hana

  • 部署至HANA来持久化数据:cds deploy –-to hana

安全

  • 基于CDS Annotations来生成XSUAA相关的部署配置:cds add xsuaa

  • 更新XSUAA相关的部署配置:cds compile

MTA

  • 根据package.json生成MTA相关部署配置:cds add mta

Fiori工具

  • 增加前端应用部署相关配置:fiori add deploy-config

通用

导入API接口定义:cds import

你需要做的第一件事是获取XML文件 (EDM XML,Entity Data Model XML,缩写 EDMX) ,这些文件定义了OData服务包含的实体,以便后续应用程序使用。你可以从SAP API Business Hub获得这些XML文件。

使用 cds import 命令将API接口定义导入你的项目:

cds import srv/<api-definition-file-name>.edmx

这条命令会将你的API接口定义以CSN(Core Schema Notation)的格式加入srv/external文件夹中,同时拷贝EDMX文件至该文件。你可以看到,CSN文件不过是一个对服务元数据以及所包含实体描述的JSON文件。通过该JSON文件,CDS框架可以以简单且安全的CQL(CDS Query Language)方式调用OData服务。

另外,对于Node.js来说,API将会以外部OData服务的形式被加入package.json的配置:

"cds": {
 "requires": {
     "API_BUSINESS_PARTNER": {
         "kind": "odata-v2",
         "model": "srv/external/API_BUSINESS_PARTNER"
     }
 }
}

基于上面的配置,可以为其添加destination来连接远端服务:Use Destinations with Node.js. 与此同时,你需要在BTP的Cockpit里面创建一个destination,比如:S4HANA

"cds": {
 "requires": {
     "API_BUSINESS_PARTNER": {
         "kind": "odata",
         "model": "srv/external/API_BUSINESS_PARTNER",
         "[production]": {
             "credentials": {
                 "destination": "S4HANA",
                 "path": "/sap/opu/odata/sap/API_BUSINESS_PARTNER"
             }
         }
     }
 }
}

或者,你可以在.cdsrc.json文件中设置options和flags参数,以便后续用cds import导入至项目中。

例如:

{
 "import": {
     "as": "cds",
     "force": true,
     "include_namespaces": "sap,c4c"
 }
}

在准备好以上配置文件后,你就可以用 cds import <file-name>命令来导入了。

数据库

编译CDS模型为不同格式的输出:cds compile

cds compile db/ --to sql
cds compile db/ --to json
cds compile db/ --to yml
...

或者:

cds db/ -2 sql
cds db/ -2 json
cds db/ -2 yml
...

目前支持的输出类型:

- json, yml
- edm, edmx, edmx-v2, edmx-v4, edmx-w4, edmx-x4
- sql, hdbcds, hdbtable
- cdl [beta]
- xsuaa
- openapi

例如: cds compile db/ --to sql

CREATE TABLE sap_ui_riskmanagement_Risks (
createdAt TIMESTAMP_TEXT,
createdBy NVARCHAR(255),
modifiedAt TIMESTAMP_TEXT,
modifiedBy NVARCHAR(255),
ID NVARCHAR(36) NOT NULL,
title NVARCHAR(100),
prio NVARCHAR(5),
descr NVARCHAR(5000),
miti_ID NVARCHAR(36),
impact INTEGER,
criticality INTEGER,
PRIMARY KEY(ID)
);

CREATE TABLE sap_ui_riskmanagement_Mitigations (
createdAt TIMESTAMP_TEXT,
createdBy NVARCHAR(255),
modifiedAt TIMESTAMP_TEXT,
modifiedBy NVARCHAR(255),
ID NVARCHAR(36) NOT NULL,
description NVARCHAR(5000),
owner NVARCHAR(5000),
timeline NVARCHAR(5000),
PRIMARY KEY(ID)
);

部署至SQLite来持久化数据:cds deploy --to sqlite

初始情况下,CDS使用内存来临时存储数据。除了使用内存,我们还可以使用持久数据库,比如:SQLite。

与默认提供的内存数据库不同的是,我们可以持久化方地将数据存储在本地的文件中,比如./sqlite.db。这会被记录在package.json中。

cds deploy --to sqlite

在获得本地数据库文件后,我们可以利用命令行工具sqlite3来对数据库进行数据查询:

sqlite3 sqlite.db .dump
sqlite3 sqlite.db .tables

添加HANA的配置:cds add hana

如果想要使用HANA作为持久化数据库,我们需要添加一些配置。可以利用以下命令实现一键配置:

cds add hana

package.json文件中,会自动添加dependency和cds requires的配置:

{
 ...
 "dependencies": {
    ...
    "hdb": "^0.19.0"
 },
 ...
 "cds": {
    "requires": {
      ...
      "db": {
        "kind": "hana-cloud"        
      }
    }
 }
}

或者,你还可以只针对生产环境做相应的配置:

cds add hana --for production

package.json文件中,会自动添加dependency和cds requires的配置:

{
...
"dependencies": {
 ...
 "hdb": "^0.19.0"
},
...
"cds": {
 "requires": {
   ...
   "[production]": {
     "db": {
       "kind": "hana-cloud"
     }
   },
   "db": {
     "kind": "sql"        
   }
 }
}
}

又或者,你还可以做混合模式的配置:

cds add hana --for hybrid

部署至HANA来持久化数据:cds deploy –-to hana

初始情况下,CDS使用内存来临时存储数据。除了使用内存,我们还可以使用持久数据库,比如:HANA。cds deploy命令可以将项目的数据库部分部署至HANA。服务端应用(Node.js或者Java部分)仍然可以本地运行,同时连接到远端的HANA数据库实例,提高开发效率。

cds deploy –-to hana
cds watch

cds deploy –-to hana 命令背后做了以下的事情:

  1. 编译项目的数据库构件至根目录下的gen文件夹,以便进一步部署至HANA Cloud实例中

  2. 创建一个SAP HANA Schemas & HDI Containers( hdi-shared plan)类型的服务实例,来处理HDI容器

  3. 在HDI容器中创建来自应用的数据库构件

  4. 通过修改default-env.json文件将服务实例绑定到本地应用中,配置包括有数据库相关的密钥信息。

或者,在混合模式下:

cds deploy –-to hana --profile hybrid
cds watch --profile hybrid

安全

基于CDS Annotations来生成XSUAA相关的部署配置:cds add xsuaa

cds add xsuaa
cds add xsuaa --for production

cds add xsuaa命令背后做的事情:

  • 添加XSUAA配置到项目的package.json文件中

  • 创建XSUAA配置文件xs-security.json,并根据CDS annotation自动生成role和scope的定义。

确保在这些annotation发生更改时重新运行cds compile --to xsuaa,详见:Authorization guide

更新XSUAA相关的部署配置:cds compile

确保在这些annotation发生更改时重新运行cds compile --to xsuaa,详见:Authorization guide

cds compile srv/ --to xsuaa > xs-security.json
{
"scopes": [
 {
   "name": "$XSAPPNAME.Admin",
   "description": "Admin"
 }
],
"role-templates": [
 {
   "name": "Admin",
   "description": "generated",
   "scope-references": [
     "$XSAPPNAME.Admin"
   ],
   "attribute-references": []
 }
]
}

更新后,使用cf create-service xsuaa application <servicename> -c xs-security.json命令来重新创建一个带有此配置的XSUAA实例,或者,通过cf update-service <servicename> -c xs-security.json命令来更新旧实例的配置。

MTA

根据package.json生成MTA相关部署配置:cds add mta

要想将应用程序部署到SAP BTP的Cloud Foundry环境上,你会用到MTA(Multi-Target Application)的部署方式。这种方式可以提高部署效率,通过配置文件mta.yaml一次性将所有项目内容部署到平台上,包括:UI前端应用、后端服务、数据库、服务实例创建和绑定等。

利用以下命令来让CAP服务器自动生成mta.yaml文件。该文件是根据项目中的package.json文件生成的:

cds add mta

例如:

package.json

{
"name": "cpapp",
"version": "1.0.0",
"description": "A simple CAP project.",
"repository": "<Add your repository here>",
"license": "UNLICENSED",
"private": true,
"dependencies": {
"@sap/cds": "^6",
"@sap/xssec": "^3.2.8",
"express": "^4",
"passport": "^0.5.0",
"hdb": "^0.19.0"
},
"devDependencies": {
"@sap/ux-specification": "^1.90.12",
"sqlite3": "^5.0.2"
},
"engines": {
"node": "^16"
},
"scripts": {
"start": "cds run",
"watch-risks": "cds watch --open risks/webapp/index.html#fe-lrop-v4",
"watch-mitigations": "cds watch --open mitigations/webapp/index.html#fe-lrop-v4"
},
"eslintConfig": {
"extends": "eslint:recommended",
"env": {
    "es2020": true,
    "node": true,
    "jest": true,
    "mocha": true
},
"globals": {
    "SELECT": true,
    "INSERT": true,
    "UPDATE": true,
    "DELETE": true,
    "CREATE": true,
    "DROP": true,
    "CDL": true,
    "CQL": true,
    "CXL": true,
    "cds": true
},
"rules": {
    "no-console": "off",
    "require-atomic-updates": "off"
}
},
"sapux": [
"app/risks",
"app/mitigations"
],
"cds": {
"requires": {
    "[production]": {
        "db": {
            "kind": "hana-cloud"
        },
        "auth": {
            "kind": "xsuaa"
        }
    },
    "db": {
        "kind": "sql"
    },
    "uaa": {
        "kind": "xsuaa",
        "credentials": {}
    }
}
}
}

生成的mta.yaml文件:

---
_schema-version: '3.1'
ID: cpapp
version: 1.0.0
description: "A simple CAP project."
parameters:
enable-parallel-deployments: true
build-parameters:
before-all:
    - builder: custom
      commands:
        - npx -p @sap/cds-dk cds build --production

modules:
  - name: cpapp-srv
    type: nodejs
    path: gen/srv
    parameters:
      buildpack: nodejs_buildpack
    build-parameters:
      builder: npm-ci
    provides:
      - name: srv-api # required by consumers of CAP services (e.g. approuter)
        properties:
          srv-url: ${default-url}
    requires:
      - name: cpapp-db
      - name: cpapp-auth

  - name: cpapp-db-deployer
    type: hdb
    path: gen/db
    parameters:
      buildpack: nodejs_buildpack
    requires:
      - name: cpapp-db

resources:
  - name: cpapp-db
    type: com.sap.xs.hdi-container
    parameters:
      service: hana
      service-plan: hdi-shared
  - name: cpapp-auth
    type: org.cloudfoundry.managed-service
    parameters:
      service: xsuaa
      service-plan: application
      path: ./xs-security.json
      config:
        xsappname: cpapp-${org}-${space}
        tenant-mode: dedicated

mta.yaml文件主要由module(Cloud Foundry应用)和resource组成(Cloud Foundry服务):

modules
  • cpapp-srv – OData服务

  • cpapp-db-deployer – 负责部署CAP schema和data(CSV文件)到数据库中

resources

根据package.json文件中的cds requires生成。

  • cpapp-db – SAP HANA数据库HDI容器实例

  • cpapp-uaa – XSUAA服务实例

这些资源是在MTA部署过程中自动创建和更新的Cloud Foundry服务实例。

Fiori工具

增加前端应用部署相关配置:fiori add deploy-config

npm install --global @sap/ux-ui5-tooling
npm install --global @sap/generator-fiori
cd app/<appname>
fiori add deploy-config cf

例如:

mta.yaml

_schema-version: "3.1"
ID: sfsf-projman
description: A simple CAP project.
version: 1.0.0
modules:
- name: sfsf-projman-srv
  type: nodejs
  path: gen/srv
  requires:
  - name: sfsf-projman-db
  - name: sfsf-projman_html_repo_host
  - name: sfsf-projman-destination-service
  provides:
  - name: srv-api
    properties:
      srv-url: ${default-url}
  parameters:
    buildpack: nodejs_buildpack
  build-parameters:
    builder: npm-ci
- name: sfsf-projman-db-deployer
  type: hdb
  path: gen/db
  requires:
  - name: sfsf-projman-db
  parameters:
    buildpack: nodejs_buildpack
- name: sfsf-projman-destination-content
  type: com.sap.application.content
  requires:
  - name: sfsf-projman-destination-service
    parameters:
      content-target: true
  - name: sfsf-projman_html_repo_host
    parameters:
      service-key:
        name: sfsf-projman_html_repo_host-key
  - name: uaa_sfsf-projman
    parameters:
      service-key:
        name: uaa_sfsf-projman-key
  parameters:
    content:
      instance:
        destinations:
        - Name: sfsf_projman_tia_sfsf_projman_html_repo_host
          ServiceInstanceName: sfsf-projman-html5-app-host-service
          ServiceKeyName: sfsf-projman_html_repo_host-key
          sap.cloud.service: sfsf-projman-tia
        - Authentication: OAuth2UserTokenExchange
          Name: sfsf_projman_tia_uaa_sfsf_projman
          ServiceInstanceName: sfsf-projman-xsuaa-service
          ServiceKeyName: uaa_sfsf-projman-key
          sap.cloud.service: sfsf-projman-tia
        existing_destinations_policy: ignore
  build-parameters:
    no-source: true
resources:
- name: sfsf-projman-db
  type: com.sap.xs.hdi-container
  parameters:
    service: hana
    service-plan: hdi-shared
- name: sfsf-projman-destination-service
  type: org.cloudfoundry.managed-service
  parameters:
    config:
      HTML5Runtime_enabled: true
      version: 1.0.0
    service: destination
    service-name: sfsf-projman-destination-service
    service-plan: lite
- name: sfsf-projman_html_repo_host
  type: org.cloudfoundry.managed-service
  parameters:
    service: html5-apps-repo
    service-name: sfsf-projman-html5-app-host-service
    service-plan: app-host
- name: uaa_sfsf-projman
  type: org.cloudfoundry.managed-service
  parameters:
    path: ./xs-security.json
    service: xsuaa
    service-name: sfsf-projman-xsuaa-service
    service-plan: application
parameters:
  enable-parallel-deployments: true
build-parameters:
  before-all:
  - builder: custom
    commands:
    - npx -p @sap/cds-dk cds build --production

mta.yaml文件中, 将会生成两个与SAP Fiori应用相关的module:

- name: sfsf-projman-app-content
  type: com.sap.application.content
  path: .
  requires:
  - name: sfsf-projman_html_repo_host
    parameters:
      content-target: true
  build-parameters:
    build-result: resources
    requires:
    - artifacts:
      - sfsfprojmansfsfprojman.zip
      name: sfsfprojmansfsfprojman
      target-path: resources/
- name: sfsfprojmansfsfprojman
  type: html5
  path: app/sfsf-projman
  build-parameters:
    build-result: dist
    builder: custom
    commands:
    - npm install
    - npm run build:cf
    supported-platforms: []

mta.yaml文件中,自动为destination实例生成parameters和requires的配置:

- name: sfsf-projman-destination-service
  type: org.cloudfoundry.managed-service
  parameters:
    config:
      HTML5Runtime_enabled: true
      init_data:
        instance:
          destinations:
          - Authentication: NoAuthentication
            Name: ui5
            ProxyType: Internet
            Type: HTTP
            URL: https://ui5.sap.com
          - Authentication: NoAuthentication
            HTML5.DynamicDestination: true
            HTML5.ForwardAuthToken: true
            Name: sfsf-projman-srv-api
            ProxyType: Internet
            Type: HTTP
            URL: ~{srv-api/srv-url}
          existing_destinations_policy: update
      version: 1.0.0
    service: destination
    service-name: sfsf-projman-destination-service
    service-plan: lite
  requires:
  - name: srv-api

xs-app.json文件被创建,文件包含approuter的路由配置:

{
  "welcomeFile": "/index.html",
  "authenticationMethod": "route",
  "routes": [
    {
      "source": "^/projman/(.*)$",
      "target": "/projman/$1",
      "destination": "sfsf-projman-srv-api",
      "authenticationType": "xsuaa",
      "csrfProtection": false
    },
    {
      "source": "^/resources/(.*)$",
      "target": "/resources/$1",
      "authenticationType": "none",
      "destination": "ui5"
    },
    {
      "source": "^/test-resources/(.*)$",
      "target": "/test-resources/$1",
      "authenticationType": "none",
      "destination": "ui5"
    },
    {
      "source": "^(.*)$",
      "target": "$1",
      "service": "html5-apps-repo-rt",
      "authenticationType": "xsuaa"
    }
  ]
}

ui5-deploy.yaml文件被创建:

# yaml-language-server: $schema=https://sap.github.io/ui5-tooling/schema/ui5.yaml.json
specVersion: '2.4'
metadata:
  name: sfsf.projman.sfsfprojman
type: application
resources:
  configuration:
    propertiesFileSourceEncoding: UTF-8
builder:
  resources:
    excludes:
      - "/test/**"
      - "/localService/**"
  customTasks:
  - name: webide-extension-task-updateManifestJson
    beforeTask: generateManifestBundle
    configuration:
      appFolder: webapp
      destDir: dist
  - name: ui5-task-zipper
    afterTask: generateCachebusterInfo
    configuration:
      archiveName: sfsfprojmansfsfprojman
      additionalFiles:
      - xs-app.json

另外,在package.jsonmanifest.json文件中,还会生成一些配置.

Sara Sampaio

Sara Sampaio

Author Since: March 10, 2022

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x