Home Reference Source Repository
import Connection from 'influxdb-nodejs/src/Connection.js'
public class | source

Connection

Main class used to communicate with InfluxDB

Constructor Summary

Public Constructor
public

Create new Connection object.

Member Summary

Public Members
public

stub: *

Method Summary

Public Methods
public

Verify the connection to InfluxDB is available.

public

Disconnect from the database.

public

Execute query on InfluxDB

public

Execute query on InfluxDB and get unmodified JSON data result

public

Flush buffered points into InfluxDB server(s)

public

write(dataPoints: DataPoint[], forceFlush: Boolean): Promise

Write measurement data points into InfluxDB.

Public Constructors

public constructor(options: ConnectionConfiguration): * source

Create new Connection object. To verify that everything is set correctly, call the Connection#connect method

Params:

NameTypeAttributeDescription
options ConnectionConfiguration

all settings needed to connect to InfluxDB and configure the communication protocol

Return:

*

new Connection object

Public Members

public stub: * source

Public Methods

public connect(): Promise source

Verify the connection to InfluxDB is available. If you don't call this method on your own, it will be called automatically before the first write to InfluxDB.

Return:

Promise

Throw:

InfluxDBError

public disconnect(): Promise source

Disconnect from the database. The internal buffer gets automatically flushed when you invoke this function and you will receive a promise object for this operation as a return value. Further write or query operations invoked on the disconnected connection will throw an exception unless you call Connection.connect again.

Return:

Promise

resolved when all buffered data are written to InfluxDB

Throw:

InfluxDBError

public executeQuery(query: String): Array source

Execute query on InfluxDB

Params:

NameTypeAttributeDescription
query String

text definition of the query, e.g. 'select * from outdoorTemperature'.

Return:

Array

post-processed data so that these are easy to work with. The result is an array of objects where fields and tags are stored as regular properties. There is also a time property holding the timestamp of the measurement as a JavaScript Date object.

Throw:

InfluxDBError

Example:

[
  {
    "time": "1970-01-18T07:59:02.227Z",
    "temperature": 23.7,
    "location": "greenhouse"
  },
  {
    "time": "1970-01-18T07:59:02.265Z",
    "temperature": 23.7,
    "location": "greenhouse"
  }
]

public executeRawQuery(query: String): Array source

Execute query on InfluxDB and get unmodified JSON data result

Params:

NameTypeAttributeDescription
query String

text definition of the query, e.g. 'select * from outdoorTemperature'

Return:

Array

unmodified JSON response data

Throw:

InfluxDBError

public flush(): Promise source

Flush buffered points into InfluxDB server(s)

Return:

Promise

Throw:

InfluxDBError

public write(dataPoints: DataPoint[], forceFlush: Boolean): Promise source

Write measurement data points into InfluxDB.

By default data submitted for writing is buffered on the Connection object, to be written as a batch. This buffer gets flushed and batch-written to InfluxDB when one of the following conditions are met:

  1. Once its data point capacity has been reached (It is configured using ConnectionConfiguration.batchSize when creating the connection)
  2. Once the oldest data point submitted in the buffer gets older than the value ConnectionConfiguration.maximumWriteDelay defined when creating the connection.
  3. When you call the Connection.flush method
  4. When you call the Connection.write method with the optional parameter forceFlush=true

You may disable the batch writes by setting ConnectionConfiguration.batchSize or ConnectionConfiguration.maximumWriteDelay to 0. If batch writes are disabled, writes to InfluxDB will be initiated at each invocation of Connection.write.

The function returns a promise. There are two ways promises are resolved (distinguished by ConnectionConfiguration.autoResolveBufferedWritePromises):

  1. (autoResolveBufferedWritePromises=true, Default) The promise gets resolved when the data points are stored in the connection buffer. In this case, some writes are stored in the buffer only to be batch-written. If an error occurs during communication with InfluxDB the error will propagate as follows:
    • To the write method promise that triggered communication with InfluxDB (either data point capacity overrun or by calling write with the parameter forceFlush=true)
    • To the promise returned by invoking Connection.flush
    • To the error handler defined by ConnectionConfiguration.batchWriteErrorHandler in the case the communication to InfluxDB is triggered when there is data in the connection buffer older than the delay defined by ConnectionConfiguration.maximumWriteDelay
  2. (autoResolveBufferedWritePromises=false) The promise is never resolved before the data points are successfully written into InfluxDB. In the case of communication failure you will receive as many errors as the number of invocations of ConnectionConfiguration.write since the last successful write to InfluxDB. This mode is useful when you need finer visibility into the writes to InfluxDB (you may respond accordingly to each missed write). On the other hand applications that don't require finer visibility would suffer from log pollution from error messages (one error for each batch write to InfluxDB might be enough). Also, this mode consumes more cpu and memory resources.

Params:

NameTypeAttributeDescription
dataPoints DataPoint[]

an array of measurement points to write to InfluxDB

forceFlush Boolean
  • optional
  • default: false

if true the internal data point buffer gets flushed into InfluxDB right away.

Return:

Promise

a promise that is evaluated when data is either written into InfluxDB or an i/o error occurs.

Throw:

InfluxDBError

Example:

  const series=[dataPoint1, dataPoint2];
  connection.write(series).then(() => {
       console.log('Data were written');
  }).catch(console.error);