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-light

v1.1.6

Published

A light-weight client to read & write data to InfluxDb v1 and v2. This client supports both InfluxQL(SQL) and Flux queries

Downloads

38

Readme

influxdb-light

A light-weight client to read and write data to InfluxDb V1 and V2.
It supports both InfluxQL(SQL) and Flux queries.
This light-weight client is incredibly easy to use.
Handling of tags and fields is straightforward with no additional complexity.

Good to know

You have to create a DBRP mapping if you want to use InfluxQL(SQL) on your V2 buckets.
For more details on DBRP mapping please read this guide.

Installation

npm i influxdb-light

Usage

InfluxDb Version 1 Example

import { InfluxDB } from "influxdb-light";

const request = { 
    host: "localhost", 
    port: "8086", 
    protocol: "http", 
    username: "<username>", 
    password: "<password>" 
};
const influxDb = new InfluxDB(request);
const dbName = "demo1";
const measurement = "energy";

//Write to InfluxDb
influxDb.writeV1({
            measurement: measurement,
            payload: [{ //You can store multiple data point at once.
                deviceId: "d007",
                location: "L007",
                type: 7,
                enabled: true,
                valueX: 100,
                valueY: 120,
                ip: "192.168.0.2"
            }, {
                deviceId: "d009",
                location: "L009",
                type: 9,
                enabled: false,
                valueX: 110,
                valueY: 130,
                ip: "192.168.0.3"
            }],
            tags: ["deviceId", "location", "type", "enabled"],
            timestamp: 1664821800000000000 //timestamp is optional. Use this if you want to explicitly set different time.
        }, 
        dbName,
        // queryParams is optional. see WriteV1QueryParams
        {
            rp: 'autogen', // retention policy
            consistency: 'all'
        })
        .then(res => {
            console.log(res);
            //Output: [] //Empty array indicates data added
        }).catch(err => {
            console.error(err);
        });

//Read from InfluxDb
influxDb.queryV1(dbName, `SELECT * FROM ${measurement} WHERE "deviceId"='d007'`)
    .then(res => {
        console.log(res);
        //Output: 
        // [
        //     {
        //         "statement_id": <number>,
        //         "series": [
        //             {
        //                 "name": <string>,
        //                 "columns": Array<string>,
        //                 "values": Array<Array<string | number | boolean | null>>
        //             }
        //         ]
        //     }
        // ]
    }).catch(err => {
        console.error(err);
    });

InfluxDb Version 2 Example

import { InfluxDB } from "influxdb-light";

const request = { 
    host: "localhost", 
    port: "8086", 
    protocol: "http", 
    token: "<token_value>" 
};
const influxDb = new InfluxDB(request);
const org = "demo_org";
const dbName = "demo1";
const measurement = "energy";
const precision = "ns"; // precision is unit about timestamp of payload. and it's optional parameter.

//Write to InfluxDb
influxDb.writeV2({
            measurement: measurement,
            payload: [{ //You can store multiple data point at once.
                deviceId: "d007",
                location: "L007",
                type: 7,
                enabled: true,
                valueX: 100,
                valueY: 120,
                ip: "192.168.0.2"
            }, {
                deviceId: "d009",
                location: "L009",
                type: 9,
                enabled: false,
                valueX: 110,
                valueY: 130,
                ip: "192.168.0.3"
            }],
            tags: ["deviceId", "location", "type", "enabled"],
            timestamp: 1664821800000000000 //timestamp is optional. Use this if you want to explicitly set different time.
        }, org, dbName, precision)
        .then(res => {
            console.log(res);            
            //Output: [] //Empty array indicates data added
        }).catch(err => {
            console.error(err);
        });

//Read from InfluxDb
influxDb.queryV2("orgtest", `from(bucket:"demo1") |> 
    range(start: -60d) |> 
    filter(fn: (r) => r._measurement == "energy") |> 
    filter(fn: (r) => r.deviceId == "d007") |> 
    pivot(rowKey: ["_time"], columnKey: ["_field"], valueColumn: "_value")`))
    .then(res => {
        console.log(res);
        //Output: /*Returns string with comma separation. To be parsed as csv*/
        //,result,table,_start,_stop,_time,_measurement,deviceId,enabled,location,type,ip,valueX,valueY
        //,_result,0,2022-07-11T10:50:17.6390562Z,2022-09-09T10:50:17.6390562Z,
            //2022-09-09T10:46:09.3298829Z,energy,d007,false,L007,7,192.168.0.2,100,120
    }).catch(err => {
        console.error(err);
    });

Examples of Bucket operations on InfluxDB V2.x.x

import { InfluxDB } from "influxdb-light";
const influxDb = new InfluxDB({ host: "localhost", port: "8086", protocol: "http", token: "<token_value>" });

//Create a Bucket
influxDb.createBucket(<org_ID>, <bucket_name>, <retention_in_seconds>)
    //output: { bucketId: <string>, name: <string>, createdAt: <string>, retentionTime: <number> }

//Get specific bucket
//i). By Bucket ID
influxDb.getBucket("640bee9277f75f2d", { id: "10592e2bff3d1789" }).then(result => console.log(result));
//ii). By Bucket Name
influxDb.getBucket("640bee9277f75f2d", { name: "testBucket007" }).then(result => console.log(result));
    //output: { bucketId: <string>, name: <string>, createdAt: <string>, retentionTime: <number> }

//List All Buckets
influxDb.listBuckets("640bee9277f75f2d").then(result => console.log(result));
    //output: Array<{ bucketId: <string>, name: <string>, createdAt: <string>, retentionTime: <number> }>