InfluxDB javascript driver
This is a InfluxDB driver for Javascript apps. It could work both in Node or browser1.
For node.js/iojs usage:
$ npm install --save influent
For usage in browser:
bower install --save influent
var influent = require('influent');
influent
.createHttpClient({
server: [
{
protocol: "http",
host: "localhost",
port: 8086
}
],
username: "gobwas",
password: "xxxx",
database: "mydb"
})
.then(function(client) {
client
.query("show databases")
.then(function(result) {
// ...
});
// super simple point
client.write({ key: "myseries", value: 10 });
// more explicit point
client
.write({
key: "myseries",
tags: {
some_tag: "sweet"
},
fields: {
some_field: 10
},
timestamp: Date.now()
})
.then(function() {
// ...
});
});
According to InfluxDB@0.9 docs there are four data types:
Field values may be stored as float64, int64, boolean, or string. All subsequent field values must match the type of the first point written to given measurement.
float64
values are the default numerical type.1
is a float,1i
is an integer;int64
value must have a trailingi
. The fieldbikes_present=15i
stores an integer and the fieldbikes_present=15
stores a float;boolean
values aret
,T
,true
,True
, orTRUE
for TRUE, andf
,F
,false
,False
, orFALSE
for FALSE;string
values for field values must be double-quoted. Double-quotes contained within the string must be escaped. All other characters are supported without escaping.
There is a little bit problem with Javascript numbers, cause it could be both integer or float. So to solve it there is the influent.Value
abstraction for you:
var influent = require("influent");
// client creation somewhere
client
.write({
key: "myseries",
tags: {
some_tag: "sweet"
},
fields: {
// this will be written as 10i, and saved as int64 10 into InfluxDB
i_field: new influent.I64(10),
// implicit way to write values
// note that all implicit field numbers are casted to the influxdb's float64
f_field: 10, // is equal to new influent.F64(10)
s_field: "string" // is equal to new influent.Str("string")
b_field: true // is equal to new influent.Bool(true)
},
timestamp: Date.now()
});
When you call influent.createAnyClient
you get a decorated client, that allows you to pass simple object
literals to write
and query
. This, of course, get some performance overhead and unnecessary object casting and type checks.
You could use this way, to be more explicit:
// create client
var client = new HttpClient({
username: "gobwas",
password: "xxxx"
});
// use line serializer
client.injectSerializer(new LineSerializer());
// use http client (this is for node, XhrHttp is for browser)
client.injectHttp(new NodeHttp());
// use stub elector, that always elects first host
client.injectElector(new StubElector([ host ]));
// create batch of points
var batch = new Batch({ database: "mydb" });
batch.add((new Measurement("key")).addField("value", 1))
// send batch
client.write(batch).then(...);
// create query object
var query = new Query("select * from key", { database: "mydb" });
// eval query
client.query(query).then(...);
Creates influent.DecoratorClient
instance, with influent.HttpClient
inside.
This method makes client.ping()
, to sure that connection is OK.
The config
should have structure like this:
{
// required
// --------
server: {
protocol: string
host: string
port: number
}
// or
server: [ serverA... serverN ]
username: string
password: string
// optional
// --------
database: string
// write options
precision: enum[n, u, ms, s, m, h]
consistency: enum[one, quorum, all, any]
rp: string
max_batch: number
// query options
epoch: enum[n, u, ms, s, m, h]
chunk_size: number
}
Default factory for creating udp client. Creates influent.DecoratorClient
instance, with influent.UdpClient
inside.
The config
should have structure like:
{
// required
// --------
server: {
protocol: string
host: string
port: number
}
// or
server: [ serverA... serverN ]
// optional
// --------
// write options
precision: enum[n, u, ms, s, m, h] // unsupported yet
max_batch: number
safe_limit: number
}
Where options could be:
{
database: string
precision: enum[n, u, ms, s, m, h]
consistency: enum[one, quorum, all, any]
rp: string
}
Where options could be:
{
database: string
epoch: enum[n, u, ms, s, m, h]
chunk_size: number
}
Abstract class of InfluxDB client. Has several abstract methods:
Pings host.
Asks for data.
Writes measurements.
Abstract ancessor of influent.Client
. Has several injector methods:
Implementation of influent.NetClient
for http usage.
Where options could be like:
{
// required
// --------
username: string,
password: string,
}
Injector of http service, that is implementation of abstract hurl.Http
class. hurl
is just npm dependency.
Implementation of influent.NetClient
for udp usage.
Where options could be like:
{
// optional
// --------
safe_limit: number
}
This method returns rejected Promise
, cause there is no ability to fetch some data through udp from InfluxDB.
Injector of udp service.
Wrapper around influent.Client
for better usability purposes.
If options are present, the could contain these optional fields:
database: string
// write options
precision: enum[n, u, ms, s, m, h]
consistency: enum[one, quorum, all, any]
rp: string
max_batch: number
// query options
epoch: enum[n, u, ms, s, m, h]
chunk_size: number
decoratorClient.write(data: influent.Batch | Object | influent.Measurement | Array[Object | influent.Measurement][, options: Object])
-> Promise[]
When measurement is Object
, it should have structure like:
{
// required
key: string,
// one of or both `value` or non-empty `fields` should be present
value: string | number | boolean | influent.Type,
fields: {
fieldName: string | number | boolean | influent.Type
},
// optional
tags: {
tagName: string
},
// optional
timestamp: number | string | Date
}
Represents strategy of electing host to send request.
Round robin strategy of host election.
Base strategy of election. Uses influent.Ping
to check health.
Where options:
{
period: number
}
Represents strategy of checking host health.
Checks health via http request.
Where options:
{
timeout: number
}
Checks health via exec ping ...
.
Where options:
{
timeout: number,
count: number
}
Line protocol implementation of influent.Serializer
.
Sets timestamp to the measurement. Using numeric string
, cause it make sense
on a big numbers with precision in nanoseconds.
Represents client.ping()
meta information.
1: Browser version is about 41KB minified, and 13KB gzipped. There are no polyfills in bundle for old browsers! Be sure, that you have at least these global objects and object methods:
Promise
;Object.keys
;Array.forEach
;XMLHttpRequest
.
Some Node.js specific classes are excluded from the influent
API browser build.
InfluxDB | Influent |
---|---|
<0.9.3 |
^0.2.3 |
>0.9.3 |
^0.3.0 |
MIT © Sergey Kamardin