This is the Node.js library that lets you connect to [TDengine](https://www.github.com/taosdata/tdengine).
This is the Node.js library that lets you connect to [TDengine](https://www.github.com/taosdata/tdengine). 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!
## Installation
...
...
@@ -67,164 +67,73 @@ To target native ARM64 Node.js on Windows 10 on ARM, add the components "Visual
## Usage
To use the connector, first request the library ```td-connector```. Running the function ```taos.connect``` with the connection options passed in as an object will return a TDengine connection object. A cursor needs to be intialized in order to interact with TDengine from node.
### Connection
To use the connector, first require the library ```td-connector```. Running the function ```taos.connect``` with the connection options passed in as an object will return a TDengine connection object. A cursor needs to be initialized in order to interact with TDengine from Node.js.
varcursor=conn.cursor();// Initializing a new cursor
```
We can now start executing queries through the ```cursor.execute``` function.
Close a connection
```javascript
c1.execute('show databases;')
conn.close();
```
We can get the results of the queries by doing the following
### Queries
We can now start executing simple queries through the ```cursor.query``` function, which returns a TaosQuery object.
```javascript
vardata=c1.fetchall();
console.log(c1.fieldNames);// Returns the names of the columns/fields
console.log(data);// Logs all the data from the query as an array of arrays, each of which represents a row and data[row_number] is sorted in order of the fields
varquery=cursor.query('show databases;')
```
## Example
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.
The following is an example use of the connector showing how to make a table with weather data, insert random data, and then retrieve it.
```javascript
varpromise=query.execute();
promise.then(function(result){
result.pretty();//logs the results to the console as if you were in the taos shell
});
```
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
```javascript
// Get the td-connector package
consttaos=require('td-connector');
varquery=cursor.query('select * from meterinfo.meters where ts <= ? and areaid = ?').bind(newDate(),5);
query.execute().then(function(result){
result.pretty();
})
```
/* We will connect to TDengine by passing an object comprised of connection options to taos.connect and store the
* connection to the variable conn
*/
/*
* Connection Options
* host: the host to connect to
* user: the use to login as
* password: the password for the above user to login
* config: the location of the taos.cfg file, by default it is in /etc/taos
c1.execute('insert into db.weather values('+insertData.join(',')+' );');
}
}
catch(err){
conn.close();
throwerr;
}
// Now let's look at our newly inserted data
varretrievedData;
try{
c1.execute('select * from db.weather;')
retrievedData=c1.fetchall();
// c1.fieldNames stores the names of each column retrieved
console.log(c1.fieldNames);
console.log(retrievedData);
// timestamps retrieved are always JS Date Objects
// Numbers are numbers, big ints are big ints, and strings are strings
}
catch(err){
conn.close();
throwerr;
}
// Let's try running some basic functions
try{
c1.execute('select count(*), avg(temperature), max(temperature), min(temperature), stddev(temperature) from db.weather;')
c1.fetchall();
console.log(c1.fieldNames);
console.log(c1.data);
}
catch(err){
conn.close();
throwerr;
}
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
varpromise=cursor.query('select * from meterinfo.meters where v1 = 30',true)
query.execute().then(function(result){
result.pretty();
})
```
conn.close();
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
cursor.execute('select count(*), avg(v1), min(v2) from meterinfo.meters where ts >= \"2019-07-20 00:00:00.000\"');
vardata=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.
```
// Feel free to fork this repository or copy this code and start developing your own apps and backends with NodeJS and TDengine!
### Async functionality
```
Coming soon
## 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)