Tempo.SQL.Meta (Tempo SQL v0.1.0)

View Source

JSON encoding and decoding for the meta column of the tempo_range and tempo_multirange composite types.

The range column of a composite carries the queryable span. The meta column carries everything else Tempo knows that tstzrange cannot express — qualifications, non-Gregorian calendars, zone identifiers, recurrence rules, interval metadata, and the implicit-vs-explicit-span distinction.

On store, the meta is a JSON document with this shape:

%{
  "v"           => 1,
  "from"        => iso8601_string | null,
  "to"          => iso8601_string | null,
  "recurrence"  => pos_integer | "infinity" | 1,
  "direction"   => 1 | -1,
  "duration"    => iso8601_string | null,
  "repeat_rule" => iso8601_string | null,
  "metadata"    => map            # must be JSON-serialisable
}

On load, the endpoints are reconstituted via Tempo.from_iso8601/1, which faithfully recovers every Tempo feature ISO 8601 / ISO 8601-2 / IXDTF can express.

Encoding uses Erlang's built-in :json module (OTP 27+); the jsonb column type handles storage and indexing on the Postgres side.

Summary

Functions

Decode a JSON string (or already-decoded map) back into a Tempo.Interval.t/0.

Decode a nil-aware JSON binary. Returns the decoded term directly.

Decoder options for :json.decode/3 that map JSON null to nil.

Encode a Tempo.Interval.t/0 to a JSON string for storage in a jsonb column.

Encode an arbitrary term as a nil-aware JSON binary.

Encoder callback for :json.encode/2 that treats nil as JSON null.

Functions

decode_interval(json)

@spec decode_interval(String.t() | map()) ::
  {:ok, Tempo.Interval.t()} | {:error, term()}

Decode a JSON string (or already-decoded map) back into a Tempo.Interval.t/0.

Returns {:ok, interval} or {:error, reason}.

decode_json(binary)

Decode a nil-aware JSON binary. Returns the decoded term directly.

decode_options()

Decoder options for :json.decode/3 that map JSON null to nil.

encode_interval(interval)

@spec encode_interval(Tempo.Interval.t()) :: iodata()

Encode a Tempo.Interval.t/0 to a JSON string for storage in a jsonb column.

Returns an iolist suitable for handing to Postgrex.

encode_json(term)

Encode an arbitrary term as a nil-aware JSON binary.

nil_aware_encoder(value, encode)

Encoder callback for :json.encode/2 that treats nil as JSON null.