PgFlow.Queries.Helpers (PgFlow v0.7.2)

Copy Markdown View Source

Shared utilities for database queries.

Provides RPC-style PostgreSQL function calls, UUID handling, row-to-map conversion, time range calculations, and status conversions.

Used by both PgFlow core query modules and the PgFlow Dashboard.

Summary

Functions

Normalizes an atom or string flow slug and rejects unsupported values.

Casts a UUID to its canonical string form and fails closed for invalid IDs.

Converts a direction atom to string for database queries.

Runs sql with params and discards the result.

Executes an RPC-style call to a PostgreSQL function.

Formats a binary UUID to string representation.

Converts a health status atom or string to string for database queries.

Casts an optional UUID while preserving nil.

Normalizes a UUID to its canonical string form for database queries.

Returns a positive :limit option or the supplied positive default.

Converts a status atom or string to string for database queries.

Converts a time range atom to a start DateTime.

Functions

cast_flow_slug(flow_slug)

@spec cast_flow_slug(term()) :: {:ok, String.t()} | {:error, :invalid_flow_slug}

Normalizes an atom or string flow slug and rejects unsupported values.

This only normalizes the Elixir value. Callers that interpolate a slug into SQL must additionally validate it with PgFlow's database slug rules.

cast_uuid(uuid)

@spec cast_uuid(term()) :: {:ok, Ecto.UUID.t()} | {:error, :invalid_id}

Casts a UUID to its canonical string form and fails closed for invalid IDs.

direction_to_string(dir)

@spec direction_to_string(:next | :prev | String.t()) :: String.t()

Converts a direction atom to string for database queries.

execute_ok(repo, sql, params)

@spec execute_ok(module(), String.t(), list()) :: :ok | {:error, term()}

Runs sql with params and discards the result.

Returns :ok when the query succeeds, or the query's {:error, reason}.

execute_rpc(repo, function_name, params, opts \\ [])

@spec execute_rpc(module(), String.t(), list(), keyword()) ::
  {:ok, nil | map() | [map()]}
  | {:error, :not_found | term()}
  | [map()]
  | integer()

Executes an RPC-style call to a PostgreSQL function.

Parameters

  • repo - The Ecto repository
  • function_name - Name of the PostgreSQL function
  • params - List of parameters to pass
  • opts - Options keyword list

Options

  • :schema - (required) The PostgreSQL schema containing the function
  • :mode - Result handling mode: :list, :single, :count, :void, :raw
  • :uuid_params - 1-based parameter positions holding canonical UUID strings; each is cast in SQL via $n::text::uuid so logs show readable IDs

Returns

Depends on mode:

  • :list - Returns list of maps
  • :single - Returns {:ok, map} or {:error, :not_found}
  • :count - Returns integer count
  • :void - Returns {:ok, nil}
  • :raw - Returns {:ok, list of maps} or error

format_uuid(uuid)

@spec format_uuid(nil | binary()) :: nil | String.t()

Formats a binary UUID to string representation.

health_status_to_string(status)

@spec health_status_to_string(nil | atom() | String.t()) :: nil | String.t()

Converts a health status atom or string to string for database queries.

optional_uuid(uuid)

@spec optional_uuid(term()) :: {:ok, Ecto.UUID.t() | nil} | {:error, :invalid_id}

Casts an optional UUID while preserving nil.

parse_uuid(uuid)

@spec parse_uuid(nil | String.t() | binary()) :: nil | binary()

Normalizes a UUID to its canonical string form for database queries.

Queries bind the result to a $n::text::uuid parameter so query logs show readable IDs instead of raw 16-byte binaries. Input that is not a valid UUID is returned unchanged and rejected by the database cast.

positive_limit(opts, default)

@spec positive_limit(keyword(), pos_integer()) :: pos_integer()

Returns a positive :limit option or the supplied positive default.

status_to_string(status)

@spec status_to_string(nil | atom() | String.t()) :: nil | String.t()

Converts a status atom or string to string for database queries.

time_range_start(arg1)

@spec time_range_start(atom()) :: DateTime.t()

Converts a time range atom to a start DateTime.