Client

Client

Members

availableServers

Source:
Since:
  • 2.2.0

Get the available list

Example
const serverList = client.availableServers;
console.info(serverList)
// => [ { protocol: 'http', host: '127.0.0.1', port: 8086 } ]

database

Source:
Since:
  • 2.4.4

Get the database

Example
const database = client.database;
console.info(database);
// => mydb

epoch

Source:
Since:
  • 2.2.0

Get the specified precision of epoch timestamps

Example
const epoch = client.epoch;
console.info(epoch);
// => undefined

epoch

Source:
Since:
  • 2.2.0

Set the specified precision of epoch timestamps.It should be [h,m,s,ms,u,ns]

Example
client.epoch = 'ms';
console.info(client.epoch);
// => 'ms'

format

Source:
Since:
  • 2.2.0

Get the query data format type

Example
const formatType = client.format;
console.info(formatType);
// => undefined

format

Source:
Since:
  • 2.2.0

Set the query data format type

Example
client.format = 'json';
console.info(client.format);
// => 'json'

queryQueueLength

Source:
Since:
  • 2.2.0

Get the query queue length

Example
client.query('http')
 .set({limit: 1})
 .queue();
client.query('login')
 .set({limit: 1})
 .queue();
console.info(client.queryQueueLength);
// => 2

timeout

Source:
Since:
  • 2.2.0

Set the http request timeout value, the unit is ms. The default is 0, no timeout.

Example
const ms = client.timeout;
console.info(ms);
// => 0

timeout

Source:
Since:
  • 2.2.0

Set the http request timeout value.

Example
client.timeout = 1000;
console.info(client.timeout);
// => 1000

unavailableServers

Source:
Since:
  • 2.2.0
Deprecated:
  • since version 2.5.0

Get the unavailable list

Example
const serverList = client.unavailableServers;
console.info(serverList)
// => [ { protocol: 'http', host: '127.0.0.1', port: 8086 } ]

writeQueueLength

Source:
Since:
  • 2.2.0

Get the write queue length

Example
client.write('http')
 .tag({
   spdy: 'fast',
   type: '2',
 })
 .field({
   use: 300,
 })
 .queue();
client.write('http')
 .tag({
   spdy: 'slow',
   type: '4',
 })
 .field({
   use: 1000,
 })
 .queue();
console.info(client.writeQueueLength);
// => 2

Methods

addAlgorithm(type, fn)

Source:
Since:
  • 2.7.0

Set the load balance algorithm

Example
client.addAlgorithm('getByUrl', (request) => {
  return request.url.length;
});
Parameters:
Name Type Description
type String

the load balance type

fn function

the load balance function, it shoud return an integer

addPlugin(fn)

Source:
Since:
  • 2.11.0

Add plugin function

Example
const noCache = require('superagent-no-cache')
client.addPlugin(noCache);
Parameters:
Name Type Description
fn function

plugin function

createDatabase() → {Promise}

Source:
Since:
  • 2.2.0

Create the database of the connection uri

Example
const client = new Influx('http://127.0.0.1:8086/mydb');
client.createDatabase()
 .then(() => console.info('create database success'))
 .catch(err => console.error(`create database fail, ${err.message}`));
Returns:
Type
Promise

createRetentionPolicy(name, duration, replication, shardDuration, isDefault) → {Promise}

Source:
Since:
  • 2.2.0

Create retention policy

Example
client.createRetentionPolicy('mytest', '2h')
  .then(() => console.info('create retention policy success'))
  .then(err => console.error(`create retention policy fail, ${err.message}`));
Parameters:
Name Type Description
name String

The retention policy

duration String

The duration

replication Integer

The replication

shardDuration String

The shardDuration

isDefault Boolean

Is the default retention policy

Returns:
Type
Promise

dropDatabase() → {Promise}

Source:
Since:
  • 2.2.0

Drop the database of the connection uri

Example
const client = new Influx('http://127.0.0.1:8086/mydb');
client.dropDatabase()
 .then(() => console.info('drop database success'))
 .catch(err => console.error(`drop database fail, ${err.message}`));
Returns:
Type
Promise

dropRetentionPolicy(name) → {Promise}

Source:
Since:
  • 2.2.0

Drop retention policy

Example
client.dropRetentionPolicy('mytest')
  .then(() => console.info('drop retention policy success'))
  .then(err => console.error(`drop retention policy fail, ${err.message}`));
Parameters:
Name Type Description
name String

The retention policy

Returns:
Type
Promise

findOneAndUpdate(measurement, conditions, updateFields) → {Writer}

Source:

Update one record (latest)

Parameters:
Name Type Description
measurement any

The influxdb's measurement

conditions any

The query conditions

updateFields any

The update fileds

Returns:
Type
Writer

on(eventName, listener)

Source:
Since:
  • 2.4.0

Listen on the event, not is support: 'queue', 'writeQueue', 'queryQueue'

Example
client.on('queue', (type, data) => {
  // write or query
  console.info(type);
  console.info(data);
});
client.on('writeQueue', (data) => {
  console.info(data);
});
client.on('invalid-fields', (data) => {
  //{ measurement: 'request',
  // fail: [ { category: 'stripUnknown', key: 'version', value: 1 },
  //  { category: 'stripUnknown', key: 'token', value: 'abcd' },
  //  { category: 'invalid', type: 'integer', key: 'no', value: 'abcd' },
  //  { category: 'invalid', type: 'float', key: 'score', value: 'ab' } ]}
  console.info(data);
});
client.on('invalid-tags', (data) => {
  // {measurement: 'request',
  // fail: [ { category: 'invalid', key: 'type', value: 'a' } ]}
  console.info(data);
})
console.on('queryQueue', (data) => {
  console.info(data);
});
Parameters:
Name Type Description
eventName String

The name of event

listener function

The callback function

query(measurement) → {Reader}

Source:
Since:
  • 2.2.0

Get the influxdb query reader

Example
const reader = client.query('http');
Parameters:
Name Type Description
measurement String

The influxdb's measurement

Returns:
Type
Reader

queryPost(q, db) → {Promise}

Source:
Since:
  • 2.2.0

Use post for the influxdb query, such as create user

Example
client.queryPost('create user "vicanso" with password \'mypwd\' with all privileges')
  .then(() => console.info('create user success'))
  .catch(err => console.error(`create user fail, ${err.message}`));
Parameters:
Name Type Description
q String

The influx ql

db String

The database

Returns:
Type
Promise

queryRaw(q, db) → {Promise}

Source:
Since:
  • 2.2.0

Use influx ql to query the data

Examples
const client = new Influx('http://127.0.0.1:8086/mydb')
client.queryRaw('select * from "http"')
  .then(console.info)
  .catch(console.error);
const client = new Influx('http://127.0.0.1:8086/mydb')
client.queryRaw('select * from "login"', 'testdb')
  .then(console.info)
  .catch(console.error);
Parameters:
Name Type Description
q String

The influx ql

db String

[optional] The database, if the param is not set, it will use the connection's db.

Returns:
Type
Promise

showDatabases() → {Array}

Source:
Since:
  • 2.2.0

List all database of the server

Example
client.showDatabases()
  .then(console.info)
  .catch(console.error);
// => [ 'telegraf', '_internal', 'mydb' ]
Returns:
Type
Array

showFieldKeys(measurement) → {Array}

Source:
Since:
  • 2.2.0

Show field keys of measurement

Examples
client.showFieldKeys()
  .then(console.info)
  .catch(console.error);
// => [
//       { name: 'cpu', values: [
//         { key: 'usage_guest', type: 'float' },
//         { key: 'usage_guest_nice', type: 'float' }]
//       },
//       { name: 'disk', values: [
//         { key: 'free', type: 'integer' },
//         { key: 'inodes_free', type: 'integer' }]
//       }
//    ]
client.showFieldKeys('cpu')
  .then(console.info)
  .catch(console.error);
// => [
//       { name: 'cpu', values: [
//         { key: 'usage_guest', type: 'float' },
//         { key: 'usage_guest_nice', type: 'float' }]
//       }
//    ]
Parameters:
Name Type Description
measurement String

[optional] If not set the param, will get the field keys of the database.

Returns:
Type
Array

showMeasurements() → {Array}

Source:
Since:
  • 2.2.0

List the measurement of the influxdb server

Example
client.showMeasurements()
  .then(console.info)
  .catch(console.error);
// => [ 'cpu', 'disk', 'diskio', 'kernel', 'mem', 'processes', 'swap', 'system' ]
Returns:
Type
Array

showRetentionPolicies() → {Array}

Source:
Since:
  • 2.2.0

List retention policies

Example
client.showRetentionPolicies()
  .then(console.info)
  .catch(console.error);
// => [ { name: 'autogen', duration: '0s', shardGroupDuration: '168h0m0s', replicaN: 1, default: true } ]
Returns:
Type
Array

showTagKeys(measurement) → {Array}

Source:
Since:
  • 2.2.0

Show tag keys of measurement

Examples
client.showTagKeys()
  .then(console.info)
  .catch(console.error);
// => [
//       { name: 'cpu', values: [
//         { key: 'cpu' },
//         { key: 'host' },
//         { key: 'dc' }]
//       },
//       { name: 'disk', values: [
//         { key: 'dc' },
//         { key: 'fstype' }]
//       }
//    ]
client.showTagKeys('cpu')
  .then(console.info)
  .catch(console.error);
// => [
//       { name: 'cpu', values: [
//         { key: 'cpu' },
//         { key: 'host' },
//         { key: 'dc' }]
//       }
//    ]
Parameters:
Name Type Description
measurement String

[optional] If not set the param, will get the tag keys of the database.

Returns:
Type
Array

startHealthCheck(ping, interval) → {Client}

Source:
Since:
  • 2.4.5

Start the influxdb server health check

Example
client.startHealthCheck();
Parameters:
Name Type Description
ping function | Promise

[optional] The ping checker, It not set the param, will use the default ping checker.

interval Integer

[optional] The check interval

Returns:
Type
Client

stopHealthCheck() → {Client}

Source:
Since:
  • 2.2.0

Stop the influxdb server health check

Example
client.stopHealthCheck();
Returns:
Type
Client

syncQuery(format) → {Promise}

Source:
Since:
  • 2.2.0

Sync the query queue

Example
client.query('cpu')
  .set({
    limit: 1,
  })
  .queue();
client.query('mem')
  .set({
    limit: 1,
  })
  .queue();
client.syncQuery('json')
  .then(console.info)
  .catch(console.error);
// => { cpu:
//      [ { time: '2016-12-09T12:46:48Z',
//          cpu: 'cpu-total',
//          host: 'red',
//          usage_user: 0.05000000000000008 } ],
//       mem:
//       [ { time: '2016-12-09T12:46:48Z',
//           active: 175300608,
//           available: 7891501056,
//           available_percent: 94.7944240310642,
//           used_percent: 5.205575968935799 } ] }
Parameters:
Name Type Description
format String

[optional] The query response format type

Returns:
Type
Promise

syncWrite() → {Promise}

Source:
Since:
  • 2.2.0

Sync the write queue

Example
client.write('http')
  .tag({
    spdy: '1',
    type: '2',
  })
  .field({
     use: 300,
     code: 200,
   })
   .queue();
client.write('http')
  .tag({
    spdy: '2',
    type: '3',
  })
  .field({
    use: 600,
    code: 304,
  })
  .queue();
client.syncWrite()
  .then(() => console.info('sync write queue success'))
  .catch(err => console.error(`sync write queue fail, ${err.message}`));
// => sync write queue success
Returns:
Type
Promise

updateRetentionPolicy(name, duration, replication, shardDuration, isDefault) → {Promise}

Source:
Since:
  • 2.2.0

Update retention policy

Example
client.updateRetentionPolicy('mytest', '2h')
  .then(() => console.info('update retention policy success'))
  .then(err => console.error(`update retention policy fail, ${err.message}`));
Parameters:
Name Type Description
name String

The retention policy

duration String

The duration

replication Integer

The replication

shardDuration String

The shardDuration

isDefault Boolean

Is the default retention policy

Returns:
Type
Promise

write(measurement, precision) → {Writer}

Source:
Since:
  • 2.2.0

Get the writer for the influxdb

Example
client.write('http')
  .tag({
    spdy: '1',
    type: '2',
  })
  .field({
     use: 300,
     code: 200,
   })
   .then(() => console.info('write point success'));
   .catch(err => console.error(`write point fail, ${err.message}`));
Parameters:
Name Type Description
measurement String

The measurment name

precision String

[optional] timestamp precision, 'h', 'm', 's', 'ms', 'u', 'n'

Returns:
Type
Writer

writePoint(measurement, fields, tags, precision) → {Writer}

Source:
Since:
  • 2.2.0

Write point to influxdb

Example
client.writePoint('http', {
  use: 300,
  code: 200,
}, {
  spdy: '1',
  type: '2',
}).then(() => console.info('write point success'))
.catch(err => console.error(`write point fail,  ${err.message}`));
Parameters:
Name Type Description
measurement String

The measurement name

fields Object

The fields of write point

tags Object

[optional] The tags of write point

precision string

[optional] The timestamp precision. 'h', 'm', 's', 'ms', 'u', 'n'

Returns:
Type
Writer