Ecto.SoftDelete.Repo behaviour (ecto_soft_delete v2.1.0)

Adds soft delete functions to an repository.

defmodule Repo do
  use Ecto.Repo,
    otp_app: :my_app,
    adapter: Ecto.Adapters.Postgres
  use Ecto.SoftDelete.Repo
end

Summary

Callbacks

Soft deletes a struct. Updates the deleted_at field with the current datetime in UTC. It returns {:ok, struct} if the struct has been successfully soft deleted or {:error, changeset} if there was a validation or a known constraint error.

Same as soft_delete/2 but returns the struct or raises if the changeset is invalid.

Soft deletes all entries matching the given query.

Callbacks

Link to this callback

soft_delete(struct_or_changeset, opts)

@callback soft_delete(
  struct_or_changeset :: Ecto.Schema.t() | Ecto.Changeset.t(),
  opts :: Keyword.t()
) :: {:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}

Soft deletes a struct. Updates the deleted_at field with the current datetime in UTC. It returns {:ok, struct} if the struct has been successfully soft deleted or {:error, changeset} if there was a validation or a known constraint error.

Options

All options supported by Ecto.Repo.update/2 can be used.

Examples

post = MyRepo.get!(Post, 42)
case MyRepo.soft_delete post do
  {:ok, struct}       -> # Soft deleted with success
  {:error, changeset} -> # Something went wrong
end

# With schema prefix for multi-tenant databases
MyRepo.soft_delete(post, prefix: "tenant_abc")
Link to this callback

soft_delete!(struct_or_changeset, opts)

@callback soft_delete!(
  struct_or_changeset :: Ecto.Schema.t() | Ecto.Changeset.t(),
  opts :: Keyword.t()
) :: Ecto.Schema.t()

Same as soft_delete/2 but returns the struct or raises if the changeset is invalid.

Link to this callback

soft_delete_all(queryable, opts)

@callback soft_delete_all(queryable :: Ecto.Queryable.t(), opts :: Keyword.t()) ::
  {integer(), nil | [term()]}

Soft deletes all entries matching the given query.

It returns a tuple containing the number of entries and any returned result as second element. The second element is nil by default unless a select is supplied in the update query.

Options

All options supported by Ecto.Repo.update_all/3 can be used.

Examples

MyRepo.soft_delete_all(Post)
from(p in Post, where: p.id < 10) |> MyRepo.soft_delete_all()

# With schema prefix for multi-tenant databases
MyRepo.soft_delete_all(Post, prefix: "tenant_abc")