InfluxDB-v2
A light-weight library to write datas to InfluxDB v2 and query it.
It is created with simple goals in mind: be simple, efficient and easy to use.
If you like it, please star it! 🥰
Note that InfluxDB-v2
is compatible with InfluxDB v2 only.
It does NOT work with InfluxDB v1.
InfluxDB-v2
is sponsored by Stackhero.
If you are looking for powerful managed services, you should seriously consider Stackhero 🤓
Installation
With NPM: npm install --save influxdb-v2
With Yarn: yarn add influxdb-v2
Example
const Influxdb = require('influxdb-v2');
(async () => {
const influxdb = new Influxdb({
host: 'influxdb.example.com',
token: 'myInfluxdbToken'
});
await influxdb.write(
{
org: 'myOrganization',
bucket: 'myBucket'
},
[{
measurement: 'web',
fields: {
load: 12.34
}
}]
);
const result = await influxdb.query(
{ org: 'myOrganization' },
{ query: 'from(bucket: "myBucket") |> range(start: -1h)' }
);
console.log(result);
})().catch(error => {
console.error('\n🐞 An error occurred!', error);
process.exit(1);
});
Usage
Import the library and initialize it:
const Influxdb = require('influxdb-v2');
const influxdb = new Influxdb({
host: '',
token: ''
});
Write
Basic
You can write datas to InfluxDB simply like this:
influxdb.write(
{
org: '',
bucket: '',
},
[{
measurement: '',
fields: {
load: 12.34
}
}]
);
Handle timestamp
By default, the time recorded in InfluxDB is the time when you sent the request.
This is probably not what you want to do.
You can add a timestamp like this:
influxdb.write(
{
org: '',
bucket: '',
precision: 'ms'
},
[{
measurement: '',
fields: {
load: 12.34
},
timestamp: Date.now()
}]
);
Handle tags and multiple fields
You can add tags and multiple fields like this:
influxdb.write(
{
org: '',
bucket: '',
},
[{
measurement: 'host',
tags: {
deviceId: 'gyh43',
version: '1.2.3',
location: 'factory-1'
},
fields: {
load: 12.34,
cpu: 42,
battery: 65
}
}]
);
Send multiple datas
And finally, you can send multiple datas at a same time:
influxdb.write(
{
org: '',
bucket: '',
precision: 's'
},
[
{
measurement: '',
fields: {
load: 12.34
},
timestamp: 1569513823
},
{
measurement: '',
fields: {
load: 23.45
},
timestamp: 1569513838
}
]
);
Query
You can write flux queries simply like this:
const result = await influxdb.query(
{
org: '',
},
{
query: 'from(bucket: "myBucket") |> range(start: -1h)'
}
);
console.log(result);
Get RAW result
By default InfluxDB-v2
will parse the result of queries.
If you prefer to get the RAW result (CSV), just set the parameter csv
to true.
const result = await influxdb.query(
{
org: '',
csv: true
},
{
query: 'from(bucket: "myBucket") |> range(start: -1h)'
}
);
console.log(result);
Handle self signed certificates
influxdb-v2
uses fetch
to communicate with InfluxDB API.
You can pass options to fetch
using fetchOptions
in the constructor.
You can use a defined CA certificate like this:
const https = require('https');
const influxdb = new Influxdb({
host: '',
token: '',
fetchOptions: {
agent: new https.Agent({
ca: [ fs.readFileSync('yourCaFile.ca') ],
})
}
});
You can ignore certificate checks too.
⚠️ It is not recommended at all and expose your to MITM attacks!
const https = require('https');
const influxdb = new Influxdb({
host: '',
token: '',
fetchOptions: {
agent: new https.Agent({
rejectUnauthorized: false
})
}
});