Electric.Plug.Utils (electric v1.0.17)

View Source

Utility functions for Electric endpoints, e.g. for parsing and validating path and query parameters.

Summary

Functions

Calculate the next interval that should be used for long polling based on the current time and previous interval used.

Parse columns parameter from a string consisting of a comma separated list of potentially quoted column names into a sorted list of strings.

Functions

common_open_telemetry_attrs(conn)

get_next_interval_timestamp(long_poll_timeout_ms, prev_interval \\ nil)

@spec get_next_interval_timestamp(integer(), binary() | nil) :: integer()

Calculate the next interval that should be used for long polling based on the current time and previous interval used.

Timestamp returned is in seconds and uses a custom epoch of 9th of October 2024, UTC.

parse_columns_param(columns)

@spec parse_columns_param(binary()) :: {:ok, [String.t(), ...]} | {:error, term()}

Parse columns parameter from a string consisting of a comma separated list of potentially quoted column names into a sorted list of strings.

Examples

iex> Electric.Plug.Utils.parse_columns_param("")
{:error, "Invalid zero-length delimited identifier"}
iex> Electric.Plug.Utils.parse_columns_param("foo,")
{:error, "Invalid zero-length delimited identifier"}
iex> Electric.Plug.Utils.parse_columns_param("id")
{:ok, ["id"]}
iex> Electric.Plug.Utils.parse_columns_param("id,name")
{:ok, ["id", "name"]}
iex> Electric.Plug.Utils.parse_columns_param(~S|"PoT@To",PoTaTo|)
{:ok, ["PoT@To", "potato"]}
iex> Electric.Plug.Utils.parse_columns_param(~S|"PoTaTo,sunday",foo|)
{:ok, ["PoTaTo,sunday", "foo"]}
iex> Electric.Plug.Utils.parse_columns_param(~S|"fo""o",bar|)
{:ok, [~S|fo"o|, "bar"]}
iex> Electric.Plug.Utils.parse_columns_param(~S|"id,"name"|)
{:error, ~S|Invalid unquoted identifier contains special characters: "id|}