Skip to content

Commit 8ea6e29

Browse files
committed
Added automatic database creation if it does not exist and fixes for test.js
1 parent 711e439 commit 8ea6e29

File tree

3 files changed

+27
-27
lines changed

3 files changed

+27
-27
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ This plugin adds an InfluxDB driver made available to all routes via the `decora
8686
| ---------- | ----------------------------------------------------------------------------------------------------------- |
8787
| `host` | Optional, the host to connect to. Defaults to `localhost` |
8888
| `hosts` | Optional, the multiple hosts to connect to. If specified, multi-cluster setup will be passed on to `influx` |
89-
| `database` | Optional, the database to connect to |
89+
| `database` | Optional, the database to connect to. If the database does not exist, the library will create it. |
9090
| `schema` | Optional, the schema of the database we are connecting to |
9191
| `username` | Optional, the username to use for authorization if any |
9292
| `password` | Optional, the password to use for authorization if any |

index.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ async function influxConnector(
1515
password
1616
}
1717
) {
18-
const connectionOptions = { database, port, schema };
18+
const connectionOptions = { database, port };
1919

2020
if (hosts === undefined) connectionOptions.host = host;
2121
else connectionOptions.hosts = hosts;
@@ -26,7 +26,7 @@ async function influxConnector(
2626
}
2727

2828
if (schema !== undefined) {
29-
schema.map(schema => {
29+
connectionOptions.schema = schema.map(schema => {
3030
Object.keys(schema.fields).forEach(
3131
field => (schema.fields[field] = Influx.FieldType[schema.fields[field]])
3232
);
@@ -36,6 +36,14 @@ async function influxConnector(
3636

3737
const influx = new Influx.InfluxDB(connectionOptions);
3838

39+
if (database !== undefined) {
40+
const names = await influx.getDatabaseNames();
41+
42+
if (!names.includes(database)) {
43+
await influx.createDatabase(database);
44+
}
45+
}
46+
3947
Influx.instance = influx;
4048
delete Influx.InfluxDB;
4149

test.js

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,9 @@
33
const fastify = require("fastify")();
44
const tap = require("tap");
55
const fastifyInfluxDB = require("./index");
6-
const { InfluxDB } = require("influx");
7-
const influx = new InfluxDB();
86

97
tap.test("fastify influxDB is correctly injected", async test => {
10-
// Pre-cursory database creation
11-
await influx.createDatabase("NOAA_water_database");
12-
// End of pre-cursory code
8+
test.plan(4);
139

1410
fastify.register(fastifyInfluxDB, {
1511
host: "localhost",
@@ -71,23 +67,19 @@ tap.test("fastify influxDB is correctly injected", async test => {
7167
});
7268
});
7369

74-
fastify.ready(err => {
75-
test.error(err);
76-
fastify.inject(
77-
{
78-
method: "GET",
79-
url: "/"
80-
},
81-
(err, { payload }) => {
82-
const [fetchedRow] = JSON.parse(payload).rows;
83-
test.strictEqual(fetchedRow["level description"], "Medium");
84-
test.strictEqual(fetchedRow.location, "athens");
85-
test.strictEqual(fetchedRow.water_level, 2.4324);
86-
fastify.close(() => {
87-
test.end();
88-
process.exit(0);
89-
});
90-
}
91-
);
92-
});
70+
try {
71+
await fastify.ready();
72+
73+
const { payload } = await fastify.inject({
74+
method: "GET",
75+
url: "/"
76+
});
77+
78+
const [fetchedRow] = JSON.parse(payload).rows;
79+
test.strictEqual(fetchedRow["level description"], "Medium");
80+
test.strictEqual(fetchedRow.location, "athens");
81+
test.strictEqual(fetchedRow.water_level, 2.4324);
82+
} catch (e) {
83+
test.error(e);
84+
}
9385
});

0 commit comments

Comments
 (0)