1

我重构了我的项目以使用新的云平台应用程序编程模型。

我的项目结构看起来像这样:

<my_project_name>
  - hdb (db module)
  - srv (service module)
  - ui (ui module)
  - cdsrc.json
  - package.json
  - mta.yaml

cdsrc.json:

{
  "build": {
    "target": ".",
    "tasks": [
      {"src":"hdb","for":"hana","options":{"model":["hdb","srv"]}},
      {"src":"srv","for":"node-cf","options":{"model":["hdb","srv"]}}
    ]
  }
}

包.json:

{
  "name": "<my_project_name>",
  "description": "",
  "version": "1.0.0",
  "engines": {
    "node": "^8.9"
  },
  "files": [
    "hdb",
    "srv"
  ],
  "dependencies": {
    "@sap/cds": "^3.7.1",
    "express": "^4.16.4",
    "hdb": "^0.17.0"
  },
  "devDependencies": {
    "sqlite3": "^4.0.8"
  },
  "scripts": {
    "start": "cds run",
    "build": "cds build/all --clean",
    "deploy": "cds deploy"
  },
  "cds": {
    "requires": {
      "db": {
        "kind": "sqlite",
        "model": [
          "hdb",
          "srv"
        ],
        "credentials": {
          "database": "sqlite.db"
        }
      }
    },
    "folders": {
      "db": "hdb",
      "srv": "srv"
    }
  }
}

srv/package.json:

{
    "name": "srv",
    "dependencies": {
        "@sap/cds": "^3.7.1",
        "@sap/xssec": "^2.1.12",
        <other_packages>
    },
    "scripts": {
        "start": "node server.js"
    },
    "cds": {
        "requires": {
            "db": {
                "kind": "hana",
                "model": "gen/csn.json"
            }
        }
    }
}

srv/server.js:

const cds = require("@sap/cds");
const express = require("express");
//other imports
var app = express();

//other middleware

cds.connect();

cds.serve("gen/csn.json", {
        crashOnError: false
    })
    .in(app)
    .catch((err) => {
        console.log(err);
    });

app.listen(port,  () => {
    console.info(`App listening on port: ${port}`);
});

当我在 WebIDE 中构建我的项目时,它会成功构建所有 .cds 文件,并将这样的输出写入控制台:

> <my_project_name>@1.0.0 build /hana/shared/HXE/xs/controller_data/executionagent/executionroot/269f9533-c450-4f35-ab0e-b62990473bb5/app/META-INF/.sap_java_buildpack/tomcat/temp/builder/sap.cds.mta/builds/build-838087488308692914/<my_project_name>
> cds build/all --clean

[cds] - cds [3.10.0], compiler [1.13.4], home [/hana/shared/HXE/xs/controller_data/executionagent/executionroot/269f9533-c450-4f35-ab0e-b62990473bb5/app/META-INF/.sap_java_buildpack/tomcat/temp/builder/sap.cds.mta/builds/build-838087488308692914/<my_project_name>/node_modules/@sap/cds]

[cds] - building project [/hana/shared/HXE/xs/controller_data/executionagent/executionroot/269f9533-c450-4f35-ab0e-b62990473bb5/app/META-INF/.sap_java_buildpack/tomcat/temp/builder/sap.cds.mta/builds/build-838087488308692914/<my_project_name>], clean [true]

[cds] - building module [hdb] using [Hana DB Module Builder]
[cds] - model: hdb/<file1>.cds, hdb/<file2>.cds, <all_other_cds_files>

[cds] - building module [srv] using [Node CF Module Builder]
[cds] - model: model: hdb/<file1>.cds, hdb/<file2>.cds, <all_other_cds_files>

12:03:53 PM (DIBuild) 
[cds] - done > wrote output to:
  hdb/src/gen/.hdinamespace
  hdb/src/gen/csv/<file1>_<entity1>.hdbcds
  hdb/src/gen/csv/<file1>_<entity2>.hdbcds
  hdb/src/gen/csv/<file2>_<entity1>.hdbcds
  ...
  srv/gen/csn.json
  srv/gen/_i18n/i18n.json

[cds] - time: 2600.051ms

CDS return code: 0

即使“cds build”命令在“hdb/src/gen”和“srv/gen”文件夹中生成这些文件(我通过 SSH-ing 到安装 SAP Hana 的匹配项看到所有文件,在上面输出的路径上: /hana/shared/HXE/xs/controller_data/executionagent/executionroot/269f9533-c450-4f35-ab0e-b62990473bb5/app/META-INF/.sap_java_buildpack/tomcat/temp/builder/sap.cds.mta/builds/build- 838087488308692914/),它们不会显示在 WebIDE 的项目资源管理器中。

当我构建我的 db 模块 (hdb) 时,它将工件部署到 HDI 容器而没有任何错误,我可以使用数据库资源管理器浏览容器

但是运行“srv”模块会给我这个错误:

{ Error: Couldn't find a CDS model at: gen/csn.json
    at notFound (/hana/shared/HXE/xs/controller_data/executionagent/executionroot/2dff31be-8521-4d87-a173-f71c65f6b69c/app/node_modules/@sap/cds/lib/models/load.js:59:39)
    at EventEmitter.load (/hana/shared/HXE/xs/controller_data/executionagent/executionroot/2dff31be-8521-4d87-a173-f71c65f6b69c/app/node_modules/@sap/cds/lib/models/load.js:10:50)
    at _loadModel (/hana/shared/HXE/xs/controller_data/executionagent/executionroot/2dff31be-8521-4d87-a173-f71c65f6b69c/app/node_modules/@sap/cds/lib/runtime/serve.js:78:35)
    at <anonymous>
    at runMicrotasksCallback (internal/process/next_tick.js:122:5)
    at _combinedTickCallback (internal/process/next_tick.js:132:7)
    at process._tickCallback (internal/process/next_tick.js:181:9)
    at Function.Module.runMain (module.js:696:11)
    at startup (bootstrap_node.js:225:16)
    at bootstrap_node.js:646:3 code: 'MODEL_NOT_FOUND', model: 'gen/csn.json' }
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! serve@ start: `node server.js`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the serve@ start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.


npm ERR! A complete log of this run can be found in:
npm ERR!     /hana/shared/HXE/xs/controller_data/executionagent/executionroot/2dff31be-8521-4d87-a173-f71c65f6b69c/app/.npm/_logs/2019-06-17T11_49_21_417Z-debug.log

当然,“cds build”生成 csn.json 的路径和 serve.js 想要加载该模型的路径是不同的。但是,它不会自动将所有文件复制到“srv”模块执行根目录吗?

我目前的工作流程:

  • 使用 VS Code 在本地更改 CDS 文件
  • 构建项目,因此它生成 csn.json
  • 提交更改并推送到 Git 存储库
  • 在 WebIDE 中拉取更改
  • CDS Build,所以我可以将更改部署到数据库
  • 在运行“srv”模块之前,从本地生成的 csn.json 复制内容,将其粘贴到 WebIDE 的 csn.json 中,并将路径替换为 CDS 构建控制台输出中给出的路径。

我感谢任何帮助解决这个问题。:)

4

0 回答 0