Timeseries Database
The Influx class provides a client for interacting with an InfluxDB database. This database is specifically designed for storing and querying time-series data, such as sensor readings (IoT), application metrics, or financial data.
A key feature of this class is its support for downsampling. It can automatically manage data resolution, keeping high-frequency data for recent timeframes while aggregating older data into lower-resolution buckets (e.g., keeping raw data for 24 hours, but only daily averages for data older than a year).
Constructor and Member Functions
create
Creates an instance of the InfluxDB client, configuring it to connect to a specific database URL with the necessary credentials.
Parameters
url: The URL of your InfluxDB instance (e.g.,http://localhost:8086).token: The authentication token with the required permissions for your organization and buckets.org: The name of the organization in InfluxDB.
Example
writePoint
Writes a single data point to a specific bucket and measurement. This is the standard way to record a piece of data.
Parameters
bucket: The name of the bucket to write to.measurement: The name of the measurement (e.g., "temperature", "cpu_load").data: The value to record. Can be a number, string, boolean, or a JSON object.tags: An optional object of key-value pairs to tag the data (e.g.,{ sensorId: "s1" }).
Example
Output
Returns true on success.
writePoints
Writes multiple data points at once to a specific bucket and measurement. This is more efficient than calling writePoint multiple times in a loop.
Parameters
bucket: The name of the bucket.measurement: The name of the measurement.data: An array of values to record.tags: Optional tags. If provided as an array, it must match the length of thedataarray (one tag object per data point). If provided as a single object, it applies to all points.
Example
Output
Returns true on success.
writeDownsampled
Writes numeric data to the high-frequency bucket (H+) specifically for the purpose of automatic downsampling. The system will automatically aggregate this data into lower-resolution buckets (daily, weekly, etc.) over time based on the internal pipeline.
Parameters
measurement: The name of the measurement.data: The numeric value or array of numbers to store.tags: Optional tags to associate with the data.
Example
Output
Returns true on success.
read
Reads time-series data from a specific bucket and measurement. It offers powerful options for filtering by time, limiting results, and aggregating data (e.g., calculating averages) into time windows.
Parameters
bucket: The name of the bucket to query.measurement: The name of the measurement.options: An optional object to refine the query.start: The earliest time to include (e.g.,'-12h','-7d','2025-01-01T00:00:00Z'). Defaults to'-1y'.stop: The latest time to include. Defaults to'now()'.limit: Limits the result to the firstndata points.tail: Limits the result to the lastndata points.every: Duration of time windows for aggregation (e.g.,'15m'). Requiresfunc.func: The aggregation function (e.g.,'mean','sum','count','last').tags: An object of tags to filter by.
Example 1: Get raw data from the last hour
Example 2: Get average temperature every 15 minutes for the last day
Output
An array of objects, where each object has a date (ISO timestamp) and a value.
readDownsampled
A "smart" query function that automatically stitches together data from different aggregated buckets. It retrieves high-resolution data for recent timeframes and lower-resolution (aggregated) data for older timeframes, providing an optimized view of long-term history without processing millions of raw data points.
Parameters
measurement: The name of the measurement.options: Query options.start: Earliest time (default'-1y').stop: Latest time (default'now()').limit: Limit to firstnresults.tail: Limit to lastnresults.tags: Filter by tags.
Example
Output
An array of objects containing statistical data (mean, min, max, count) for each time point.
query
Allows you to execute a raw Flux query string. This provides maximum flexibility for complex database operations not covered by the helper functions.
Parameters
flux: The raw Flux query string.
Example
Output
The raw result rows from InfluxDB.
delete
Deletes data from a measurement. You can delete the entire measurement or specify a time range to delete only a portion of the data.
Parameters
bucket: The name of the bucket.measurement: The name of the measurement to delete.options: Time range options.start: Start time (defaults to1970-01-01...).stop: End time (defaults to now).
Example
Output
Returns true on success.
flush
Manually forces any buffered data (pending writes) to be sent to the database immediately. This is useful during testing or before shutting down a process to ensure no data is lost.
Output
Returns true (void promise) when complete.
reset
Danger Zone. This function deletes all data from all measurements in all buckets connected to this instance. The buckets themselves are preserved, but they will be empty.
Output
Returns true when the reset is complete.
listBuckets
Retrieves a list of all available buckets in the connected organization.
Parameters
None.
Output
An array of bucket objects.
listMeasurements
Lists detailed information about all measurements across all buckets.
Parameters
options:includeStats: Iftrue, calculates row count and cardinality (Warning: this can be slow).statsRangeStart: Time range for stats (default'-1y').
Example
Output
An array of measurement details.
getMeasurementDetails
Retrieves the schema (fields and tags) for a specific measurement in a specific bucket.
Parameters
bucket: The bucket name.measurement: The measurement name.
Example
Output
getMeasurementStats
Calculates statistics (row count and cardinality) for a specific measurement over a given time range.
Parameters
bucket: The bucket name.measurement: The measurement name.options:start: Start time (default'-30d').stop: End time (default'now()').
Example
Output
Last updated
