登錄 SAP Business Technology Platform,找到 space 下自己創建好的 HANA Cloud 實例,右鍵菜單選擇 Copy SQL Endpoint,將 HANA Cloud 實例的 endpoint 拷貝下來:
25eeba68-24ce-4b2d-aaa5-ee8d599ff4a0.hana.trial-eu10.hanacloud.ondemand.com:443
稍後我們在 Node.js 代碼裏,會使用到。
新建一個 Node.js 應用,安裝 hana client 開發庫:
npm install --save @sap/hana-client
完整的源代碼:
var hana = require("@sap/hana-client");
var connOptions = {
serverNode: "25eeba68-24ce-4b2d-aaa5-ee8d599ff4a0.hana.trial-eu10.hanacloud.ondemand.com:443",
encrypt: "true",
sslValidateCertificate: "false",
uid: "DBADMIN",
pwd: "Sgd",
};
var dbConnection = hana.createConnection();
dbConnection.connect(connOptions, function (err) {
if (err) throw err;
dbConnection.exec(
`SELECT TOP 1000
"REGION",
"DESCRIPTION"
FROM "PLAIN"."REGIONS"`,
function (err, result) {
if (err) throw err;
for( var i = 0; i < result.length; i++)
console.log(result[i]);
dbConnection.disconnect();
}
);
});
執行結果:
Node.js 代碼裏打印的數據庫表 REGIONS,來自 PLAIN schema:
注意,使用 DBADMIN 這個用戶,只能訪問到該用戶擁有 access right 的數據庫表。
例如我們更換使用 Node.js 訪問的數據庫表名稱爲 DB1.COMMUNITY:
此時,Node.js 應用報錯:
Error: insufficient privilege: Detailed info for this error can be found with guid '6F6F68ED5F58804CB8C7B25D7559D0E0'
更多Jerry的原創文章,盡在:"汪子熙":