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
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.
@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.
Converts a direction atom to string for database queries.
Runs sql with params and discards the result.
Returns :ok when the query succeeds, or the query's {:error, reason}.
@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 repositoryfunction_name- Name of the PostgreSQL functionparams- List of parameters to passopts- 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::uuidso 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
Formats a binary UUID to string representation.
Converts a health status atom or string to string for database queries.
@spec optional_uuid(term()) :: {:ok, Ecto.UUID.t() | nil} | {:error, :invalid_id}
Casts an optional UUID while preserving nil.
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.
@spec positive_limit(keyword(), pos_integer()) :: pos_integer()
Returns a positive :limit option or the supplied positive default.
Converts a status atom or string to string for database queries.
@spec time_range_start(atom()) :: DateTime.t()
Converts a time range atom to a start DateTime.