Giter Club home page Giter Club logo

soft_repo's Introduction

Build Status Hex.pm License: MIT

SoftRepo

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

Installation

  • this package can be installed by adding soft_repo to your list of dependencies in mix.exs:
def deps do
  [
    {:soft_repo, "~> 0.2.0"}
  ]
end
  • Configure soft_repo to use your application repo in config/config.exs:
config :soft_repo, repo: YourApplicationName.Repo
  • Migrations for schemas to add support for soft deletion, add soft_repo_column() when creating/modifing a table
  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
  • Schema

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
  • Queries Now you can call SoftRepo gobally or alias to your Repo
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
  • Pagination, to do pagination (using scrivener) I added a function to use in your Repo injected pagination:
SoftRepo.paginate(MyApp.User) # will exclude soft deleted records paginated

Inspiration

TODO's

  • Use Metaprogramming to change from set config Repo to inject into a module.
  • Support associations, like has_many(:whatevers, on_delete: :soft_delete)

soft_repo's People

Contributors

carakan avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.