View Source PaperTrail behaviour (paper_trail v0.11.0)

Provide functions to insert, update and delete records on the database alongside their changes.

Usage:

defmodule MyApp.PaperTrail do
  use PaperTrail,
    repo: MyApp.Repo,
    originator_type: Ecto.UUID,
    item_type: Ecto.UUID
end

defmodule MyApp.Context do
  def create_user(params) do
    changeset = MyApp.User.create_changeset(params)
    # A `PaperTrail.Version` record with event `insert` is inserted
    MyApp.PaperTrail.insert(changeset)
  end

  def update_user(user, params) do
    changeset = MyApp.User.update_changeset(user, params)
    # A `PaperTrail.Version` record with event `update` is inserted
    MyApp.PaperTrail.update(changeset)
  end

  def delete_user(user) do
    # A `PaperTrail.Version` record with event `delete` is inserted
    MyApp.PaperTrail.delete(user)
  end
end

Summary

Functions

See PaperTrail.Serializer.add_prefix/2.

Deletes a record from the database with a related version insertion in one transaction

Same as delete/2 but returns only the model struct or raises if the changeset is invalid.

See PaperTrail.VersionQueries.get_current_model/2.

See PaperTrail.Serializer.get_item_type/1.

See PaperTrail.Serializer.get_model_id/1.

See PaperTrail.Serializer.get_sequence_from_model/2.

See PaperTrail.Serializer.get_sequence_id/2.

See PaperTrail.VersionQueries.get_version/1.

See PaperTrail.VersionQueries.get_version/2.

See PaperTrail.VersionQueries.get_version/3.

See PaperTrail.VersionQueries.get_versions/1.

See PaperTrail.VersionQueries.get_versions/2.

See PaperTrail.VersionQueries.get_versions/3.

Inserts a record to the database with a related version insertion in one transaction

Same as insert/2 but returns only the model struct or raises if the changeset is invalid.

See PaperTrail.Serializer.make_version_struct/3.

See PaperTrail.Serializer.serialize/2.

Updates a record from the database with a related version insertion in one transaction

Same as update/2 but returns only the model struct or raises if the changeset is invalid.

Updates all records from the database with a related version insertion in one transaction

Types

@type all_result() :: {integer(), nil | [any()]}
@type meta() :: map() | nil
@type multi_name() :: Ecto.Multi.name() | nil
@type options() ::
  []
  | [
      repo: repo(),
      strict_mode: strict_mode(),
      origin: origin(),
      meta: meta(),
      originator: originator(),
      prefix: prefix(),
      model_key: multi_name(),
      version_key: multi_name(),
      return_operation: multi_name(),
      returning: boolean(),
      repo_options: Keyword.t()
    ]
@type origin() :: String.t() | nil
@type originator() :: Ecto.Schema.t() | nil
@type prefix() :: String.t() | nil
@type queryable() :: Ecto.Queryable.t()
@type repo() :: module() | nil
@type result() :: {:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}
@type strict_mode() :: boolean() | nil
@type updates() :: Keyword.t()

Callbacks

@callback delete(Ecto.Changeset.t(), options()) :: result()
@callback delete!(Ecto.Changeset.t(), options()) :: Ecto.Schema.t()
@callback get_current_model(PaperTrail.Version.t()) :: Ecto.Schema.t()
@callback get_version(Ecto.Schema.t()) :: Ecto.Query.t()
Link to this callback

get_version(module, any)

View Source
@callback get_version(module(), any()) :: Ecto.Query.t()
Link to this callback

get_version(module, any, keyword)

View Source
@callback get_version(module(), any(), keyword()) :: Ecto.Query.t()
@callback get_versions(Ecto.Schema.t()) :: Ecto.Query.t()
Link to this callback

get_versions(module, any)

View Source
@callback get_versions(module(), any()) :: Ecto.Query.t()
Link to this callback

get_versions(module, any, keyword)

View Source
@callback get_versions(module(), any(), keyword()) :: Ecto.Query.t()
@callback insert(Ecto.Changeset.t(), options()) :: result()
@callback insert!(Ecto.Changeset.t(), options()) :: Ecto.Schema.t()
@callback update(Ecto.Changeset.t(), options()) :: result()
@callback update!(Ecto.Changeset.t(), options()) :: Ecto.Schema.t()
Link to this callback

update_all(queryable, updates, options)

View Source
@callback update_all(queryable(), updates(), options()) :: all_result()

Functions

Link to this function

add_prefix(changeset, prefix)

View Source

See PaperTrail.Serializer.add_prefix/2.

Link to this function

delete(struct, options \\ [])

View Source
@spec delete(Ecto.Changeset.t(), options()) :: result()

Deletes a record from the database with a related version insertion in one transaction

Link to this function

delete!(struct, options \\ [])

View Source
@spec delete!(Ecto.Schema.t(), options()) :: Ecto.Schema.t()

Same as delete/2 but returns only the model struct or raises if the changeset is invalid.

Link to this function

get_current_model(version, options \\ [])

View Source

See PaperTrail.VersionQueries.get_current_model/2.

See PaperTrail.Serializer.get_item_type/1.

See PaperTrail.Serializer.get_model_id/1.

Link to this function

get_sequence_from_model(changeset, options \\ [])

View Source

See PaperTrail.Serializer.get_sequence_from_model/2.

Link to this function

get_sequence_id(table_name, options \\ [])

View Source

See PaperTrail.Serializer.get_sequence_id/2.

See PaperTrail.VersionQueries.get_version/1.

Link to this function

get_version(model_or_record, id_or_options)

View Source

See PaperTrail.VersionQueries.get_version/2.

Link to this function

get_version(model, id, options)

View Source

See PaperTrail.VersionQueries.get_version/3.

See PaperTrail.VersionQueries.get_versions/1.

Link to this function

get_versions(model_or_record, id_or_options)

View Source

See PaperTrail.VersionQueries.get_versions/2.

Link to this function

get_versions(model, id, options)

View Source

See PaperTrail.VersionQueries.get_versions/3.

Link to this function

insert(changeset, options \\ [])

View Source
@spec insert(Ecto.Changeset.t(), options()) :: result()

Inserts a record to the database with a related version insertion in one transaction

Link to this function

insert!(changeset, options \\ [])

View Source
@spec insert!(Ecto.Schema.t(), options()) :: Ecto.Schema.t()

Same as insert/2 but returns only the model struct or raises if the changeset is invalid.

Link to this function

make_version_struct(version, model, options)

View Source

See PaperTrail.Serializer.make_version_struct/3.

Link to this function

serialize(data, options)

View Source

See PaperTrail.Serializer.serialize/2.

Link to this function

update(changeset, options \\ [])

View Source
@spec update(Ecto.Changeset.t(), options()) :: result()

Updates a record from the database with a related version insertion in one transaction

Link to this function

update!(changeset, options \\ [])

View Source
@spec update!(Ecto.Schema.t(), options()) :: Ecto.Schema.t()

Same as update/2 but returns only the model struct or raises if the changeset is invalid.

Link to this function

update_all(queryable, updates, options \\ [])

View Source
@spec update_all(queryable(), updates(), options()) :: all_result()

Updates all records from the database with a related version insertion in one transaction