提交 2af021cd 编写于 作者: X xialei_li

fix some description

上级 bf81b8b1
import {TDengineRestConnection} from "../src/restConnect"; import {TDengineRestConnection} from "../src/restConnect";
let conn = new TDengineRestConnection({host: 'u195'}) let conn = new TDengineRestConnection({host: 'u195', user: 'root', pass: 'taosdata', port: 6041})
let cursor = conn.cursor(); let cursor = conn.cursor();
console.log(conn)
let data = {};
(async () => { (async () => {
data = await cursor.query("select * from test.meters limit 10").catch(e => console.log(e)); data = await cursor.query("show databases");
data.toString() data.toString()
})() })()
......
{ {
"name": "taos-rest", "name": "td-rest-connector",
"version": "1.0.0", "version": "1.0.0",
"module": "src/index.js", "description": "A Node.js connector for TDengine restful",
"main": "lib/index.js", "module": "src/TDengineRest.js",
"main": "lib/TDengineclearRest.js",
"license": "MIT", "license": "MIT",
"scripts": { "scripts": {
"prepare": "npm run build", "prepare": "npm run build",
"build": "esbuild --bundle --platform=node --outfile=lib/index.js src/index.js", "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-databases.js --watch" "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": { "devDependencies": {
"esbuild": "^0.12.25", "esbuild": "^0.12.25",
......
...@@ -4,35 +4,32 @@ This is the Node.js library that lets you connect to [TDengine](https://www.gith ...@@ -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. restful. This restful can help you access the TDengine from different platform.
## Install ## Install
To get started, just type in the following to install the connector through [npm](https://www.npmjs.com/)
### On Linux ```cmd
npm install td-rest-connector
### On macOs ```
### On Windows
## Usage ## Usage
### Connection ### Connection
```javascript ```javascript
import taoRest from '' import taoRest from 'TDengineRest'
var connRest = taoRest({}) var connRest = taoRest({host:'127.0.0.1',user:'root',pass:'taosdata',port:6041})
```
close a connection
```javascript
connRest.close()
``` ```
query query
```javascript ```javascript
let data = connRest.Query("sql") (async()=>{
console.log ("data:"+data) data = await connRest.query("show databases");
data.toString();
}
)()
``` ```
## Example ## 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 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)
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)
## Contributing to TDengine ## Contributing to TDengine
......
import {TDengineRestCursor} from '../src/restCursor' import {TDengineRestCursor} from '../src/restCursor'
/** /**
* *this class collect basic information that can be used to build
* a restful connection.
*/ */
export class TDengineRestConnection { export class TDengineRestConnection {
/**
* constructor,give variables some default values
* @param options
* @returns {TDengineRestConnection}
*/
constructor(options) { constructor(options) {
this.host = 'localhost' this.host = 'localhost'
this.port = '6041' this.port = '6041'
...@@ -15,7 +21,7 @@ export class TDengineRestConnection { ...@@ -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 * @param options
* @private * @private
*/ */
...@@ -37,9 +43,13 @@ export class TDengineRestConnection { ...@@ -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() { cursor() {
return new TDengineRestCursor(this) return new TDengineRestCursor(this)
console.log("return a cursor object user query sql")
} }
} }
......
/**
* 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 = { export const typeCodesToName = {
0: 'Null', 0: 'Null',
1: 'Boolean', 1: 'Boolean',
...@@ -12,6 +16,11 @@ export const typeCodesToName = { ...@@ -12,6 +16,11 @@ export const typeCodesToName = {
10: 'Nchar', 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) { export function getTaoType(typecode) {
return typeCodesToName[typecode]; return typeCodesToName[typecode];
} }
\ No newline at end of file
import fetch from 'node-fetch' import fetch from 'node-fetch'
import {TDengineRestResultSet} from '../src/restResult' 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 { export class TDengineRestCursor {
/**
* constructor,used to get the connection info
* @param connection
*/
constructor(connection) { constructor(connection) {
this._connection = null; this._connection = null;
this.data = []; this.data = [];
...@@ -13,15 +22,29 @@ export class TDengineRestCursor { ...@@ -13,15 +22,29 @@ export class TDengineRestCursor {
} }
} }
/**
* used to build an url,like http://localhost:6041/rest/sql
* @returns {string}
* @private
*/
_apiUpl() { _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 return (this.http ? "https" : "http") + "://" + this._connection.host + ":" + this._connection.port + this._connection.path
} }
/**
* used to make an authorization token
* @returns {string}
* @private
*/
_token() { _token() {
return 'Basic ' + Buffer.from(this._connection.user + ":" + this._connection.pass).toString('base64') 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<TDengineRestResultSet>}
*/
async query(sql) { async query(sql) {
try { try {
let response = await fetch(this._apiUpl(), { let response = await fetch(this._apiUpl(), {
......
import {getTaoType} from '../src/restConstant' import {getTaoType} from '../src/restConstant'
export class TDengineRestResultSet { export class TDengineRestResultSet {
constructor(jason) { constructor(result) {
this.status = '' //succ this.status = '' //succ
this.column_name = {} //head this.column_name = {} //head
this.column_type = {} //column_meta this.column_type = {} //column_meta
...@@ -9,7 +10,7 @@ export class TDengineRestResultSet { ...@@ -9,7 +10,7 @@ export class TDengineRestResultSet {
this.affectRows = null //rows this.affectRows = null //rows
this.code = null this.code = null
this.desc = null this.desc = null
this._init(jason) this._init(result)
} }
//initial the resultSet with a jason parameter //initial the resultSet with a jason parameter
...@@ -17,27 +18,27 @@ export class TDengineRestResultSet { ...@@ -17,27 +18,27 @@ export class TDengineRestResultSet {
* *
* @param jason * @param jason
*/ */
_init(jason) { _init(result) {
if (jason.status) { if (result.status) {
this.status = jason.status this.status = result.status
} }
if (jason.head) { if (result.head) {
this.column_name = jason.head this.column_name = result.head
} }
if (jason.column_meta) { if (result.column_meta) {
this.column_type = jason.column_meta this.column_type = result.column_meta
} }
if (jason.data) { if (result.data) {
this.data = jason.data this.data = result.data
} }
if (jason.rows) { if (result.rows) {
this.affectRows = jason.rows this.affectRows = result.rows
} }
if (jason.code) { if (result.code) {
this.code = jason.code this.code = result.code
} }
if (jason.desc) { if (result.desc) {
this.desc = jason.desc this.desc = result.desc
} }
} }
...@@ -95,7 +96,7 @@ export class TDengineRestResultSet { ...@@ -95,7 +96,7 @@ export class TDengineRestResultSet {
if ((fields[i][1]) == 8 || (fields[i][1]) == 10) { 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) colSize.push(Math.max(fields[i][0].length, fields[i][2])); //max(column_name.length,column_type_precision)
} else { } 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) // console.log(colSize)
} }
...@@ -122,20 +123,6 @@ export class TDengineRestResultSet { ...@@ -122,20 +123,6 @@ export class TDengineRestResultSet {
return colStr 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) { _fillEmpty(n) {
let str = ""; let str = "";
for (let i = 0; i < n; i++) { for (let i = 0; i < n; i++) {
...@@ -151,4 +138,18 @@ export class TDengineRestResultSet { ...@@ -151,4 +138,18 @@ export class TDengineRestResultSet {
} }
return f; 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,
}
import {TDRestConnection} from "../TDengineRest"; 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(); let cursor = conn.cursor();
(async () => { // (async () => {
result = await cursor.query("drop database if exists node_rest"); // result = await cursor.query("drop database if exists node_rest").catch(e=>console.log(e))
result.toString() // result.toString()
})() // })()
const createDB = "create database if not exists node_rest"; const createDB = "create database if not exists node_rest";
const dropDB = "drop database if 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 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 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 select = "select * from node_rest.d1001 ";
const selectStbl = "select * from node_rest.meters"; const selectStbl = "select * from node_rest.meters";
async function execute(sql) { async function execute(sql) {
console.log(sql); console.log("SQL:" + sql);
result = await cursor.query(sql); result = await cursor.query(sql);
result.toString() result.toString()
}; };
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册