Versioned (Versioned v0.1.0) View Source

Tools for operating on versioned records.

Link to this section Summary

Functions

Deletes a struct using its primary key and adds a deleted version.

Get a version for a schema module.

List all versions for a schema module, newest first.

Get the query to fetch all the versions for a schema, newest first.

Inserts a versioned struct defined via Ecto.Schema or a changeset.

Get the timestamp for the very first version of this entity.

Preload version associations.

Updates a changeset (of a versioned schema) using its primary key.

True if the given module or struct is a version.

Get the version module from the subject module.

True if the Ecto.Schema module is versioned.

Build the query to populate the :version_id virtual field on a versioned entity.

Link to this section Functions

Link to this function

delete(struct_or_changeset, opts \\ [])

View Source

Specs

delete(
  struct_or_changeset :: Ecto.Schema.t() | Ecto.Changeset.t(),
  opts :: Keyword.t()
) ::
  {:ok, Ecto.Schema.t()}
  | {:error, any()}
  | {:error, Ecto.Multi.name(), any(), %{required(Ecto.Multi.name()) => any()}}

Deletes a struct using its primary key and adds a deleted version.

Link to this function

get(module, id, opts \\ [])

View Source

Specs

get(module(), any(), keyword()) :: Ecto.Schema.t() | nil

Get a version for a schema module.

Options can include anything used by the repo's get/3.

Link to this function

history(module_or_struct, id_or_opts \\ [], opts \\ [])

View Source

Specs

history(module() | Ecto.Schema.t(), any(), keyword()) :: [Ecto.Schema.t()]

List all versions for a schema module, newest first.

History will be found based on a module name and id or pass in a struct.

Options can include anything used by the repo's all/2 and history_query/3.

Link to this function

history_query(module, id, opts \\ [])

View Source

Specs

history_query(module(), any(), keyword()) :: Ecto.Queryable.t()

Get the query to fetch all the versions for a schema, newest first.

Options

  • :limit - Max number of records to return. Default: return all records.
Link to this function

insert(struct_or_changeset, opts \\ [])

View Source

Specs

insert(Ecto.Schema.t() | Ecto.Changeset.t(), keyword()) ::
  {:ok, Ecto.Schema.t()}
  | {:error, any()}
  | {:error, Ecto.Multi.name(), any(), %{required(Ecto.Multi.name()) => any()}}

Inserts a versioned struct defined via Ecto.Schema or a changeset.

This function calls to the Ecto.Repo module twice -- once to insert the record itself, and once to insert a copy as the first version in the versions table.

Specs

inserted_at(struct()) :: DateTime.t() | nil

Get the timestamp for the very first version of this entity.

Link to this function

preload(list_or_struct, preload)

View Source

Specs

preload(Ecto.Schema.t() | [Ecto.Schema.t()], atom() | list()) ::
  Ecto.Schema.t() | [Ecto.Schema.t()]

Preload version associations.

Example

iex> pv = Repo.get(Person.Version, "7f85b58b-ef57-4288-ade0-ff47f0ceb116")
iex> Versioned.preload(pv, :fancy_hobby_versions)
%Person.Version{
  id: "7f85b58b-ef57-4288-ade0-ff47f0ceb116",
  fancy_hobby_versions: [
    %{id: "a2a911fb-e2a6-459c-93e2-616be0fa1a45", name: "Jenga"}
  ]
}
Link to this function

update(changeset, opts \\ [])

View Source

Specs

update(Ecto.Changeset.t(), keyword()) ::
  {:ok, Ecto.Schema.t()}
  | {:error, any()}
  | {:error, Ecto.Multi.name(), any(), %{required(Ecto.Multi.name()) => any()}}

Updates a changeset (of a versioned schema) using its primary key.

This function uses the Ecto.Repo module, first calling update/2 to update the record itself, and then insert/1 to add a copy of the new version to the versions table.

Specs

version?(module() | Ecto.Schema.t()) :: boolean()

True if the given module or struct is a version.

Specs

version_mod(module()) :: module()

Get the version module from the subject module.

Specs

versioned?(module() | Ecto.Schema.t()) :: boolean()

True if the Ecto.Schema module is versioned.

This means there is a corresponding Ecto.Schema module with an extra ".Version" on the end.

Link to this function

with_versions(query, mod \\ nil)

View Source

Specs

with_versions(Ecto.Queryable.t(), Ecto.Schema.t() | nil) :: Ecto.Query.t()

Build the query to populate the :version_id virtual field on a versioned entity.

query may be any existing base query for the entity which is versioned. mod, if defined, should be the entity module name itself. If not defined, query must be this module name and not any type of query.