Connection
Main class used to communicate with InfluxDB
Constructor Summary
Public Constructor | ||
public |
constructor(options: ConnectionConfiguration): * 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 |
executeQuery(query: String): Array Execute query on InfluxDB |
|
public |
executeRawQuery(query: String): Array Execute query on InfluxDB and get unmodified JSON data result |
|
public |
Flush buffered points into InfluxDB server(s) |
|
public |
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:
Name | Type | Attribute | Description |
options | ConnectionConfiguration | all settings needed to connect to InfluxDB and configure the communication protocol |
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.
Throw:
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.
Throw:
public executeQuery(query: String): Array source
Execute query on InfluxDB
Params:
Name | Type | Attribute | Description |
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:
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:
Name | Type | Attribute | Description |
query | String | text definition of the query, e.g. 'select * from outdoorTemperature' |
Throw:
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:
- Once its data point capacity has been reached (It is configured using ConnectionConfiguration.batchSize when creating the connection)
- Once the oldest data point submitted in the buffer gets older than the value ConnectionConfiguration.maximumWriteDelay defined when creating the connection.
- When you call the Connection.flush method
- 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):
- (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
- (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.
Return:
Promise | a promise that is evaluated when data is either written into InfluxDB or an i/o error occurs. |
Throw:
Example:
const series=[dataPoint1, dataPoint2];
connection.write(series).then(() => {
console.log('Data were written');
}).catch(console.error);