-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
53 lines (42 loc) · 1.13 KB
/
index.js
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
"use strict";
const fastifyPlugin = require("fastify-plugin");
const Influx = require("influx");
async function influxConnector(
fastify,
{
host = "localhost",
port = 8086,
hosts,
database,
schema,
username,
password
}
) {
const connectionOptions = { database, port };
if (hosts === undefined) connectionOptions.host = host;
else connectionOptions.hosts = hosts;
if (username !== undefined) {
connectionOptions.username = username;
connectionOptions.password = password;
}
if (schema !== undefined) {
connectionOptions.schema = schema.map(schema => {
Object.keys(schema.fields).forEach(
field => (schema.fields[field] = Influx.FieldType[schema.fields[field]])
);
return schema;
});
}
const influx = new Influx.InfluxDB(connectionOptions);
if (database !== undefined) {
const names = await influx.getDatabaseNames();
if (!names.includes(database)) {
await influx.createDatabase(database);
}
}
Influx.instance = influx;
delete Influx.InfluxDB;
fastify.decorate("influxdb", Influx);
}
module.exports = fastifyPlugin(influxConnector);