readme.md 7.4 KB
Newer Older
1
# TDengine Node.js connector
L
liu0x54 已提交
2
[![minzip](https://img.shields.io/bundlephobia/minzip/td2.0-connector.svg)](https://github.com/taosdata/TDengine/tree/master/src/connector/nodejs) [![NPM](https://img.shields.io/npm/l/td2.0-connector.svg)](https://github.com/taosdata/TDengine/#what-is-tdengine)
3

L
liu0x54 已提交
4
This is the Node.js library that lets you connect to [TDengine](https://www.github.com/taosdata/tdengine) 2.0 version. It is built so that you can use as much of it as you want or as little of it as you want through providing an extensive API. If you want the raw data in the form of an array of arrays for the row data retrieved from a table, you can do that. If you want to wrap that data with objects that allow you easily manipulate and display data such as using a prettifier function, you can do that!
5 6 7 8 9 10

## Installation

To get started, just type in the following to install the connector through [npm](https://www.npmjs.com/)

```cmd
L
liu0x54 已提交
11
npm install td2.0-connector
12 13
```

14
To interact with TDengine, we make use of the [node-gyp](https://github.com/nodejs/node-gyp) library. To install, you will need to install the following depending on platform (the following instructions are quoted from node-gyp)
15

L
liu0x54 已提交
16
### On Linux
17 18 19 20

- `python` (`v2.7` recommended, `v3.x.x` is **not** supported)
- `make`
- A proper C/C++ compiler toolchain, like [GCC](https://gcc.gnu.org)
L
liu0x54 已提交
21
- `node` (between `v10.x` and `v11.x`, other version has some dependency compatibility problems)
22 23 24 25 26 27 28

### On macOS

- `python` (`v2.7` recommended, `v3.x.x` is **not** supported) (already installed on macOS)

- Xcode

29
  - You also need to install the
30 31 32 33 34

    ```
    Command Line Tools
    ```

35
     via Xcode. You can find this under the menu
36 37 38 39 40

    ```
    Xcode -> Preferences -> Locations
    ```

41
     (or by running
42 43 44 45 46

    ```
    xcode-select --install
    ```

47
     in your Terminal)
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70

    - This step will install `gcc` and the related toolchain containing `make`

### On Windows

#### Option 1

Install all the required tools and configurations using Microsoft's [windows-build-tools](https://github.com/felixrieseberg/windows-build-tools) using `npm install --global --production windows-build-tools` from an elevated PowerShell or CMD.exe (run as Administrator).

#### Option 2

Install tools and configuration manually:

- Install Visual C++ Build Environment: [Visual Studio Build Tools](https://visualstudio.microsoft.com/thank-you-downloading-visual-studio/?sku=BuildTools) (using "Visual C++ build tools" workload) or [Visual Studio 2017 Community](https://visualstudio.microsoft.com/pl/thank-you-downloading-visual-studio/?sku=Community) (using the "Desktop development with C++" workload)
- Install [Python 2.7](https://www.python.org/downloads/) (`v3.x.x` is not supported), and run `npm config set python python2.7` (or see below for further instructions on specifying the proper Python version and path.)
- Launch cmd, `npm config set msvs_version 2017`

If the above steps didn't work for you, please visit [Microsoft's Node.js Guidelines for Windows](https://github.com/Microsoft/nodejs-guidelines/blob/master/windows-environment.md#compiling-native-addon-modules) for additional tips.

To target native ARM64 Node.js on Windows 10 on ARM, add the  components "Visual C++ compilers and libraries for ARM64" and "Visual  C++ ATL for ARM64".

## Usage

71 72
The following is a short summary of the basic usage of the connector, the  full api and documentation can be found [here](http://docs.taosdata.com/node)

S
StoneT2000 已提交
73 74
### Connection

L
liu0x54 已提交
75
To use the connector, first require the library ```td2.0-connector```. Running the function ```taos.connect``` with the connection options passed in as an object will return a TDengine connection object. The required connection option is ```host```, other options if not set, will be the default values as shown below.
76 77

A cursor also needs to be initialized in order to interact with TDengine from Node.js.
78 79

```javascript
L
liu0x54 已提交
80
const taos = require('td2.0-connector');
81
var conn = taos.connect({host:"127.0.0.1", user:"root", password:"taosdata", config:"/etc/taos",port:0})
S
StoneT2000 已提交
82
var cursor = conn.cursor(); // Initializing a new cursor
83 84
```

S
StoneT2000 已提交
85
Close a connection
86 87

```javascript
S
StoneT2000 已提交
88
conn.close();
89 90
```

S
StoneT2000 已提交
91 92 93
### Queries

We can now start executing simple queries through the ```cursor.query``` function, which returns a TaosQuery object.
94 95

```javascript
S
StoneT2000 已提交
96
var query = cursor.query('show databases;')
97 98
```

S
StoneT2000 已提交
99
We can get the results of the queries through the ```query.execute()``` function, which returns a promise that resolves with a TaosResult object, which contains the raw data and additional functionalities such as pretty printing the results.
100

S
StoneT2000 已提交
101 102 103 104 105 106
```javascript
var promise = query.execute();
promise.then(function(result) {
  result.pretty(); //logs the results to the console as if you were in the taos shell
});
```
107

S
StoneT2000 已提交
108
You can also query by binding parameters to a query by filling in the question marks in a string as so. The query will automatically parse what was binded and convert it to the proper format for use with TDengine
109
```javascript
110
var query = cursor.query('select * from meterinfo.meters where ts <= ? and areaid = ?;').bind(new Date(), 5);
S
StoneT2000 已提交
111 112 113 114
query.execute().then(function(result) {
  result.pretty();
})
```
115

S
StoneT2000 已提交
116 117
The TaosQuery object can also be immediately executed upon creation by passing true as the second argument, returning a promise instead of a TaosQuery.
```javascript
118
var promise = cursor.query('select * from meterinfo.meters where v1 = 30;', true)
119
promise.then(function(result) {
S
StoneT2000 已提交
120 121 122
  result.pretty();
})
```
123

S
StoneT2000 已提交
124 125
If you want to execute queries without objects being wrapped around the data, use ```cursor.execute()``` directly and ```cursor.fetchall()``` to retrieve data if there is any.
```javascript
126
cursor.execute('select count(*), avg(v1), min(v2) from meterinfo.meters where ts >= \"2019-07-20 00:00:00.000\";');
S
StoneT2000 已提交
127 128 129 130
var data = cursor.fetchall();
console.log(cursor.fields); // Latest query's Field metadata is stored in cursor.fields
console.log(cursor.data); // Latest query's result data is stored in cursor.data, also returned by fetchall.
```
131

S
StoneT2000 已提交
132
### Async functionality
133

134
Async queries can be performed using the same functions such as `cursor.execute`, `TaosQuery.query`, but now with `_a` appended to them.
135

136
Say you want to execute an two async query on two separate tables, using `cursor.query`, you can do that and get a TaosQuery object, which upon executing with the `execute_a` function, returns a promise that resolves with a TaosResult object.
137 138

```javascript
S
StoneT2000 已提交
139 140
var promise1 = cursor.query('select count(*), avg(v1), avg(v2) from meter1;').execute_a()
var promise2 = cursor.query('select count(*), avg(v1), avg(v2) from meter2;').execute_a();
141 142 143 144 145 146 147
promise1.then(function(result) {
  result.pretty();
})
promise2.then(function(result) {
  result.pretty();
})
```
S
StoneT2000 已提交
148 149 150 151 152 153

## Example

An example of using the NodeJS 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/nodejs/node-example.js) (The preferred method for using the connector)

An example of using the NodeJS 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)
154 155 156 157 158 159 160

## Contributing to TDengine

Please follow the [contribution guidelines](https://github.com/taosdata/TDengine/blob/master/CONTRIBUTING.md) to contribute to the project.

## License

S
StoneT2000 已提交
161
[GNU AGPL v3.0](http://www.gnu.org/licenses/agpl-3.0.html)