Versioned (Versioned v0.3.1) View Source

Tools for operating on versioned records.

Link to this section Summary

Functions

Given a versioned struct, populate its :version_id field.

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

Proxy function for the given repo module's get/3.

Proxy function for the given repo module's get_by/3.

Get the most recent version of module with the given entity_id.

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.

Proxy function for the given repo module's one/3.

Preload version associations.

Updates a versioned changeset 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

Specs

add_version_id(map()) :: map()

Given a versioned struct, populate its :version_id field.

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, ver_id, opts \\ [])

View Source

Specs

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

Proxy function for the given repo module's get/3.

Link to this function

get_by(queryable, clauses, opts \\ [])

View Source

Specs

get_by(Ecto.Queryable.t(), keyword() | map(), keyword()) ::
  Ecto.Schema.t() | nil

Proxy function for the given repo module's get_by/3.

Link to this function

get_last(module, entity_id, opts \\ [])

View Source

Specs

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

Get the most recent version of module with the given entity_id.

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.

Specs

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

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

Link to this function

one(queryable, opts \\ [])

View Source

Specs

one(
  Ecto.Queryable.t(),
  keyword()
) :: Ecto.Schema.t() | nil

Proxy function for the given repo module's one/3.

Link to this function

preload(list_or_struct, preload)

View Source

Specs

preload(Ecto.Schema.t() | [Ecto.Schema.t()] | nil, atom() | list() | nil) ::
  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 versioned changeset using its primary key.

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_version_id(queryable, mod \\ nil)

View Source

Specs

with_version_id(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.