08-node.mdx 11.1 KB
Newer Older
1 2
---
title: TDengine Node.js Connector
D
danielclow 已提交
3 4 5
sidebar_label: Node.js
description: This document describes the TDengine Node.js connector.
toc_max_heading_level: 4
6 7 8 9 10
---

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

G
gccgdb1234 已提交
11
import Preparition from "./_preparation.mdx";
12 13 14 15 16 17
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 已提交
18
`@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.
19

D
danielclow 已提交
20
`@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.
21

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

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

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 已提交
37 38 39 40 41
1. Connection Management
2. General Query
3. Continuous Query
4. Parameter Binding
5. Subscription
42 43 44 45
6. Schemaless

### REST Connector

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

D
danielclow 已提交
50
## Installation Steps
51

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

- 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 已提交
61
- `@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.
62
- `make`
63
- C compiler, [GCC](https://gcc.gnu.org) v4.8.5 or later.
64 65

</TabItem>
66 67 68 69 70 71 72 73 74 75

<TabItem value="macOS" label="macOS installation dependencies">

- `python` (recommended for `v2.7` , `v3.x.x` currently not supported)
- `@tdengine/client` 3.0.0 currently supports Node.js from v12.22.12, but only later versions of v12. Other versions may be incompatible.
- `make`
- C compiler, [GCC](https://gcc.gnu.org) v4.8.5 or later.

</TabItem>

76 77 78 79 80 81 82 83 84 85 86 87 88 89
<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`

90
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).
91 92 93 94 95 96 97 98

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

99
<Tabs defaultValue="install_rest">
100 101 102
<TabItem value="install_native" label="Install native connector">

```bash
D
danielclow 已提交
103
npm install @tdengine/client
104 105 106 107 108 109
```

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

```bash
D
danielclow 已提交
110
npm install @tdengine/rest
111 112 113 114 115
```

</TabItem>
</Tabs>

D
danielclow 已提交
116
### Verify
117 118 119 120 121

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 已提交
122
- 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.
123 124 125 126 127

- Execute the following command from the command-line.

```bash
npm init -y
D
danielclow 已提交
128
npm install @tdengine/client
129 130 131 132 133 134 135 136 137
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.

138
<Tabs defaultValue="rest">
D
danielclow 已提交
139
<TabItem value="native" label="native connection">
140

D
danielclow 已提交
141
Install and import the `@tdengine/client` package.
142 143 144

```javascript
//A cursor also needs to be initialized in order to interact with TDengine from Node.js.
D
danielclow 已提交
145
const taos = require("@tdengine/client");
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
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 已提交
162
Install and import the `@tdengine/rest` package.
163 164 165

```javascript
//A cursor also needs to be initialized in order to interact with TDengine from Node.js.
D
danielclow 已提交
166 167
import { options, connect } from "@tdengine/rest";
options.path = "/rest/sql";
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
// 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 已提交
183
#### SQL Write
184 185 186

<NodeInsert />

D
danielclow 已提交
187
#### InfluxDB line protocol write
188 189 190

<NodeInfluxLine />

D
danielclow 已提交
191
#### OpenTSDB Telnet line protocol write
192 193 194

<NodeOpenTSDBTelnet />

D
danielclow 已提交
195
#### OpenTSDB JSON line protocol write
196 197 198

<NodeOpenTSDBJson />

D
danielclow 已提交
199
### Querying data
200 201 202

<NodeQuery />

D
danielclow 已提交
203 204

## More sample programs
205 206 207

| Sample Programs | Sample Program Description |
| --------------------------------------------------------------------------------------------------------------------------------- --------- | -------------------------------------- |
D
danielclow 已提交
208 209 210 211 212 213 214 215 216 217 218 219
| [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.
220 221 222 223 224 225 226 227 228 229 230 231 232





## Frequently Asked Questions

1. Using REST connections requires starting taosadapter.

   ```bash
   sudo systemctl start taosadapter
   ```

D
danielclow 已提交
233 234 235 236 237
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"
238

239
  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.
240

D
danielclow 已提交
241
## Important update records
242 243 244

### Native connectors

D
danielclow 已提交
245 246 247 248 249 250
| 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 |
251 252
### REST Connector

D
danielclow 已提交
253 254 255 256 257 258 259
| 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          |
260 261 262

## API Reference

263
[API Reference](https://docs.taosdata.com/api/td2.0-connector/)