Giter Site home page Giter Site logo

ecto_soft_delete's Introduction

Build Status Hex.pm License: MIT Coverage Status

EctoSoftDelete

Adds columns, fields, and queries for soft deletion with Ecto.

Documentation

Usage

Migrations

In migrations for schemas to support soft deletion, import Ecto.SoftDelete.Migration. Next, add soft_delete_columns() when creating a table

defmodule MyApp.Repo.Migrations.CreateUser do
  use Ecto.Migration
  import Ecto.SoftDelete.Migration

  def change do
    create table(:users) do
      add :email, :string
      add :password, :string
      timestamps()
      soft_delete_columns()
    end
  end
end

Schemas

Import Ecto.SoftDelete.Schema into your Schema module, then add soft_delete_schema() to your schema

  defmodule User do
    use Ecto.Schema
    import Ecto.SoftDelete.Schema

    schema "users" do
      field :email,           :string
      soft_delete_schema()
    end
  end

Queries

To query for items that have not been deleted, use with_undeleted(query) which will filter out deleted items using the deleted_at column produced by the previous 2 steps

import Ecto.SoftDelete.Query

query = from(u in User, select: u)
|> with_undeleted

results = Repo.all(query)

Repos

To support deletion in repos, just add use Ecto.SoftDelete.Repo to your repo. After that, the functions soft_delete!/1, soft_delete/1 and soft_delete_all/1 will be available for you.

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

# posts.ex
Repo.soft_delete_all(Post)
from(p in Post, where: p.id < 10) |> Repo.soft_delete_all()

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

post = Repo.get!(Post, 42)
struct = Repo.soft_delete!(post)

Installation

Add to mix.exs:

defp deps do
  [{:ecto_soft_delete, "~> 1.0"}]
end

and do

mix deps.get

Configuration

There are currently no configuration options.

Usage

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/revelrylabs/ecto_soft_delete. Check out CONTRIBUTING.md for more info.

Everyone is welcome to participate in the project. We expect contributors to adhere the Contributor Covenant Code of Conduct (see CODE_OF_CONDUCT.md).

ecto_soft_delete's People

Contributors

ayan-haider avatar brianberlin avatar bryanjos avatar dacello avatar dependabot-preview[bot] avatar dependabot-support avatar dependabot[bot] avatar jwietelmann avatar lukeledet avatar mauricionr avatar prehnra avatar quinnmccourt avatar radditude avatar samkeer1 avatar thiamsantos avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ecto_soft_delete's Issues

Support soft delete when join.

When you join right now, you still need to add not is_nil(deleted_at) during join. It is really easy to forget to add as normally you don't need to think about it as it is automatically handled by the library.
Tried to modify the Repo, but not sure if there is any way to do the dynamic binding in the query. (as you can use query.joins to figure out how many join it is, theoretically if there is any way to loop build the query, it might work(
Happy to discuss or implement if anyone have any ideas.

version 0.2.0 not published to hex

while the readme instructs to add [{:ecto_soft_delete, "0.2.0"}] to mix file.
it seems like version 0.2.0 not published to hex yet https://hex.pm/packages/ecto_soft_delete.
a try to mix deps.get will throw the following error
** (Mix) No matching version for ecto_soft_delete 0.2.0 (from: mix.exs) in registry The latest version is: 0.1.0

Deleted records are not being fetched by default

Hi ๐Ÿ‘‹

If I understood correctly, deleted records should be fetched by default e.g.

Record |> Repo.all()

However, I see a deleted_at IS NULL clause being applied. Is this correct or is it a known issue?

Your documentation suggests this would only happen if I use the with_undeleted query.

I need to do something like this to fetch all records

Record
|> where([record], not is_nil(record.deleted_at))
|> or_where([record], is_nil(record.deleted_at))
|> Repo.all()

Thank you!

The deactivated_at clause inclusion check

The deleted_at clause here works perfectly fine when we apply each filter in separate where expressions.
However, if we do something like this:

from(c in City, where: c.country == "Sweden" and is_nil(c.deleted_at))

The code does not recognize it and adds another check that is exactly the same.

Support configuration such as changing column name

Everything seems to fit very well except that I would like to be able to change the column name use to track soft deletes from :deleted_at to :retired_at. It would be great if that was a configuration option that could be passed. If this is something you wouldn't mind supporting I would be happy to contribute

Housekeeping Checklist

There's still a few things left to do in order to make this repo an awesome place to contribute. Checklist as follows:

`soft_delete_columns()` potentially misleading api for users

If the function only adds a single column to a table then might we want to change the name to
soft_delete_column?

I didn't want to open up multiple PRs at a time but if I have the go ahead I have a branch ready.

I think that a change like this actually has some dangers.
Since the only files that this function will be called in are migration files and it is generally frowned upon to edit migration files after checking them into source control.

If the api changes it should be backwards compatible and only the documentation should change with it.

I would consider deprecation warnings for the plural version but I wonder if that would be detrimental in terms of encouraging developers to edit migration files.

Feedback appreciated but not expected after all it is just a naming issue! ๐Ÿ˜„

Add functionality to `Ecto.SoftDelete.Repo` to exclude soft deleted records from Repo queries by default

Soft deleted records are included by default in all ecto queries. This is less than ideal, particularly when preloading associations. Would be really nice if we could just assume that any time we do a Repo.all/get/one as well as an ecto preload, soft deleted records were excluded by default. We could potentially bake this functionality into Ecto.SoftDelete.Repo so that it overrides the standard Ecto.Repo functionality for these functions.

Must construct additional test coverage

I made sure we had test coverage measurement here. It looks like we're possibly missing some spots. Please make sure we've got enough coverage on the keys bits.

Add Repo.soft_delete

Would be if this library provided an way to add soft_delete functions to Repo as well.

Maybe something like this:

defmodule MyApp.Repo do
  use Ecto.Repo,
    otp_app: :my_app,
    adapter: Ecto.Adapters.Postgres
  use Ecto.SoftDelete
end
post = MyApp.Repo.get!(Post, 42)
MyApp.Repo.soft_delete(post)
post = MyApp.Repo.get!(Post, 42)
MyApp.Repo.soft_delete!(post)
from(p in Post, where: p.id < 10) |> MyApp.Repo.soft_delete_all()

What do you think?

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.