2019-07-26 01:46:25 +00:00
/* This example is to show the preferred way to use the td-connector */
/* To run, enter node path/to/node-example.js */
2019-07-24 06:09:45 +00:00
// Get the td-connector package
2020-07-31 06:11:05 +00:00
const taos = require ( 'td2.0-connector' ) ;
2019-07-24 06:09:45 +00:00
/ * W e w i l l c o n n e c t t o T D e n g i n e b y p a s s i n g a n o b j e c t c o m p r i s e d o f c o n n e c t i o n o p t i o n s t o t a o s . c o n n e c t a n d s t o r e t h e
* 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 / e t c / t a o s
* 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 ( ) ;
2019-07-26 01:46:25 +00:00
// c1.query(query) will return a TaosQuery object, of which then we can execute. The execute function then returns a promise
2019-07-24 06:09:45 +00:00
// Let's create a database named db
try {
2020-09-04 08:37:41 +00:00
c1 . execute ( 'create database if not exists db;' ) ;
//var query = c1.query('create database if not exists db;');
//query.execute();
2019-07-24 06:09:45 +00:00
}
catch ( err ) {
conn . close ( ) ;
throw err ;
}
2019-07-26 01:46:25 +00:00
// 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
2019-07-24 06:09:45 +00:00
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
2019-07-26 01:46:25 +00:00
// 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
2019-07-24 06:09:45 +00:00
try {
2019-07-26 01:46:25 +00:00
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." )
} ) ;
2019-07-24 06:09:45 +00:00
}
catch ( err ) {
conn . close ( ) ;
throw err ;
}
// Let's get the description of the table weather
2019-07-26 01:46:25 +00:00
// 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.
2019-07-24 06:09:45 +00:00
try {
2019-07-26 01:46:25 +00:00
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 ( ) ;
} ) ;
2019-07-24 06:09:45 +00:00
}
catch ( err ) {
conn . close ( ) ;
throw err ;
}
2020-09-04 08:37:41 +00:00
Date . prototype . Format = function ( fmt ) {
var o = {
'M+' : this . getMonth ( ) + 1 ,
'd+' : this . getDate ( ) ,
'H+' : this . getHours ( ) ,
'm+' : this . getMinutes ( ) ,
's+' : this . getSeconds ( ) ,
'S+' : this . getMilliseconds ( )
} ;
if ( /(y+)/ . test ( fmt ) ) {
fmt = fmt . replace ( RegExp . $1 , ( this . getFullYear ( ) + '' ) . substr ( 4 - RegExp . $1 . length ) ) ;
}
for ( var k in o ) {
if ( new RegExp ( '(' + k + ')' ) . test ( fmt ) ) {
fmt = fmt . replace ( RegExp . $1 , ( RegExp . $1 . length == 1 ) ? ( o [ k ] ) : ( ( '00' + o [ k ] ) . substr ( String ( o [ k ] ) . length ) ) ) ;
}
}
return fmt ;
}
2019-07-24 06:09:45 +00:00
// Let's try to insert some random generated data to test with
2019-07-26 01:46:25 +00:00
// 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
2019-07-24 06:09:45 +00:00
let stime = new Date ( ) ;
let interval = 1000 ;
try {
2019-07-26 01:46:25 +00:00
for ( let i = 0 ; i < 1000 ; i ++ ) {
2019-07-24 06:09:45 +00:00
stime . setMilliseconds ( stime . getMilliseconds ( ) + interval ) ;
2020-09-04 08:37:41 +00:00
//console.log(stime.Format('yyyy-MM-dd HH:mm:ss.SSS'));
2019-07-26 01:46:25 +00:00
let insertData = [ stime ,
2019-07-24 06:09:45 +00:00
parseInt ( Math . random ( ) * 100 ) ,
parseInt ( Math . random ( ) * 300 ) ,
parseFloat ( Math . random ( ) * 10 + 30 ) ,
2020-09-04 08:37:41 +00:00
"Note" ] ;
2019-07-26 01:46:25 +00:00
//c1.execute('insert into db.weather values(' + insertData.join(',') + ' );');
2020-09-04 08:37:41 +00:00
//var query = c1.query('insert into db.weather values(?, ?, ?, ?, ?);').bind(insertData);
//query.execute();
c1 . execute ( 'insert into db.weather values(\"' + stime . Format ( 'yyyy-MM-dd HH:mm:ss.SSS' ) + '\",' + parseInt ( Math . random ( ) * 100 ) + ',' + parseInt ( Math . random ( ) * 300 ) + ',' + parseFloat ( Math . random ( ) * 10 + 30 ) + ',"Note");' ) ;
2019-07-24 06:09:45 +00:00
}
2020-09-04 08:37:41 +00:00
} catch ( err ) {
2019-07-24 06:09:45 +00:00
conn . close ( ) ;
throw err ;
}
// Now let's look at our newly inserted data
var retrievedData ;
try {
2019-07-26 01:46:25 +00:00
c1 . query ( 'select * from db.weather limit 5 offset 100;' , true ) . then ( function ( result ) {
2020-09-04 08:37:41 +00:00
//result.pretty();
console . log ( '=========>' + JSON . stringify ( result ) ) ;
2019-07-26 01:46:25 +00:00
// Neat!
} ) ;
2019-07-24 06:09:45 +00:00
}
catch ( err ) {
conn . close ( ) ;
throw err ;
}
// Let's try running some basic functions
try {
2019-07-26 01:46:25 +00:00
c1 . query ( 'select count(*), avg(temperature), max(temperature), min(temperature), stddev(temperature) from db.weather;' , true )
. then ( function ( result ) {
result . pretty ( ) ;
} )
2019-07-24 06:09:45 +00:00
}
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!