Chronicle.Ecto (chronicle v0.1.2)

Copy Markdown

Low-level conversion of completed Ecto operations into version events.

Application code normally uses Chronicle.insert/2, Chronicle.update/2, Chronicle.delete/2, and Chronicle.Multi.

Summary

Functions

Returns whether a completed operation has anything to audit.

Returns normalized field transitions for a changeset.

Builds an event for a completed Ecto operation.

Types

operation()

@type operation() :: :insert | :update | :delete

Functions

auditable?(operation, field_changes, opts \\ [])

@spec auditable?(operation(), [map()], keyword()) :: boolean()

Returns whether a completed operation has anything to audit.

An update whose tracked field changes are empty records nothing. That covers a changeset with no changes at all, and an update touching only fields the schema policy excluded with only or except — by the application's own declaration, nothing auditable happened.

A caller who supplied :data or :type is recording a semantic fact rather than a diff, so the event is kept. Inserts and deletes are always audited: creating or destroying the row is the fact, independent of which fields are tracked.

changes(operation, changeset, result, opts \\ [])

@spec changes(operation(), Ecto.Changeset.t(), struct(), keyword()) :: [map()]

Returns normalized field transitions for a changeset.

Only fields present in changeset.changes are included. Database-generated fields are therefore not misrepresented as user changes.

event(operation, changeset, result, opts \\ [])

Builds an event for a completed Ecto operation.

result is the inserted, updated, or deleted schema returned by the repo. Configure :redact with field atoms/strings or a (field, value -> value) function. Common credential fields are redacted by default.