node.mdx 10.8 KB
Newer Older
D
dingbo 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
---
toc_max_heading_level: 4
sidebar_position: 6
sidebar_label: Node.js
title: TDengine Node.js Connector
---

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

import Preparition from "./_preparition.mdx";
import NodeInsert from "../../04-develop/03-insert-data/_js_sql.mdx";
import NodeInfluxLine from "../../04-develop/03-insert-data/_js_line.mdx";
import NodeOpenTSDBTelnet from "../../04-develop/03-insert-data/_js_opts_telnet.mdx";
import NodeOpenTSDBJson from "../../04-develop/03-insert-data/_js_opts_json.mdx";
import NodeQuery from "../../04-develop/04-query-data/_js.mdx";
import NodeAsyncQuery from "../../04-develop/04-query-data/_js_async.mdx";

19
`td2.0-connector` and `td2.0-rest-connector` are the official Node.js language connectors for TDengine, through which Node.js developers can develop applications that can access TDengine cluster data.
D
dingbo 已提交
20

21
`td2.0-connector` is a **native connector** that connects to TDengine runtime instances via the TDengine client driver (taosc) and supports data writing, querying, subscriptions, schemaless interface, and parameter binding interface. The `td2.0-rest-connector` is a **REST connector** that connects to TDengine runtime 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.
D
dingbo 已提交
22

23
The Node.js connector source code is hosted on [GitHub](https://github.com/taosdata/taos-connector-node).
D
dingbo 已提交
24

25
## Supported Platforms
D
dingbo 已提交
26

27 28
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.
D
dingbo 已提交
29

30
## Version support
D
dingbo 已提交
31

32
Please refer to [version support list](/reference/connector#version support)
D
dingbo 已提交
33

34
## Supported features
D
dingbo 已提交
35

36
### Native connectors
D
dingbo 已提交
37

38 39 40 41 42
1. connection management
2. general query
3. continuous query
4. parameter binding
5. subscription function
D
dingbo 已提交
43 44
6. Schemaless

45
### REST Connector
D
dingbo 已提交
46

47 48 49
1. connection management
2. general queries
3. Continuous query
D
dingbo 已提交
50

51
## Installation steps
D
dingbo 已提交
52

53
### Pre-installation
D
dingbo 已提交
54

55 56
- 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.
D
dingbo 已提交
57 58

<Tabs defaultValue="Linux">
59
<TabItem value="Linux" label="Linux system installation dependencies">
D
dingbo 已提交
60

61 62
- `python` (recommended for `v2.7` , `v3.x.x` currently not supported)
- `td2.0-connector` 2.0.6 supports Node.js LTS v10.9.0 or later, Node.js LTS v12.8.0 or later; 2.0.5 and earlier support Node.js LTS v10.x versions. Other versions may have package compatibility issues
D
dingbo 已提交
63
- `make`
64
- C compiler, [GCC](https://gcc.gnu.org) v4.8.5 or higher
D
dingbo 已提交
65 66

</TabItem>
67
<TabItem value="Windows" label="Windows system installation dependencies">
D
dingbo 已提交
68

69
- Installation method 1
D
dingbo 已提交
70

71
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.
D
dingbo 已提交
72

73
- Installation method 2
D
dingbo 已提交
74

75
Manually install the following tools.
D
dingbo 已提交
76

77 78 79
- 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`
D
dingbo 已提交
80

81
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).
D
dingbo 已提交
82

83
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".
D
dingbo 已提交
84 85 86 87

</TabItem>
</Tabs>

88
### Install via npm
D
dingbo 已提交
89 90

<Tabs defaultValue="install_native">
91
<TabItem value="install_native" label="Install native connector">
D
dingbo 已提交
92 93 94 95 96 97

```bash
npm install td2.0-connector
```

</TabItem>
98
<TabItem value="install_rest" label="Install REST connector">
D
dingbo 已提交
99 100 101 102 103 104 105 106

```bash
npm i td2.0-rest-connector
```

</TabItem>
</Tabs>

107
### Installation verification
D
dingbo 已提交
108

109
After installing the TDengine client, use the nodejsChecker.js program to verify that the current environment supports Node.js access to TDengine.
D
dingbo 已提交
110

111
Validation method.
D
dingbo 已提交
112

113
- Create a new installation verification directory, e.g. `~/tdengine-test`, and download the [nodejsChecker.js source code](https://github.com/taosdata/TDengine/tree/develop/examples/nodejs/) from GitHub. nodejsChecker.js) to local.
D
dingbo 已提交
114

115
- Execute the following command from the command line.
D
dingbo 已提交
116 117 118 119 120 121 122

```bash
npm init -y
npm install td2.0-connector
node nodejsChecker.js host=localhost
```

123
- 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.
D
dingbo 已提交
124

125
## Establishing a connection
D
dingbo 已提交
126

127
Please choose to use one of the connectors.
D
dingbo 已提交
128 129

<Tabs defaultValue="native">
130
<TabItem value="native" label="Native connection">
D
dingbo 已提交
131

132
Install and refer to `td2.0-connector` package:
D
dingbo 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152

```javascript
//A cursor also needs to be initialized in order to interact with TDengine from Node.js.
const taos = require("td2.0-connector");
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 连接">

153
Install and refer to `td2.0-rest-connector`package:
D
dingbo 已提交
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169

```javascript
//A cursor also needs to be initialized in order to interact with TDengine from Node.js.
import { options, connect } from "td2.0-rest-connector";
options.path = "/rest/sqlt";
// set host
options.host = "localhost";
// set other options like user/passwd

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

</TabItem>
</Tabs>

170
## Usage examples
D
dingbo 已提交
171

172
### Write data
D
dingbo 已提交
173

174
#### SQL Writing
D
dingbo 已提交
175 176 177

<NodeInsert />

178
#### InfluxDB line protocol writing
D
dingbo 已提交
179 180 181

<NodeInfluxLine />

182
#### OpenTSDB Telnet line protocol writing
D
dingbo 已提交
183 184 185

<NodeOpenTSDBTelnet />

186
#### OpenTSDB JSON line protocol writing
D
dingbo 已提交
187 188 189

<NodeOpenTSDBJson />

190
### Query data
D
dingbo 已提交
191

192
#### Synchronous queries
D
dingbo 已提交
193 194 195

<NodeQuery />

196
#### asynchronous query
D
dingbo 已提交
197 198 199

<NodeAsyncQuery />

200
## More Sample Programs
D
dingbo 已提交
201

202 203 204 205 206 207 208 209 210 211 212 213 214 215
| Sample Programs | Sample Program Description |
| --------------------------------------------------------------------------------------------------------------------------------- --------- | -------------------------------------- |
| [connection](https://github.com/taosdata/taos-connector-node/tree/develop/nodejs/examples/cursorClose.js) | Example of establishing a connection.                       | [stmtBind]()
| [stmtBindBatch](https://github.com/taosdata/taos-connector-node/tree/develop/nodejs/examples/stmtBindParamBatchSample.js) | Example of binding a multi-line parameter Example of inserting.               | [stmtBindBatch]()
| [stmtBind](https://github.com/taosdata/taos-connector-node/tree/develop/nodejs/examples/stmtBindParamSample.js) | Example of a line-by-line bind parameter insertion.           | [stmtBind]()
| [stmtBindSingleParamBatch](https://github.com/taosdata/taos-connector-node/tree/develop/nodejs/examples/) stmtBindSingleParamBatchSample.js) | Example of binding parameters by column.               |
| [stmtUseResult](https://github.com/taosdata/taos-connector-node/tree/develop/nodejs/examples/stmtUseResultSample.js) | Example of a bound parameter query.                   | [stmtUseResult]()
| [json tag](https://github.com/taosdata/taos-connector-node/tree/develop/nodejs/examples/testJsonTag.js) | Example of using Json tag.                  | [json tag]()
| [Nanosecond](https://github.com/taosdata/taos-connector-node/tree/develop/nodejs/examples/testNanoseconds.js) | An example of using timestamps with nanosecond precision.         | [Nanosecond]()
| [Microsecond](https://github.com/taosdata/taos-connector-node/tree/develop/nodejs/examples/testMicroseconds.js) | Example of using microsecond timestamp.         | [schemless insert]()
| [schemless insert](https://github.com/taosdata/taos-connector-node/tree/develop/nodejs/examples/testSchemalessInsert.js) | schemless Example of a schemless insert.                 | [schemless insert]()
| [subscribe](https://github.com/taosdata/taos-connector-node/tree/develop/nodejs/examples/testSubscribe.js) | Example of using subscribe.                       | [schemless insert]()
| [asyncQuery](https://github.com/taosdata/taos-connector-node/tree/develop/nodejs/examples/tset.js) | An example of using asynchronous queries.                   | [asyncQuery]()
| [REST](https://github.com/taosdata/taos-connector-node/blob/develop/typescript-rest/example/example.ts) | An example of using TypeScript with REST connections. | [REST]()
D
dingbo 已提交
216

217
## Usage restrictions
D
dingbo 已提交
218

219
Node.js Connector >= v2.0.6 currently supports node versions >=v12.8.0 <= v12.9.1 || >=v10.20.0 <= v10.9.0; v10.x versions are supported in 2.0.5 and earlier, other versions may have package compatibility issues.
D
dingbo 已提交
220

221
## Other notes
D
dingbo 已提交
222

223
See [video tutorial](https://www.taosdata.com/blog/2020/11/11/1957.html) for the Node.js connector.
D
dingbo 已提交
224

225
## Frequently Asked Questions
D
dingbo 已提交
226

227
1. Using REST connections requires starting taosadapter.
D
dingbo 已提交
228 229 230 231 232

   ```bash
   sudo systemctl start taosadapter
   ```

233
2. Node.js version
D
dingbo 已提交
234

235
  Connector >v2.0.6 Currently compatible Node.js versions are: >=v10.20.0 <= v10.9.0 || >=v12.8.0 <= v12.9.1
D
dingbo 已提交
236

237
3. "Unable to establish connection", "Unable to resolve FQDN"
D
dingbo 已提交
238

239
  Usually, it is because the FQDN is not configured correctly. You can refer to [How to understand TDengine's FQDN thoroughly](https://www.taosdata.com/blog/2021/07/29/2741.html).
D
dingbo 已提交
240

241
## Important Updates
D
dingbo 已提交
242

243
### Native connectors
D
dingbo 已提交
244

245
| td2.0-connector version | description |
D
dingbo 已提交
246
| -------------------- | ---------------------------------------------------------------- |
247 248 249
| 2.0.12 | Fix bug with cursor.close() error. | 2.0.12 | Fix bug with cursor.close() error.
| 2.0.11 | Support for binding parameters, json tag, schemaless interface, etc.                  |
| 2.0.10 | Support connection management, general query, continuous query, get system information, subscribe function, etc. | ### REST Connector
D
dingbo 已提交
250

251
### REST Connector
D
dingbo 已提交
252

253
| td2.0-rest-connector version | Description |
D
dingbo 已提交
254
| ------------------------- | ---------------------------------------------------------------- |
255
| 1.0.3 | Support connection management, general query, get system information, error message, continuous query, etc. |# API Reference
D
dingbo 已提交
256

257
## API Reference
D
dingbo 已提交
258

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