From 2af021cd10797b1c975f1d21826299f4c17ba618 Mon Sep 17 00:00:00 2001 From: xialei_li Date: Thu, 16 Sep 2021 10:45:37 +0800 Subject: [PATCH] fix some description --- .../examples/{show-databases.js => demo.js} | 0 .../node-rest/examples/show-database.js | 13 ++++ .../node-rest/examples/testRestCursor.js | 12 ---- src/connector/node-rest/package.json | 12 ++-- src/connector/node-rest/readme.md | 29 ++++---- src/connector/node-rest/src/restConnect.js | 16 ++++- src/connector/node-rest/src/restConstant.js | 9 +++ src/connector/node-rest/src/restCursor.js | 25 ++++++- src/connector/node-rest/src/restResult.js | 67 ++++++++++--------- src/connector/node-rest/test/testRestConn.js | 14 ++-- 10 files changed, 120 insertions(+), 77 deletions(-) rename src/connector/node-rest/examples/{show-databases.js => demo.js} (100%) create mode 100644 src/connector/node-rest/examples/show-database.js delete mode 100644 src/connector/node-rest/examples/testRestCursor.js diff --git a/src/connector/node-rest/examples/show-databases.js b/src/connector/node-rest/examples/demo.js similarity index 100% rename from src/connector/node-rest/examples/show-databases.js rename to src/connector/node-rest/examples/demo.js diff --git a/src/connector/node-rest/examples/show-database.js b/src/connector/node-rest/examples/show-database.js new file mode 100644 index 0000000000..8722f2d001 --- /dev/null +++ b/src/connector/node-rest/examples/show-database.js @@ -0,0 +1,13 @@ +import {TDengineRestConnection} from "../src/restConnect"; + +let conn = new TDengineRestConnection({host: 'u195', user: 'root', pass: 'taosdata', port: 6041}) +let cursor = conn.cursor(); +console.log(conn) +let data = {}; +(async () => { + data = await cursor.query("show databases"); + data.toString() +})() + + + diff --git a/src/connector/node-rest/examples/testRestCursor.js b/src/connector/node-rest/examples/testRestCursor.js deleted file mode 100644 index beda3790fa..0000000000 --- a/src/connector/node-rest/examples/testRestCursor.js +++ /dev/null @@ -1,12 +0,0 @@ -import {TDengineRestConnection} from "../src/restConnect"; - -let conn = new TDengineRestConnection({host: 'u195'}) -let cursor = conn.cursor(); - -(async () => { - data = await cursor.query("select * from test.meters limit 10").catch(e => console.log(e)); - data.toString() -})() - - - diff --git a/src/connector/node-rest/package.json b/src/connector/node-rest/package.json index 7c85fa0070..1db7236efb 100644 --- a/src/connector/node-rest/package.json +++ b/src/connector/node-rest/package.json @@ -1,13 +1,15 @@ { - "name": "taos-rest", + "name": "td-rest-connector", "version": "1.0.0", - "module": "src/index.js", - "main": "lib/index.js", + "description": "A Node.js connector for TDengine restful", + "module": "src/TDengineRest.js", + "main": "lib/TDengineclearRest.js", "license": "MIT", "scripts": { "prepare": "npm run build", - "build": "esbuild --bundle --platform=node --outfile=lib/index.js src/index.js", - "build:dev": "esbuild --bundle --platform=node --outfile=dist/examples/show-database.js examples/show-databases.js --watch" + "build": "esbuild --bundle --platform=node --outfile=lib/TDengineRest.js ./TDengineRest.js", + "build:dev": "esbuild --bundle --platform=node --outfile=dist/examples/show-database.js examples/show-database.js --watch", + "build:test": "esbuild test/testRestConn.js --bundle --platform=node --outfile=dist/tests/testRestConn.js --watch" }, "devDependencies": { "esbuild": "^0.12.25", diff --git a/src/connector/node-rest/readme.md b/src/connector/node-rest/readme.md index 0317955488..db8d57c2ee 100644 --- a/src/connector/node-rest/readme.md +++ b/src/connector/node-rest/readme.md @@ -4,35 +4,32 @@ This is the Node.js library that lets you connect to [TDengine](https://www.gith restful. This restful can help you access the TDengine from different platform. ## Install +To get started, just type in the following to install the connector through [npm](https://www.npmjs.com/) -### On Linux - -### On macOs - -### On Windows +```cmd +npm install td-rest-connector +``` ## Usage ### Connection ```javascript -import taoRest from '' -var connRest = taoRest({}) -``` -close a connection -```javascript -connRest.close() +import taoRest from 'TDengineRest' +var connRest = taoRest({host:'127.0.0.1',user:'root',pass:'taosdata',port:6041}) ``` + query ```javascript -let data = connRest.Query("sql") -console.log ("data:"+data) +(async()=>{ + data = await connRest.query("show databases"); + data.toString(); + } +)() ``` ## Example -An example of using the NodeJS Restful connector to create a table with weather data and create and execute queries can be found [here](https://github.com/taosdata/TDengine/tree/master/tests/examples/node-rest/rest-node-example.js) - -An example of using the NodeJS Restful connector to achieve the same things but without all the object wrappers that wrap around the data returned to achieve higher functionality can be found [here](https://github.com/taosdata/TDengine/tree/master/tests/examples/nodejs/node-example-raw.js) +An example of using the NodeJS Restful connector to create a table with weather data and create and execute queries can be found [here](https://github.com/taosdata/TDengine/tree/master/tests/examples/node-rest/show-database.js) ## Contributing to TDengine diff --git a/src/connector/node-rest/src/restConnect.js b/src/connector/node-rest/src/restConnect.js index 0fb9593ed0..ca6acc3e47 100644 --- a/src/connector/node-rest/src/restConnect.js +++ b/src/connector/node-rest/src/restConnect.js @@ -1,9 +1,15 @@ import {TDengineRestCursor} from '../src/restCursor' /** - * + *this class collect basic information that can be used to build + * a restful connection. */ export class TDengineRestConnection { + /** + * constructor,give variables some default values + * @param options + * @returns {TDengineRestConnection} + */ constructor(options) { this.host = 'localhost' this.port = '6041' @@ -15,7 +21,7 @@ export class TDengineRestConnection { } /** - * this is a private function that + * used to init the connection info using the input options * @param options * @private */ @@ -37,9 +43,13 @@ export class TDengineRestConnection { } } + /** + * cursor will return an object of TDengineRestCursor, which can send restful(http) request and get + * the response from server. + * @returns {TDengineRestCursor} + */ cursor() { return new TDengineRestCursor(this) - console.log("return a cursor object user query sql") } } diff --git a/src/connector/node-rest/src/restConstant.js b/src/connector/node-rest/src/restConstant.js index 48994bf59e..9bab9313b3 100644 --- a/src/connector/node-rest/src/restConstant.js +++ b/src/connector/node-rest/src/restConstant.js @@ -1,3 +1,7 @@ +/** + * indicate the every type's type code + * @type {{"0": string, "1": string, "2": string, "3": string, "4": string, "5": string, "6": string, "7": string, "8": string, "9": string, "10": string}} + */ export const typeCodesToName = { 0: 'Null', 1: 'Boolean', @@ -12,6 +16,11 @@ export const typeCodesToName = { 10: 'Nchar', } +/** + * get the type of input typecode, in fact the response of restful will send every column's typecode + * @param typecode + * @returns {*} + */ export function getTaoType(typecode) { return typeCodesToName[typecode]; } \ No newline at end of file diff --git a/src/connector/node-rest/src/restCursor.js b/src/connector/node-rest/src/restCursor.js index db612528bd..204919dabf 100644 --- a/src/connector/node-rest/src/restCursor.js +++ b/src/connector/node-rest/src/restCursor.js @@ -1,7 +1,16 @@ import fetch from 'node-fetch' import {TDengineRestResultSet} from '../src/restResult' +/** + * this class is core of restful js connector + * this class resends http request to the TDengine server + * and receive the response. + */ export class TDengineRestCursor { + /** + * constructor,used to get the connection info + * @param connection + */ constructor(connection) { this._connection = null; this.data = []; @@ -13,15 +22,29 @@ export class TDengineRestCursor { } } + /** + * used to build an url,like http://localhost:6041/rest/sql + * @returns {string} + * @private + */ _apiUpl() { - // console.log((this.http ? "https" : "http") + "://" + this._connection.host + ":" + this._connection.port + this._connection.path) return (this.http ? "https" : "http") + "://" + this._connection.host + ":" + this._connection.port + this._connection.path } + /** + * used to make an authorization token + * @returns {string} + * @private + */ _token() { return 'Basic ' + Buffer.from(this._connection.user + ":" + this._connection.pass).toString('base64') } + /** + * Used fetch to send http request, and return the response as an object of TDengineRestResultSet + * @param sql + * @returns {Promise} + */ async query(sql) { try { let response = await fetch(this._apiUpl(), { diff --git a/src/connector/node-rest/src/restResult.js b/src/connector/node-rest/src/restResult.js index fd93654069..8767394228 100644 --- a/src/connector/node-rest/src/restResult.js +++ b/src/connector/node-rest/src/restResult.js @@ -1,7 +1,8 @@ import {getTaoType} from '../src/restConstant' + export class TDengineRestResultSet { - constructor(jason) { + constructor(result) { this.status = '' //succ this.column_name = {} //head this.column_type = {} //column_meta @@ -9,7 +10,7 @@ export class TDengineRestResultSet { this.affectRows = null //rows this.code = null this.desc = null - this._init(jason) + this._init(result) } //initial the resultSet with a jason parameter @@ -17,27 +18,27 @@ export class TDengineRestResultSet { * * @param jason */ - _init(jason) { - if (jason.status) { - this.status = jason.status + _init(result) { + if (result.status) { + this.status = result.status } - if (jason.head) { - this.column_name = jason.head + if (result.head) { + this.column_name = result.head } - if (jason.column_meta) { - this.column_type = jason.column_meta + if (result.column_meta) { + this.column_type = result.column_meta } - if (jason.data) { - this.data = jason.data + if (result.data) { + this.data = result.data } - if (jason.rows) { - this.affectRows = jason.rows + if (result.rows) { + this.affectRows = result.rows } - if (jason.code) { - this.code = jason.code + if (result.code) { + this.code = result.code } - if (jason.desc) { - this.desc = jason.desc + if (result.desc) { + this.desc = result.desc } } @@ -95,7 +96,7 @@ export class TDengineRestResultSet { if ((fields[i][1]) == 8 || (fields[i][1]) == 10) { colSize.push(Math.max(fields[i][0].length, fields[i][2])); //max(column_name.length,column_type_precision) } else { - colSize.push(Math.max(fields[i][0].length, this._suggestedMinWidths[fields[i][1]]));// max(column_name.length,suggest_column_with_suggestion) + colSize.push(Math.max(fields[i][0].length, suggestedMinWidths[fields[i][1]]));// max(column_name.length,suggest_column_with_suggestion) } // console.log(colSize) } @@ -122,20 +123,6 @@ export class TDengineRestResultSet { return colStr } - _suggestedMinWidths = { - 0: 4, - 1: 4, - 2: 4, - 3: 6, - 4: 11, - 5: 12, - 6: 24, - 7: 24, - 8: 10, - 9: 25, - 10: 10, - } - _fillEmpty(n) { let str = ""; for (let i = 0; i < n; i++) { @@ -151,4 +138,18 @@ export class TDengineRestResultSet { } return f; } -} \ No newline at end of file +} + +const suggestedMinWidths = { + 0: 4, + 1: 4, + 2: 4, + 3: 6, + 4: 11, + 5: 12, + 6: 24, + 7: 24, + 8: 10, + 9: 25, + 10: 10, +} diff --git a/src/connector/node-rest/test/testRestConn.js b/src/connector/node-rest/test/testRestConn.js index c866d72b5c..6abad75ab3 100644 --- a/src/connector/node-rest/test/testRestConn.js +++ b/src/connector/node-rest/test/testRestConn.js @@ -1,23 +1,23 @@ import {TDRestConnection} from "../TDengineRest"; -let conn = new TDRestConnection({host: 'u195'}); +let conn = new TDRestConnection({host: 'u195', user: 'root', pass: 'taosdata', port: 6041}); let cursor = conn.cursor(); -(async () => { - result = await cursor.query("drop database if exists node_rest"); - result.toString() -})() +// (async () => { +// result = await cursor.query("drop database if exists node_rest").catch(e=>console.log(e)) +// result.toString() +// })() const createDB = "create database if not exists node_rest"; const dropDB = "drop database if exists node_rest"; -const createTBL = "CREATE STABLE if not exists node_rest.meters (ts timestamp, current float, voltage int, phase float) TAGS (location binary(64), groupId int)"; +const createTBL = "CREATE STABLE if not exists node_rest.meters3 (ts timestamp, current float, voltage int, phase float) TAGS (location binary(64), groupId int)"; const dropTBL = "drop table if exists node_rest.meters "; const insert = "INSERT INTO node_rest.d1001 USING node_rest.meters TAGS (\"Beijng.Chaoyang\", 2) VALUES (now, 10.2, 219, 0.32) "; const select = "select * from node_rest.d1001 "; const selectStbl = "select * from node_rest.meters"; async function execute(sql) { - console.log(sql); + console.log("SQL:" + sql); result = await cursor.query(sql); result.toString() }; -- GitLab