如何在 SAP BTP 平台上重用另一个已经开发好的 service

假设我们的 SAP Business Technology Platform 平台上已经开发好了一个 products-service
项目:

我们希望在另一个 bookstore 项目里,重用 products-service
项目。
打开 products-service
项目,在 package.json 里找到其 name
的值:@sap/capire-products

创建一个 bookstore 项目:
mvn -B archetype:generate -DarchetypeArtifactId=cds-services-archetype -DarchetypeGroupId=com.sap.cds
-DarchetypeVersion=1.17.0
-DgroupId=com.sap.cap -DartifactId=bookstore
使用 open workspace 的功能打开这个项目:

执行如下命令行,安装指向 products-service
项目的依赖:
npm install $(npm pack ../products-service -s)
成功安装完毕,现在在 bookstore
项目里,就能看到其依赖 products-service
的实现了:

npm pack 从 products-service 创建一个 tarball,然后直接将其用作书店应用程序中的依赖项。 有关 npm 包的更多信息:https://docs.npmjs.com/cli-commands/pack.html。

使用下面的命令行安装依赖:
npm install && npm dedupe
现在 bookstore
项目的 package.json 里,就能看到 products-service
项目的依赖了。

在 db 文件夹下新建一个 schema.cds
文件,维护 domain model:

在 srv 文件夹下创建一个 services.cds
文件:

在 db 文件夹下新建一个 data 文件夹,然后把 4 个 sample data 的 csv 文件放置进去。
使用命令行部署 domain model 到 sqlite:
cds deploy --to sqlite
自动生成了一个 sqlite.db
文件,该文件名维护在 package.json 文件里,文件位于项目根目录下:

修改项目里的 Application.yaml 文件,将数据库的位置指向刚刚运行的命令行 cds deploy --to sqlite
生成的 sqlite.db
文件。

第 11 行设置成 never 的原因是因为我们已经用命令行 cds deploy --to sqlite
对数据库进行了初始化操作。
修改 Business Application Studio 的 settings.json
文件,添加如下内容:
选择 SQLTools
:

可以看到刚才通过 csv 文件插入的数据:

使用如下 url:
https://workspaces-ws-pdwk4-app1.us10.trial.applicationstudio.cloud.sap/odata/v4/BooksService/Books
就能访问到之前通过 sap.capire.bookstore-Books.csv
插入到 SQLite 里的数据了:


版权声明: 本文为 InfoQ 作者【Jerry Wang】的原创文章。
原文链接:【http://xie.infoq.cn/article/3e08ef11ae5dcbc12e69d669a】。文章转载请联系作者。
评论