const taos = require('../tdengine');
var conn = taos.connect();
var c1 = conn.cursor();
let stime = new Date();
let interval = 1000;
function convertDateToTS(date) {
  let tsArr = date.toISOString().split("T")
  return "\"" + tsArr[0] + " " + tsArr[1].substring(0, tsArr[1].length - 1) + "\"";
}
function R(l, r) {
  return Math.random() * (r - l) - r;
}
function randomBool() {
  if (Math.random() < 0.5) {
    return true;
  }
  return false;
}

// Initialize
//c1.execute('drop database td_connector_test;');
const dbname = 'nodejs_test_ns';
c1.execute('create database if not exists ' + dbname + ' precision "ns"');
c1.execute('use ' + dbname)
c1.execute('create table if not exists tstest (ts timestamp, _int int);');
c1.execute('insert into tstest values(1625801548423914405, 0)');
// Select
console.log('select * from tstest');
c1.execute('select * from tstest');

var d = c1.fetchall();
console.log(c1.fields);
let ts = d[0][0];
console.log(ts);

if (ts.taosTimestamp() != 1625801548423914405) {
  throw "nanosecond not match!";
}
if (ts.getNanoseconds() % 1000000 !== 914405) {
  throw "nanosecond precision error";
}
setTimeout(function () {
  c1.query('drop database nodejs_ns_test;');
}, 200);

setTimeout(function () {
  conn.close();
}, 2000);
