node-example.js 4.1 KB
Newer Older
S
StoneT2000 已提交
1 2
/* This example is to show the preferred way to use the td-connector */
/* To run, enter node path/to/node-example.js */
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
// Get the td-connector package
const taos = require('td-connector');

/* 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
 * port: the port we connect through
 */
var conn = taos.connect({host:"127.0.0.1", user:"root", password:"taosdata", config:"/etc/taos",port:0});

// Initialize our TDengineCursor, which we use to interact with TDengine
var c1 = conn.cursor();

S
StoneT2000 已提交
22
// c1.query(query) will return a TaosQuery object, of which then we can execute. The execute function then returns a promise
23 24
// Let's create a database named db
try {
S
StoneT2000 已提交
25 26
  var query = c1.query('create database db;');
  query.execute();
27 28 29 30 31 32
}
catch(err) {
  conn.close();
  throw err;
}

S
StoneT2000 已提交
33 34
// Now we will use database db. As this query won't return any results,
// we can simplify the code and directly use the c1.execute() function. No need for a TaosQuery object to wrap around the query
35 36 37 38 39 40 41 42 43 44
try {
  c1.execute('use db;');
}
catch (err) {
  conn.close();
  throw err;
}

// Let's create a table called weather
// which stores some weather data like humidity, AQI (air quality index), temperature, and some notes as text
S
StoneT2000 已提交
45 46
// We can also immedietely execute a TaosQuery object by passing true as the secodn argument
// This will then return a promise that we can then attach a callback function to
47
try {
S
StoneT2000 已提交
48 49 50 51 52 53
  var promise = c1.query('create table if not exists weather (ts timestamp, humidity smallint, aqi int, temperature float, notes binary(30));', true);
  promise.then(function(){
    console.log("Table created!");
  }).catch(function() {
    console.log("Table couldn't be created.")
  });
54 55 56 57 58 59 60
}
catch (err) {
  conn.close();
  throw err;
}

// Let's get the description of the table weather
S
StoneT2000 已提交
61 62
// When using a TaosQuery object and then executing it, upon success it returns a TaosResult object, which is a wrapper around the
// retrieved data and allows us to easily access data and manipulate or display it.
63
try {
S
StoneT2000 已提交
64 65 66 67
  c1.query('describe db.weather;').execute().then(function(result){
    // Result is an instance of TaosResult and has the function pretty() which instantly logs a prettified version to the console
    result.pretty();
  });
68 69 70 71 72 73 74
}
catch (err) {
  conn.close();
  throw err;
}

// Let's try to insert some random generated data to test with
S
StoneT2000 已提交
75 76
// We will use the bind function of the TaosQuery object to easily bind values to question marks in the query
// For Timestamps, a normal Datetime object or TaosTimestamp or milliseconds can be passed in through the bind function
77 78 79
let stime = new Date();
let interval = 1000;
try {
S
StoneT2000 已提交
80
  for (let i = 0; i < 1000; i++) {
81
    stime.setMilliseconds(stime.getMilliseconds() + interval);
S
StoneT2000 已提交
82
    let insertData = [stime,
83 84 85 86
                      parseInt(Math.random()*100),
                      parseInt(Math.random()*300),
                      parseFloat(Math.random()*10 + 30),
                      "\"random note!\""];
S
StoneT2000 已提交
87 88 89
    //c1.execute('insert into db.weather values(' + insertData.join(',') + ' );');
    var query = c1.query('insert into db.weather values(?, ?, ?, ?, ?);').bind(insertData);
    query.execute();
90 91 92 93 94 95 96 97 98 99
  }
}
catch (err) {
  conn.close();
  throw err;
}

// Now let's look at our newly inserted data
var retrievedData;
try {
S
StoneT2000 已提交
100 101 102 103
  c1.query('select * from db.weather limit 5 offset 100;', true).then(function(result){
    result.pretty();
    // Neat!
  });
104 105 106 107 108 109 110 111 112

}
catch (err) {
  conn.close();
  throw err;
}

// Let's try running some basic functions
try {
S
StoneT2000 已提交
113 114 115 116
  c1.query('select count(*), avg(temperature), max(temperature), min(temperature), stddev(temperature) from db.weather;', true)
  .then(function(result) {
    result.pretty();
  })
117 118 119 120 121 122 123 124 125
}
catch(err) {
  conn.close();
  throw err;
}

conn.close();

// Feel free to fork this repository or copy this code and start developing your own apps and backends with NodeJS and TDengine!