diff --git a/docs/en/05-develop/03-insert-data.md b/docs/en/05-develop/03-insert-data.md index 2956a224a448ff573da96c53aa5858aa5d131b37..988f51c4bf7e53aeec7504d5def71ad58b893823 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 79d0a67fb5de50a34013b17bdfe59c2ebbb8b42b..9dc0d25957f9287d6ccf0bd5606126ec982d3e97 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 0000000000000000000000000000000000000000..e7e59eb26b1b05597d6827e93f0b13301cd50efe --- /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 0000000000000000000000000000000000000000..c6218515c0f34e555a8530ca2e0f04c304763a0a --- /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();