InfluxDB Client for Node.js and Browsers

5.12.0 · active · verified Tue Apr 21

This package, `influx`, provides a client library for interacting with InfluxDB 1.x time-series databases from Node.js and browser environments. The current stable version is 5.12.0. Releases are frequent, with several minor versions and bug fixes released annually, indicating active maintenance for the 1.x series. Key differentiators include its full support across Node.js and browsers, a simple API for common InfluxDB operations, and its focus on performance, capable of processing millions of rows per second with zero external dependencies. It's crucial to note that this library is specifically for InfluxDB v1.x; users of InfluxDB v2.x should use the official `@influxdata/influxdb-client-js` package.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to connect to an InfluxDB 1.x instance, check for and create a database, write a data point, and perform a basic query. It also includes an example of using the `AbortSignal` for query cancellation, a feature introduced in v5.12.0.

import { InfluxDB, FieldType, IPoint } from 'influx';

async function run() {
  const host = process.env.INFLUXDB_HOST ?? 'localhost';
  const port = parseInt(process.env.INFLUXDB_PORT ?? '8086', 10);
  const username = process.env.INFLUXDB_USERNAME ?? 'admin';
  const password = process.env.INFLUXDB_PASSWORD ?? 'admin';
  const database = process.env.INFLUXDB_DATABASE ?? 'mydb';

  const influx = new InfluxDB({
    host: host,
    port: port,
    database: database,
    username: username,
    password: password,
    schema: [
      {
        measurement: 'cpu_load_short',
        fields: {
          value: FieldType.FLOAT,
          host: FieldType.STRING,
        },
        tags: ['host']
      }
    ]
  });

  // Ensure the database exists
  try {
    const dbs = await influx.getDatabaseNames();
    if (!dbs.includes(database)) {
      await influx.createDatabase(database);
      console.log(`Database '${database}' created.`);
    } else {
      console.log(`Database '${database}' already exists.`);
    }
  } catch (error) {
    console.error('Error checking/creating database:', error);
    process.exit(1);
  }

  // Write data
  const points: IPoint[] = [
    {
      measurement: 'cpu_load_short',
      tags: { host: 'server01' },
      fields: { value: 0.64, region: 'us-west' }
    }
  ];
  await influx.writePoints(points);
  console.log('Data written successfully.');

  // Query data
  const results = await influx.query(`
    SELECT * FROM cpu_load_short WHERE host = 'server01' ORDER BY time DESC LIMIT 5
  `);
  console.log('Query results:', results);

  // Example of using AbortSignal (v5.12.0 feature)
  const controller = new AbortController();
  const timeoutId = setTimeout(() => controller.abort(), 1000); // Abort after 1 second
  try {
    const abortableResults = await influx.query('SELECT * FROM cpu_load_short', {
      abortSignal: controller.signal
    });
    console.log('Abortable query results:', abortableResults);
  } catch (err: any) {
    if (err.name === 'AbortError') {
      console.log('Query aborted successfully.');
    } else {
      console.error('Abortable query failed:', err.message);
    }
  } finally {
    clearTimeout(timeoutId);
  }
}

run().catch(console.error);

view raw JSON →