PhoenixKitSync.ConnectionNotifier.Prepare (PhoenixKitSync v0.1.3)

Copy Markdown View Source

Record-transformation and value-preparation helpers shared across the import paths in ConnectionNotifier.

Three concerns:

  1. value/1 and value/3 — convert JSON-transported scalars back into Postgrex-ready terms. ISO8601 strings become DateTime / Date / Time. Decimal-like strings become %Decimal{} only when the target column is numeric (the 3-arity form) — the broad 1-arity form is kept for the PK/unique-key lookup paths where values are known keys, not free-text.
  2. numeric_columns/1 — returns the list of column names whose type is numeric/decimal/double precision/real for a given table. Used by the importer to scope decimal-string detection. Cached once per table-import; empty list on error (safe "don't coerce" fallback).
  3. Record field helpers (get_field/2, put_field/3, drop_field/2, normalize_keys/1) — records arrive with either string or atom keys depending on whether they came through JSON or internal code. These helpers access/modify fields uniformly without creating atoms from untrusted data.

Extracted from ConnectionNotifier in 2026-04. All functions are pure or depend only on SchemaInspector (for column introspection) — none touch HTTP, the socket, or the websocket client.

Summary

Functions

drop_field(record, field)

@spec drop_field(map(), String.t()) :: map()

get_field(record, field)

@spec get_field(map(), String.t()) :: any()

normalize_keys(record)

@spec normalize_keys(map()) :: map()

numeric_columns(table_name)

@spec numeric_columns(String.t()) :: [String.t()]

put_field(record, field, value)

@spec put_field(map(), String.t(), any()) :: map()

value(value)

@spec value(any()) :: any()

value(value, column, numeric_cols)

@spec value(any(), String.t(), [String.t()]) :: any()