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

fix some description

上级 bf81b8b1
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();
console.log(conn)
let data = {};
(async () => {
data = await cursor.query("select * from test.meters limit 10").catch(e => console.log(e));
data = await cursor.query("show databases");
data.toString()
})()
......
{
"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",
......
......@@ -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
......
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")
}
}
......
/**
* 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
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<TDengineRestResultSet>}
*/
async query(sql) {
try {
let response = await fetch(this._apiUpl(), {
......
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,
}
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()
};
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册