-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.ts
59 lines (51 loc) · 1.46 KB
/
db.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import dotenv from 'dotenv'
import { InfluxDB, WritePrecisionType } from '@influxdata/influxdb-client'
import { DeleteAPI, HealthAPI } from '@influxdata/influxdb-client-apis'
import { Agent } from 'http'
const agent = new Agent({
keepAlive: true,
keepAliveMsecs: 20 * 1000, // 20 seconds keep alive
})
dotenv.config()
export const {
INFLUXDB_URL: url = 'http://localhost:8086',
INFLUXDB_TOKEN: token,
INFLUXDB_ORG: org = 'myOrg',
INFLUXDB_BUCKET: bucket = 'mqtt_logger',
PRECISION: precision = 'ns',
} = process.env
const influxDb = new InfluxDB({
url,
token,
transportOptions: { agent },
})
export const writeApi = influxDb.getWriteApi(
org,
bucket,
precision as WritePrecisionType
)
export const queryApi = influxDb.getQueryApi(org)
export const deleteApi = new DeleteAPI(influxDb)
export const healthApi = new HealthAPI(influxDb)
export const influx_read = (query: string) =>
new Promise((resolve, reject) => {
// helper function for Influx queries
// TODO: find type
const results: any[] = []
queryApi.queryRows(query, {
next(row, tableMeta) {
// TODO: Find way to convert directly to an array
const result = tableMeta.toObject(row)
results.push(result)
},
error(error) {
reject(error)
},
complete() {
resolve(results)
},
})
})
export const healthCheck = async () => {
await healthApi.getHealth()
}