npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

influxdb-v2

v1.0.0

Published

A light-weight library to write datas to InfluxDB v2 and query it.

Downloads

648

Readme

InfluxDB-v2

A light-weight library to write datas to InfluxDB v2 and query it.

It is created with simple goals in mind: be simple, efficient and easy to use.

If you like it, please star it! 🥰

Note that InfluxDB-v2 is compatible with InfluxDB v2 only. It does NOT work with InfluxDB v1.

Sponsors

InfluxDB-v2 is sponsored by Stackhero. If you are looking for powerful managed services, you should seriously consider Stackhero 🤓

Installation

With NPM: npm install --save influxdb-v2

With Yarn: yarn add influxdb-v2

Example

const Influxdb = require('influxdb-v2');

(async () => {

  const influxdb = new Influxdb({
    host: 'influxdb.example.com',
    token: 'myInfluxdbToken'
  });

  await influxdb.write(
    {
      org: 'myOrganization',
      bucket: 'myBucket'
    },
    [{
      measurement: 'web',
      fields: {
        load: 12.34
      }
    }]
  );

  const result = await influxdb.query(
    { org: 'myOrganization' },
    { query: 'from(bucket: "myBucket") |> range(start: -1h)' }
  );
  console.log(result);

})().catch(error => {
  console.error('\n🐞 An error occurred!', error);
  process.exit(1);
});

Usage

Import the library and initialize it:

const Influxdb = require('influxdb-v2');

const influxdb = new Influxdb({
  host: '', // [Required] your server IP or domain name, like `influxdb.example.com`
  // protocol: 'https', // `http` or `https`, default to `https`
  // port: 443, // if different than 80 (for http) or 443 (for https). Probably 8086 or 9999.
  token: '' // [Required] your InfluxDB token. You can create one in InfluxDB console in "Settings"/"Tokens"/"Generate".
  // fetchOptions: {} // Option passed to node-fetch
});

Write

Basic

You can write datas to InfluxDB simply like this:

influxdb.write(
  {
    org: '', // [Required] your organization. You can set `orgID` if you prefer to use the ID
    bucket: '', // [Required] your bucket
  },
  [{
    measurement: '', // [Required] your measurement
    fields: {
      load: 12.34 // a field named "load" as an example, with a value of 12.34
    }
  }]
);

Handle timestamp

By default, the time recorded in InfluxDB is the time when you sent the request. This is probably not what you want to do. You can add a timestamp like this:

influxdb.write(
  {
    org: '', // [Required] your organization. You can set `orgID` if you prefer to use the ID
    bucket: '', // [Required] your bucket
    precision: 'ms' // precision of timestamp. Can be `ns` (nanoseconds), `us` (microseconds), `ms` (milliseconds) or `s` (seconds). The default is `ns`
  },
  [{
    measurement: '', // [Required] your measurement
    fields: {
      load: 12.34 // a field named "load" as an example, with a value of 12.34
    },
    timestamp: Date.now() // the timestamp in milliseconds, as defined in `precision`
  }]
);

Handle tags and multiple fields

You can add tags and multiple fields like this:

influxdb.write(
  {
    org: '', // [Required] your organization
    bucket: '', // [Required] your bucket
  },
  [{
    measurement: 'host', // [Required] your measurement

    // Define tags
    tags: {
      deviceId: 'gyh43',
      version: '1.2.3',
      location: 'factory-1'
    },

    // Define fields
    fields: {
      load: 12.34, // a field named "load" as an example, with a value of 12.34
      cpu: 42, // another name "cpu"
      battery: 65 // etc...
    }
  }]
);

Send multiple datas

And finally, you can send multiple datas at a same time:

influxdb.write(
  {
    org: '', // [Required] your organization. You can set `orgID` if you prefer to use the ID
    bucket: '', // [Required] your bucket
    precision: 's' // timestamp in seconds
  },
  [
    {
      measurement: '', // [Required] your measurement
      fields: {
        load: 12.34
      },
      timestamp: 1569513823
    },
    {
      measurement: '', // [Required] your measurement
      fields: {
        load: 23.45
      },
      timestamp: 1569513838
    }
  ]
);

Query

You can write flux queries simply like this:

const result = await influxdb.query(
  {
    org: '', // [Required] your organization. You can set `orgID` if you prefer to use the ID
  },
  {
    query: 'from(bucket: "myBucket") |> range(start: -1h)' // [Required] your flux query
  }
);

console.log(result);
/*
[
  [
    { result: '_result',
      table: '0',
      _start: 2019-09-26T16:36:48.868Z,
      _stop: 2019-09-26T16:37:48.868Z,
      _time: 2019-09-26T16:37:47.795Z,
      _value: 12.34,
      _field: 'fieldname1',
      _measurement: 'myMeasurement',
      tagname1: 'myTagvalue1',
      tagname2: 'myTagvalue2'
    }
  ],
  [
    { result: '_result',
      table: '1',
      _start: 2019-09-26T16:36:48.868Z,
      _stop: 2019-09-26T16:37:48.868Z,
      _time: 2019-09-26T16:37:47.795Z,
      _value: 23.45,
      _field: 'fieldname2',
      _measurement: 'myMeasurement',
      tagname1: 'myTagvalue1',
      tagname2: 'myTagvalue2'
    }
  ]
]
*/

Get RAW result

By default InfluxDB-v2 will parse the result of queries. If you prefer to get the RAW result (CSV), just set the parameter csv to true.

const result = await influxdb.query(
  {
    org: '', // [Required] your organization. You can set `orgID` if you prefer to use the ID
    csv: true // get the plain text (CSV) result
  },
  {
    query: 'from(bucket: "myBucket") |> range(start: -1h)' // [Required] your flux query
  }
);

console.log(result); // plain text (CSV) result
/*
,result,table,_start,_stop,_time,_value,_field,_measurement,tagname1,tagname2
,_result,0,2019-09-26T16:36:48.868439105Z,2019-09-26T16:37:48.868439105Z,2019-09-26T16:37:47.795Z,12.34,fieldname1,myMeasurement,myTagvalue1,myTagvalue2

,result,table,_start,_stop,_time,_value,_field,_measurement,tagname1,tagname2
,_result,1,2019-09-26T16:36:48.868439105Z,2019-09-26T16:37:48.868439105Z,2019-09-26T16:37:47.795Z,23.45,fieldname2,myMeasurement,myTagvalue1,myTagvalue2


*/

Handle self signed certificates

influxdb-v2 uses fetch to communicate with InfluxDB API. You can pass options to fetch using fetchOptions in the constructor.

You can use a defined CA certificate like this:

const https = require('https');

const influxdb = new Influxdb({
  host: '',
  token: '',
  fetchOptions: {
    agent: new https.Agent({
      ca: [ fs.readFileSync('yourCaFile.ca') ],
    })
  }
});

You can ignore certificate checks too. ⚠️ It is not recommended at all and expose your to MITM attacks!

const https = require('https');

const influxdb = new Influxdb({
  host: '',
  token: '',
  fetchOptions: {
    agent: new https.Agent({
      rejectUnauthorized: false
    })
  }
});