Build StatusHex.pmLicense: MIT

SoftRepo

Simple implementation of soft delete using repository pattern (Ecto Repo).

Installation

def deps do
  [
    {:soft_repo, "~> 0.2.0"}
  ]
end
config :soft_repo, repo: YourApplicationName.Repo
  defmodule MyApp.Repo.Migrations.CreateWhatever do
    use Ecto.Migration

    def change do
      create table(:whatever) do
        add(:my_field, :string)
        add(:my_other_field, :string)
        timestamps()
        SoftRepo.Migration.soft_repo_column()
      end
    end
  end

Import SoftRepo.Schema into your module, then add soft_repo_schema() to your schema block:

  defmodule Whatever do
    use Ecto.Schema
    import SoftRepo.Schema

    schema "users" do
      field(:email, :string)
      soft_repo_schema()
    end
  end
SoftRepo.get(MyApp.User, 1) # will return nil if record is in soft delete state
SoftRepo.get(MyApp.User, 1, with_thrash: true) # will return the soft deleted record
SoftRepo.all(MyApp.User) # will exclude soft deleted records
SoftRepo.all(MyApp.User, with_thrash: true) # will include soft deleted records
SoftRepo.delete(user) # will update the deleted_at column
SoftRepo.delete(user, force: true) # will permanently delete the record
SoftRepo.delete_all(MyApp.User) # will updated the deleted_at columns
SoftRepo.delete_all(MyApp.User, force: true) # will permanently delete all records
SoftRepo.restore(MyApp.User, 1) # will restore back the soft deleted record

Inspiration