influxdb-light
A light-weight client to read and write data to InfluxDb V1 and V2.
It supports both InfluxQL(SQL) and Flux queries.
This light-weight client is incredibly easy to use.
Handling of tags and fields is straightforward with no additional complexity.
Good to know
You have to create a DBRP mapping if you want to use InfluxQL(SQL) on your V2 buckets.
For more details on DBRP mapping please read this guide.
Installation
npm i influxdb-light
Usage
InfluxDb Version 1 Example
import { InfluxDB } from "influxdb-light";
const request = {
host: "localhost",
port: "8086",
protocol: "http",
username: "<username>",
password: "<password>"
};
const influxDb = new InfluxDB(request);
const dbName = "demo1";
const measurement = "energy";
influxDb.writeV1({
measurement: measurement,
payload: [{
deviceId: "d007",
location: "L007",
type: 7,
enabled: true,
valueX: 100,
valueY: 120,
ip: "192.168.0.2"
}, {
deviceId: "d009",
location: "L009",
type: 9,
enabled: false,
valueX: 110,
valueY: 130,
ip: "192.168.0.3"
}],
tags: ["deviceId", "location", "type", "enabled"],
timestamp: 1664821800000000000
},
dbName,
{
rp: 'autogen',
consistency: 'all'
})
.then(res => {
console.log(res);
}).catch(err => {
console.error(err);
});
influxDb.queryV1(dbName, `SELECT * FROM ${measurement} WHERE "deviceId"='d007'`)
.then(res => {
console.log(res);
}).catch(err => {
console.error(err);
});
InfluxDb Version 2 Example
import { InfluxDB } from "influxdb-light";
const request = {
host: "localhost",
port: "8086",
protocol: "http",
token: "<token_value>"
};
const influxDb = new InfluxDB(request);
const org = "demo_org";
const dbName = "demo1";
const measurement = "energy";
const precision = "ns";
influxDb.writeV2({
measurement: measurement,
payload: [{
deviceId: "d007",
location: "L007",
type: 7,
enabled: true,
valueX: 100,
valueY: 120,
ip: "192.168.0.2"
}, {
deviceId: "d009",
location: "L009",
type: 9,
enabled: false,
valueX: 110,
valueY: 130,
ip: "192.168.0.3"
}],
tags: ["deviceId", "location", "type", "enabled"],
timestamp: 1664821800000000000
}, org, dbName, precision)
.then(res => {
console.log(res);
}).catch(err => {
console.error(err);
});
influxDb.queryV2("orgtest", `from(bucket:"demo1") |>
range(start: -60d) |>
filter(fn: (r) => r._measurement == "energy") |>
filter(fn: (r) => r.deviceId == "d007") |>
pivot(rowKey: ["_time"], columnKey: ["_field"], valueColumn: "_value")`))
.then(res => {
console.log(res);
}).catch(err => {
console.error(err);
});
Examples of Bucket operations on InfluxDB V2.x.x
import { InfluxDB } from "influxdb-light";
const influxDb = new InfluxDB({ host: "localhost", port: "8086", protocol: "http", token: "<token_value>" });
influxDb.createBucket(<org_ID>, <bucket_name>, <retention_in_seconds>)
influxDb.getBucket("640bee9277f75f2d", { id: "10592e2bff3d1789" }).then(result => console.log(result));
influxDb.getBucket("640bee9277f75f2d", { name: "testBucket007" }).then(result => console.log(result));
influxDb.listBuckets("640bee9277f75f2d").then(result => console.log(result));