42 lines
1.1 KiB
JavaScript
42 lines
1.1 KiB
JavaScript
'use strict'
|
|
/** @module query
|
|
* Queries a data point in InfluxDB using the Javascript client library with Node.js.
|
|
**/
|
|
|
|
import { InfluxDB, Point } from '@influxdata/influxdb-client'
|
|
|
|
/** Environment variables **/
|
|
const url = process.env.INFLUX_URL || ''
|
|
const token = process.env.INFLUX_TOKEN
|
|
const org = process.env.INFLUX_ORG || ''
|
|
|
|
/**
|
|
* Instantiate the InfluxDB client
|
|
* with a configuration object.
|
|
*
|
|
* Get a query client configured for your org.
|
|
**/
|
|
const queryApi = new InfluxDB({url, token}).getQueryApi(org)
|
|
|
|
/** To avoid SQL injection, use a string literal for the query. */
|
|
const fluxQuery = 'from(bucket:"air_sensor") |> range(start: 0) |> filter(fn: (r) => r._measurement == "temperature")'
|
|
|
|
const fluxObserver = {
|
|
next(row, tableMeta) {
|
|
const o = tableMeta.toObject(row)
|
|
console.log(
|
|
`${o._time} ${o._measurement} in ${o.region} (${o.sensor_id}): ${o._field}=${o._value}`
|
|
)
|
|
},
|
|
error(error) {
|
|
console.error(error)
|
|
console.log('\nFinished ERROR')
|
|
},
|
|
complete() {
|
|
console.log('\nFinished SUCCESS')
|
|
}
|
|
}
|
|
|
|
/** Execute a query and receive line table metadata and rows. */
|
|
queryApi.queryRows(fluxQuery, fluxObserver)
|