简介
cds-hot 是一个 npm 包,可以用于热重载 SAP Cloud Application Programming Model 中的 CDS 服务。
安装
使用 npm 安装 cds-hot:
npm install cds-hot
使用
在 CDS 服务文件(例如 service.cds)中,使用 cds-hot 声明:
using cds from 'cds' using hot from 'cds-hot'
然后,在服务启动时,使用 hot() 函数启用热重载功能:
-- -------------------- ---- ------- ----- --- - --------- ------------------- ------ ------------- ---- ----- -- - ----------------------------------------- ---- ------ -- -- --- ------------- -------- ------------- ------------ ---------- -- - ------------------ --------------- --展开代码
此时,修改 CDS 服务文件后,应用程序会自动重新装载服务。
示例
在本示例中,我们将创建一个简单的 CDS 服务,包含一个名为 Books 的实体。
创建 CDS 服务
创建一个新目录,并使用以下命令初始化 npm 包:
npm init -y
安装依赖项:
npm install cds cds-runtime
在根目录下创建一个名为 service.cds 的文件,包含以下代码:
service CatalogService { entity Books { key ID: Integer; title: String; author: String; stock: Integer; } }
在根目录下创建一个名为 server.js 的文件,包含以下代码:
-- -------------------- ---- ------- ----- --- - ------------------- ----- ------- - ------------------ ----- --- - --------- ------------------- --- -- - ------------- ---- ----- -- - ----------------------------------------- ---- ------ -- -- --- ------------- -------- ------------- ------------------------- ---------- -- - ------------------ --------------- -- ----- ---- - ---------------- -- ---- ---------------- -- -- - ---------------- -- --------- -- -------------------------- --展开代码
运行示例
启动应用程序:
node server.js
现在,在浏览器中访问以下 URL:
http://localhost:4004/odata/CatalogService/Books
应该看到一个空数组,表示 Books 实体没有任何记录。
插入数据
使用以下 curl 命令,向 Books 实体插入一些数据:
curl -X POST -H 'Content-Type: application/json' -d '{"ID": 1, "title": "The Great Gatsby", "author": "F. Scott Fitzgerald", "stock": 5}' http://localhost:4004/odata/CatalogService/Books curl -X POST -H 'Content-Type: application/json' -d '{"ID": 2, "title": "To Kill a Mockingbird", "author": "Harper Lee", "stock": 3}' http://localhost:4004/odata/CatalogService/Books curl -X POST -H 'Content-Type: application/json' -d '{"ID": 3, "title": "1984", "author": "George Orwell", "stock": 8}' http://localhost:4004/odata/CatalogService/Books
查询数据
使用以下 curl 命令,查询 Books 实体中的所有记录:
curl http://localhost:4004/odata/CatalogService/Books
应该返回 Books 实体中的所有记录。
修改代码
现在,我们来修改 service.cds 文件,为 Books 实体添加一个 description 属性:
-- -------------------- ---- ------- ------- -------------- - ------ ----- - --- --- -------- ------ ------- ------- ------- ------------ ------- ------ -------- - -展开代码
重启应用程序(按 Ctrl + C 停止 node,然后再次运行 "node server.js")。
现在,使用以下 curl 命令查询 Books 实体的元数据:
curl http://localhost:4004/odata/CatalogService/Books/\$metadata
在元数据中,可以看到 description 属性。
我们还可以使用以下 curl 命令向 Books 实体插入一些新数据,包括 description 属性:
curl -X POST -H 'Content-Type: application/json' -d '{"ID": 4, "title": "The Catcher in the Rye", "author": "J.D. Salinger", "description": "One of the most controversial and acclaimed novels ever written, The Catcher in the Rye is a classic coming-of-age story." ,"stock": 6}' http://localhost:4004/odata/CatalogService/Books
现在,查询 Books 实体中的所有记录:
curl http://localhost:4004/odata/CatalogService/Books
可以看到新的记录已经被插入,并且包括 description 属性。
总结
本文介绍了 npm 包 cds-hot 的使用方法。借助 cds-hot,您可以自动重新加载 SAP Cloud Application Programming Model 中的 CDS 服务。通过简单的示例,我们演示了如何创建一个包含 Books 实体的 CDS 服务,向其中插入数据,查询数据,并修改代码以添加新属性。如果您正在开发基于 SAP Cloud Platform 的应用程序,那么 cds-hot 可能会成为您的重要工具。
来源:JavaScript中文网 ,转载请注明来源 https://www.javascriptcn.com/post/6005562881e8991b448d3138