From 4712b67a99a350fc5352c734ae243ce517a43c2e Mon Sep 17 00:00:00 2001 From: dingbo Date: Sat, 18 Jun 2022 18:07:47 +0800 Subject: [PATCH] docs: node --- docs/en/05-develop/03-insert-data.md | 2 +- docs/en/05-develop/04-query-data.md | 5 +++++ docs/examples/node/insert.js | 30 ++++++++++++++++++++++++++++ docs/examples/node/query.js | 29 +++++++++++++++++++++++++++ 4 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 docs/examples/node/insert.js create mode 100644 docs/examples/node/query.js diff --git a/docs/en/05-develop/03-insert-data.md b/docs/en/05-develop/03-insert-data.md index 2956a224a4..988f51c4bf 100644 --- a/docs/en/05-develop/03-insert-data.md +++ b/docs/en/05-develop/03-insert-data.md @@ -71,7 +71,7 @@ In this example, we use `exec` method to execute SQL. `exec` is designed for som ```javascript - +{{#include docs/examples/node/insert.js}} ``` diff --git a/docs/en/05-develop/04-query-data.md b/docs/en/05-develop/04-query-data.md index 79d0a67fb5..9dc0d25957 100644 --- a/docs/en/05-develop/04-query-data.md +++ b/docs/en/05-develop/04-query-data.md @@ -209,5 +209,10 @@ Get all rows and print each row: + +```javascript +{{#include docs/examples/node/query.js}} +``` + \ No newline at end of file diff --git a/docs/examples/node/insert.js b/docs/examples/node/insert.js new file mode 100644 index 0000000000..e7e59eb26b --- /dev/null +++ b/docs/examples/node/insert.js @@ -0,0 +1,30 @@ +const { options, connect } = require("td2.0-rest-connector"); + +function checkError(result) { + if (result.getErrCode() !== undefined) { + console.log(result.getErrCode(), result.getErrStr()); + process.exit(1); + } +} + +async function test() { + options.url = process.env.TDENGINE_CLOUD_URL; + options.query = { token: process.env.TDENGINE_CLOUD_TOKEN }; + let conn = connect(options); + let cursor = conn.cursor(); + try { + let result = await cursor.query("DROP DATABASE IF EXISTS power"); + checkError(result); + result = await cursor.query("CREATE DATABASE power"); + checkError(result); + result = await cursor.query("CREATE STABLE power.meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (location BINARY(64), groupId INT)"); + checkError(result); + result = await cursor.query("INSERT INTO power.d1001 USING power.meters TAGS(California.SanFrancisco, 2) VALUES ('2018-10-03 14:38:05.000', 10.30000, 219, 0.31000) ('2018-10-03 14:38:15.000', 12.60000, 218, 0.33000) ('2018-10-03 14:38:16.800', 12.30000, 221, 0.31000) power.d1002 USING power.meters TAGS(California.SanFrancisco, 3) VALUES ('2018-10-03 14:38:16.650', 10.30000, 218, 0.25000)"); + checkError(result); + console.log("AffectedRows:", result.getAffectRows()) + } catch (err) { + console.log(err); + } +} + +test(); diff --git a/docs/examples/node/query.js b/docs/examples/node/query.js new file mode 100644 index 0000000000..c6218515c0 --- /dev/null +++ b/docs/examples/node/query.js @@ -0,0 +1,29 @@ +const { options, connect } = require("td2.0-rest-connector"); + +function checkError(result) { + if (result.getErrCode() !== undefined) { + console.log(result.getErrCode(), result.getErrStr()); + process.exit(1); + } +} + +async function test() { + options.url = process.env.TDENGINE_CLOUD_URL; + options.query = { token: process.env.TDENGINE_CLOUD_TOKEN }; + let conn = connect(options); + let cursor = conn.cursor(); + try { + let result = await cursor.query("SELECT ts, current FROM power.meters LIMIT 2"); + console.log(result.getMeta()); + // [ + // { columnName: 'ts', code: 9, size: 8, typeName: 'timestamp' }, + // { columnName: 'current', code: 6, size: 4, typeName: 'float' } + // ] + console.log(result.getData()); + // [ [ '2018-10-03T14:38:05Z', 10.3 ], [ '2018-10-03T14:38:15Z', 12.6 ] ] + } catch (err) { + console.log(err); + } +} + +test(); -- GitLab