Phoenix.Sync.Writer.Operation (Phoenix.Sync v0.4.3)

View Source

Represents a mutation operation received from a client.

To handle custom formats, translate incoming changes into a Operation struct using new/4.

Summary

Functions

Takes data from a mutation and validates it before returning a struct.

Types

new_result()

@type new_result() :: {:ok, t()} | {:error, String.t()}

t()

@type t() :: %Phoenix.Sync.Writer.Operation{
  changes: map(),
  data: map(),
  index: term(),
  operation: :insert | :update | :delete,
  relation: binary() | [binary(), ...]
}

Functions

new(operation, table, data, changes)

@spec new(binary() | atom(), binary() | [binary(), ...], map() | nil, map() | nil) ::
  new_result()

Takes data from a mutation and validates it before returning a struct.

Parameters

  • operation one of "insert","INSERT",:insert,:INSERT,"update","UPDATE",:update,:UPDATE,"delete","DELETE",:delete,:DELETE
  • table the client table name for the write. Can either be a plain string name "table" or a list with ["schema", "table"].
  • data the original values (see Updates vs Inserts vs Deletes)
  • changes any updates to apply (see Updates vs Inserts vs Deletes)

Updates vs Inserts vs Deletes

The Phoenix.Sync.Writer.Operation struct has two value fields, data and changes.

data represents what's already in the database, and changes what's going to be written over the top of this.

For insert operations, data is ignored so the new values for the inserted row should be in changes.

For deletes, changes is ignored and data should contain the row specification to delete. This needn't be the full row, but must contain values for all the primary keys for the table.

For updates, data should contain the original row values and changes the changed fields.

These fields map to the arguments Ecto.Changeset.change/2 and Ecto.Changeset.cast/4 functions, data is used to populate the first argument of these functions and changes the second.

new!(operation, table, data, changes)

@spec new!(binary() | atom(), binary() | [binary(), ...], map() | nil, map() | nil) ::
  t()