EctoTimescaledb

Elixir CIHex.pmHex.pm

Extend Ecto.Query to write TimescaleDB’s SQL as builtin

Documentation

Installation

The package can be installed by adding ecto_timescaledb to your list of dependencies in mix.exs:

def deps do
  [
    {:ecto_timescaledb, "~> 0.7.0"}
  ]
end

Quick start

Create time-series table in migration

use Ecto.Migration.Timescaledb

def up do
  create table(:test_table, primary_key: false) do
    add :time, :naive_datetime, null: false
    add :example, :string
  end

  create_hypertable(:test_table, :time)
end

def down do
  drop(table(:test_table))
end

time-series query example

import Ecto.Query
import Ecto.Query.Timescaledb

from(s in Stat,
  select: [time_bucket("7 days", time, bucket), sum(s.avg) ~> sum_avg],
  where: fragment("sum_avg") > 100,
  group_by: fragment("bucket"),
  order_by: [desc: fragment("bucket")]
)