Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

github.com/benjamin658/influx-query-builder

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/benjamin658/influx-query-builder

  • v1.6.0
  • Source
  • Go
  • Socket score

Version published
Created
Source

Influx Query Builder

Build Status Coverage Status Codacy Badge Go Report Card

The super lightweight InfluxDB query builder implemented in Go.

Installation

go get -u github.com/benjamin658/influx-query-builder

Query Builder Usage

Simple query

builder := New()
query := builder.
  Select("temperature", "humidity").
  From("measurement).
  Build()

Output:

SELECT "temperature","humidity" FROM "measurement"

Select AS

builder := New()
query := builder.
  Select("temperature AS temp", "humidity AS hum").
  From("measurement).
  Build()

Output:

SELECT "temperature" AS "temp","humidity" AS "hum" FROM "measurement"

Function query

builder := New()
query := builder.
  Select(`MEAN("temperature")`, `SUM("humidity")`).
  From("measurement").
  Build()

Output:

SELECT MEAN("temperature"),SUM("humidity") FROM "measurement"

Function AS

builder := New()
query := builder.
  Select(`MEAN("temperature") AS mt`, `SUM("humidity") AS sh`).
  From("measurement").
  Build()

Output:

SELECT MEAN("temperature") AS "mt",SUM("humidity") AS "sh" FROM "measurement"

Query with criteria

builder := New()
query := builder.
  Select("temperature", "humidity").
  From("measurement").
  Where("time", ">", "2018-11-01T06:33:57.503Z").
  And("time", "<", "2018-11-02T09:35:25Z").
  Or("tag", "=", "t").
  Build()

Output:

SELECT "temperature","humidity" FROM "measurement" WHERE "time" > '2018-11-01T06:33:57.503Z' AND "time" < '2018-11-02T09:35:25Z' OR "tag" = 't'

Brackets criteria

Noted: If you use Where with WhereBrackets, Where will override the WhereBrackets.

Where Brackets
builder := New()
query := builder.
  Select("temperature", "humidity").
  From("measurement").
  WhereBrackets(
    // Passing a new builder as the param
    New().
      Where("time", ">", "2018-11-01T06:33:57.503Z").
      And("time", "<", "2018-11-02T09:35:25Z").
  ).
  Or("tag", "=", "t").
  Build()

Output:

SELECT "temperature","humidity" FROM "measurement" WHERE ("time" > '2018-11-01T06:33:57.503Z' AND "time" < '2018-11-02T09:35:25Z') OR "tag" = 't'
And Brackets
builder := New()
query := builder.
  Select("temperature", "humidity").
  From("measurement").
  Where("time", ">", "2018-11-01T06:33:57.503Z").
  AndBrackets(
    // Passing a new builder as the param
    New().
      Where("time", "<", "2018-11-02T09:35:25Z").
      Or("tag", "=", "t"),
  ).
  Build()

Output:

SELECT "temperature","humidity" FROM "measurement" WHERE "time" > '2018-11-01T06:33:57.503Z' AND ("time" < '2018-11-02T09:35:25Z' OR "tag" = 't')
Or Brackets
builder := New()
query := builder.
  Select("temperature", "humidity").
  From("measurement").
  Where("time", ">", "2018-11-01T06:33:57.503Z").
  OrBrackets(
    // Passing a new builder as the param
    New().
      Where("time", "<", "2018-11-02T09:35:25Z").
      Or("tag", "=", "t"),
  ).
  Build()

Output:

SELECT "temperature","humidity" FROM "measurement" WHERE "time" > '2018-11-01T06:33:57.503Z' OR ("time" < '2018-11-02T09:35:25Z' OR "tag" = 't')

Group By time duration

builder := New()
duration := NewDuration()
query := builder.
  Select("temperature", "humidity").
  From("measurement").
  GroupByTime(duration.Minute(10)).
  Build()

Output:

SELECT "temperature","humidity" FROM "measurement" GROUP BY time(10m)

Group By Tag

builder := New()
query := builder.
  Select("temperature", "humidity").
  From("measurement").
  GroupByTag("sensorId").
  Build()

Output:

SELECT "temperature","humidity" FROM "measurement" GROUP BY sensorId

Order By time

builder := New()
query := builder.
  Select("temperature", "humidity").
  From("measurement").
  Desc().
  Build()

Output:

SELECT "temperature","humidity" FROM "measurement" ORDER BY time DESC

Limit and Offset

builder := New()
query := builder.
  Select("temperature", "humidity").
  From("measurement").
  Limit(10).
  Offset(5).
  Build()

Output:

SELECT "temperature","humidity" FROM "measurement" LIMIT 10 OFFSET 5

Reset builder and get a new one

builder := New()
// some code...
builder = builder.Clean()

Get current query struct

builder := New()
query := builder.
  Select("temperature", "humidity").
  From("measurement").
  Limit(100).
  Offset(100).
  Asc().
  GetQueryStruct()

/*
type CurrentQuery struct {
  Measurement   string
  Where         Tag
  And           []Tag
  Or            []Tag
  WhereBrackets QueryBuilder
  AndBrackets   []QueryBuilder
  OrBrackets    []QueryBuilder
  Fields        []string
  GroupBy       string
  Limit         uint
  Offset        uint
  Order         string
  IsLimitSet    bool
  IsOffsetSet   bool
}
*/

Deprecated

Group By time

builder := New()
query := builder.
  Select("temperature", "humidity").
  From("measurement").
  GroupBy("10m").
  Build()

Output:

SELECT "temperature","humidity" FROM "measurement" GROUP BY time(10m)

License


© Ben Hu (benjamin658), 2018-NOW

Released under the MIT License

FAQs

Package last updated on 09 Oct 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc