nodejsChecker.js 1.6 KB
Newer Older
1
const taos = require("@tdengine/client");
2 3 4 5

var host = null;
var port = 6030;
for(var i = 2; i < global.process.argv.length; i++){
6 7 8 9 10 11 12 13 14
  var key = global.process.argv[i].split("=")[0];
  var value = global.process.argv[i].split("=")[1];

  if("host" == key){
    host = value;
  }
  if("port" == key){
    port = value;
  }
15 16 17
}

if(host == null){
18 19
  console.log("Usage: node nodejsChecker.js host=<hostname> port=<port>");
  process.exit(0);
20 21 22 23
}

// establish connection
var conn = taos.connect({host:host, user:"root", password:"taosdata",port:port});
24
var cursor = conn.cursor();
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
// create database
executeSql("create database if not exists test", 0);
// use db
executeSql("use test", 0);
// drop table
executeSql("drop table if exists test.weather", 0);
// create table
executeSql("create table if not exists test.weather(ts timestamp, temperature float, humidity int)", 0);
// insert
executeSql("insert into test.weather (ts, temperature, humidity) values(now, 20.5, 34)", 1);
// select
executeQuery("select * from test.weather");
// close connection
conn.close();

function executeQuery(sql){
41 42 43 44 45 46 47
  var start = new Date().getTime();
  var promise = cursor.query(sql, true);
  var end = new Date().getTime();
  promise.then(function(result){
    printSql(sql, result != null,(end - start));
    result.pretty();
  });
48 49 50
}

function executeSql(sql, affectRows){
51 52 53 54
  var start = new Date().getTime();
  var promise = cursor.execute(sql);
  var end = new Date().getTime();
  printSql(sql, promise == affectRows, (end - start));
55 56 57
}

function printSql(sql, succeed, cost){
58 59
  console.log("[ "+(succeed ? "OK" : "ERROR!")+" ] time cost: " + cost + " ms, execute statement ====> " + sql);
}