08-node.mdx 10.7 KB
Newer Older
1 2 3 4 5 6 7 8 9
---
toc_max_heading_level: 4
sidebar_label: Node.js
title: TDengine Node.js Connector
---

import Tabs from "@theme/Tabs";
import TabItem from "@theme/TabItem";

D
danielclow 已提交
10
import Preparition from "./_preparition.mdx";
11 12 13 14 15 16
import NodeInsert from "../../07-develop/03-insert-data/_js_sql.mdx";
import NodeInfluxLine from "../../07-develop/03-insert-data/_js_line.mdx";
import NodeOpenTSDBTelnet from "../../07-develop/03-insert-data/_js_opts_telnet.mdx";
import NodeOpenTSDBJson from "../../07-develop/03-insert-data/_js_opts_json.mdx";
import NodeQuery from "../../07-develop/04-query-data/_js.mdx";

D
danielclow 已提交
17
`@tdengine/client` and `@tdengine/rest` are the official Node.js connectors. Node.js developers can develop applications to access TDengine instance data. Note: The connectors for TDengine 3.0 are different than those for TDengine 2.x. The new connectors do not support TDengine 2.x.
18

D
danielclow 已提交
19
`@tdengine/client` is **native connection**, which connects to TDengine instances natively through the TDengine client driver (taosc), supporting data writing, querying, subscriptions, schemaless writing, and bind interface. `@tdengine/rest` is the **REST connection**, which connects to TDengine instances via the REST interface provided by taosAdapter. The REST connector can run on any platform, but performance is slightly degraded, and the interface implements a somewhat different set of functional features than the native interface.
20

D
danielclow 已提交
21
The source code for the Node.js connectors is located on [GitHub](https://github.com/taosdata/taos-connector-node/tree/3.0).
22

D
danielclow 已提交
23
## Supported platforms
24 25 26 27 28 29 30 31 32 33 34 35

The platforms supported by the native connector are the same as those supported by the TDengine client driver.
The REST connector supports all platforms that can run Node.js.

## Version support

Please refer to [version support list](/reference/connector#version-support)

## Supported features

### Native connectors

D
danielclow 已提交
36 37 38 39 40
1. Connection Management
2. General Query
3. Continuous Query
4. Parameter Binding
5. Subscription
41 42 43 44
6. Schemaless

### REST Connector

D
danielclow 已提交
45 46 47
1. Connection Management
2. General Query
3. Continuous Query
48

D
danielclow 已提交
49
## Installation Steps
50

D
danielclow 已提交
51
### Pre-installation preparation
52 53 54 55 56 57 58 59

- Install the Node.js development environment
- If you are using the REST connector, skip this step. However, if you use the native connector, please install the TDengine client driver. Please refer to [Install Client Driver](/reference/connector#Install-Client-Driver) for more details. We use [node-gyp](https://github.com/nodejs/node-gyp) to interact with TDengine instances and also need to install some dependencies mentioned below depending on the specific OS.

<Tabs defaultValue="Linux">
<TabItem value="Linux" label="Linux system installation dependencies">

- `python` (recommended for `v2.7` , `v3.x.x` currently not supported)
D
danielclow 已提交
60
- `@tdengine/client` 3.0.0 supports Node.js LTS v10.9.0 or later and Node.js LTS v12.8.0 or later. Older versions may be incompatible.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
- `make`
- C compiler, [GCC](https://gcc.gnu.org) v4.8.5 or higher

</TabItem>
<TabItem value="Windows" label="Windows system installation dependencies">

- Installation method 1

Use Microsoft's [windows-build-tools](https://github.com/felixrieseberg/windows-build-tools) to execute `npm install --global --production` from the `cmd` command-line interface to install all the necessary tools.

- Installation method 2

Manually install the following tools.

- Install Visual Studio related: [Visual Studio Build Tools](https://visualstudio.microsoft.com/thank-you-downloading-visual-studio/?sku=BuildTools) or [Visual Studio 2017 Community](https://visualstudio.microsoft.com/pl/thank-you-downloading-visual-studio/?sku=Community)
- Install [Python](https://www.python.org/downloads/) 2.7 (`v3.x.x` is not supported) and execute `npm config set python python2.7`.
- Go to the `cmd` command-line interface, `npm config set msvs_version 2017`

79
Refer to Microsoft's Node.js User Manual [Microsoft's Node.js Guidelines for Windows](https://github.com/Microsoft/nodejs-guidelines/blob/master/windows-environment.md#compiling-native-addon-modules).
80 81 82 83 84 85 86 87 88 89 90 91

If using ARM64 Node.js on Windows 10 ARM, you must add "Visual C++ compilers and libraries for ARM64" and "Visual C++ ATL for ARM64".

</TabItem>
</Tabs>

### Install via npm

<Tabs defaultValue="install_native">
<TabItem value="install_native" label="Install native connector">

```bash
D
danielclow 已提交
92
npm install @tdengine/client
93 94 95 96 97 98
```

</TabItem>
<TabItem value="install_rest" label="Install REST connector">

```bash
D
danielclow 已提交
99
npm install @tdengine/rest
100 101 102 103 104
```

</TabItem>
</Tabs>

D
danielclow 已提交
105
### Verify
106 107 108 109 110

After installing the TDengine client, use the `nodejsChecker.js` program to verify that the current environment supports Node.js access to TDengine.

Verification in details:

D
danielclow 已提交
111
- Create an installation test folder such as `~/tdengine-test`. Download the [nodejsChecker.js source code](https://github.com/taosdata/taos-connector-node/blob/3.0/nodejs/examples/nodejsChecker.js) to your local machine.
112 113 114 115 116

- Execute the following command from the command-line.

```bash
npm init -y
D
danielclow 已提交
117
npm install @tdengine/client
118 119 120 121 122 123 124 125 126 127
node nodejsChecker.js host=localhost
```

- After executing the above steps, the command-line will output the result of `nodejsChecker.js` connecting to the TDengine instance and performing a simple insert and query.

## Establishing a connection

Please choose to use one of the connectors.

<Tabs defaultValue="native">
D
danielclow 已提交
128
<TabItem value="native" label="native connection">
129

D
danielclow 已提交
130
Install and import the `@tdengine/client` package.
131 132 133

```javascript
//A cursor also needs to be initialized in order to interact with TDengine from Node.js.
D
danielclow 已提交
134
const taos = require("@tdengine/client");
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
var conn = taos.connect({
  host: "127.0.0.1",
  user: "root",
  password: "taosdata",
  config: "/etc/taos",
  port: 0,
});
var cursor = conn.cursor(); // Initializing a new cursor

//Close a connection
conn.close();
```

</TabItem>
<TabItem value="rest" label="REST connection">

D
danielclow 已提交
151
Install and import the `@tdengine/rest` package.
152 153 154

```javascript
//A cursor also needs to be initialized in order to interact with TDengine from Node.js.
D
danielclow 已提交
155 156
import { options, connect } from "@tdengine/rest";
options.path = "/rest/sql";
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
// set host
options.host = "localhost";
// set other options like user/passwd

let conn = connect(options);
let cursor = conn.cursor();
```

</TabItem>
</Tabs>

## Usage examples

### Write data

D
danielclow 已提交
172
#### SQL Write
173 174 175

<NodeInsert />

D
danielclow 已提交
176
#### InfluxDB line protocol write
177 178 179

<NodeInfluxLine />

D
danielclow 已提交
180
#### OpenTSDB Telnet line protocol write
181 182 183

<NodeOpenTSDBTelnet />

D
danielclow 已提交
184
#### OpenTSDB JSON line protocol write
185 186 187

<NodeOpenTSDBJson />

D
danielclow 已提交
188
### Querying data
189 190 191

<NodeQuery />

D
danielclow 已提交
192 193

## More sample programs
194 195 196

| Sample Programs | Sample Program Description |
| --------------------------------------------------------------------------------------------------------------------------------- --------- | -------------------------------------- |
D
danielclow 已提交
197 198 199 200 201 202 203 204 205 206 207 208
| [basicUse](https://github.com/taosdata/taos-connector-node/blob/3.0/nodejs/examples/queryExample.js)                                  | Basic operations such as establishing connections and running SQl commands.                       |
| [stmtBindBatch](https://github.com/taosdata/taos-connector-node/blob/3.0/nodejs/examples/bindParamBatch.js)                  | Binding multi-line parameter insertion.               | |
| [stmtBindSingleParamBatch](https://github.com/taosdata/taos-connector-node/blob/3.0/nodejs/examples/bindSingleParamBatch.js) | Columnar binding parameter insertion               |
| [stmtQuery](https://github.com/taosdata/taos-connector-node/blob/3.0/nodejs/examples/stmtQuery.js)                       | Binding parameter query                   |
| [schemless insert](https://github.com/taosdata/taos-connector-node/blob/3.0/nodejs/examples/schemaless.js)                   | Schemaless insert                 |
| [TMQ](https://github.com/taosdata/taos-connector-node/blob/3.0/nodejs/examples/tmq.js)                                 | Using data subscription                       |
| [asyncQuery](https://github.com/taosdata/taos-connector-node/blob/3.0/nodejs/examples/asyncQueryExample.js)                                         | Using asynchronous queries                   |
| [REST](https://github.com/taosdata/taos-connector-node/blob/3.0/typescript-rest/example/example.ts)                                    | Using TypeScript with the REST connector |

## Usage limitations

`@tdengine/client` 3.0.0 supports Node.js LTS v12.8.0 to 12.9.1 and 10.9.0 to 10.20.0.
209 210 211 212 213 214 215 216 217 218 219 220 221





## Frequently Asked Questions

1. Using REST connections requires starting taosadapter.

   ```bash
   sudo systemctl start taosadapter
   ```

D
danielclow 已提交
222 223 224 225 226
2. Node.js versions

   `@tdengine/client` supports Node.js v10.9.0 to 10.20.0 and 12.8.0 to 12.9.1.

3. "Unable to establish connection", "Unable to resolve FQDN"
227

228
  Usually, the root cause is an incorrect FQDN configuration. You can refer to this section in the [FAQ](https://docs.tdengine.com/2.4/train-faq/faq/#2-how-to-handle-unable-to-establish-connection) to troubleshoot.
229

D
danielclow 已提交
230
## Important update records
231 232 233

### Native connectors

D
danielclow 已提交
234 235 236 237 238 239
| package name     | version | TDengine version    | Description                                                             |
|------------------|---------|---------------------|------------------------------------------------------------------|
| @tdengine/client | 3.0.0   | 3.0.0               | Supports TDengine 3.0. Not compatible with TDengine 2.x.                                                          |
| td2.0-connector  | 2.0.12  | 2.4.x;2.5.x;2.6.x | Fixed cursor.close() bug.                                 |
| td2.0-connector  | 2.0.11  | 2.4.x;2.5.x;2.6.x | Supports parameter binding, JSON tags and schemaless interface                  |
| td2.0-connector  | 2.0.10  | 2.4.x;2.5.x;2.6.x | Supports connection management, standard queries, connection queries, system information, and data subscription |
240 241
### REST Connector

D
danielclow 已提交
242 243 244 245 246 247 248
| package name         | version | TDengine version    | Description                                                                      |
|----------------------|---------|---------------------|---------------------------------------------------------------------------|
| @tdengine/rest | 3.0.0   | 3.0.0               | Supports TDengine 3.0. Not compatible with TDengine 2.x.                                               |
| td2.0-rest-connector | 1.0.7   | 2.4.x;2.5.x;2.6.x | Removed default port 6041。                                                       |
| td2.0-rest-connector | 1.0.6   | 2.4.x;2.5.x;2.6.x | Fixed affectRows bug with create, insert, update, and alter. |
| td2.0-rest-connector | 1.0.5   | 2.4.x;2.5.x;2.6.x | Support cloud token                                                  |
| td2.0-rest-connector  | 1.0.3  | 2.4.x;2.5.x;2.6.x | Supports connection management, standard queries, system information, error information, and continuous queries          |
249 250 251

## API Reference

D
danielclow 已提交
252
[API Reference](https://docs.taosdata.com/api/td2.0-connector/)