Giter Club home page Giter Club logo

phoenix-soft-delete-helper-module's Introduction

Soft Delete Module for Phoenix/Elixir Applications

What?

  • Adds is_deleted (boolean), and deleted_at (timestamp) functioanlity in ecto (phoenix) tables/schemas.
  • Provides functionality to get only non soft deleted entries from DB table.
  • Provides functionality to (soft) delete a row in table.

How to install the dependency?

  • (Pre-requisite) You should be using elixir and phoenix to make use of this module.
  • Add the dependency in your application by adding the below line in deps method of your mix.exs file

{:soft_delete_helper_module, "~> 0.0.1"}

This will install the dependency in your project.

How to use this module?

  • In Schema, modify the code accordingly
      defmodule User do
        use Ecto.Schema

        import SoftDeleteHelperModule.Schema

        schema "users" do
          field :first_name, :string

          soft_delete_schema()
        end
      end
  • In the corresponding migration of the user schema, modify the code accordingly
      defmodule MyApp.Repo.Migrations.CreateUser do
        use Ecto.Migration

        import SoftDeleteHelperModule.Migration

        def change do
          create table(:users) do
            add :firt_name, :string

            soft_delete_columns()
          end
        end
      end
  • After these changes, run the migration using mix ecto.migrate, this will create users table in your DB with columns is_deleted and deleted_at.

Additional Functions and how to use them -

  • with_non_soft_delete(query)

This function returns an ecto query with a where clause of all non deleted entries in your table.

Pass an ecto query as parameter

Example -

query = from(u in "users", select: u.id)
This returns #Ecto.Query<from u in "users", select: u.id>

Now pass the above query to the function as follows -
soft_deleted_query = SoftDeleteHelperModule.Schema.with_non_soft_delete(query)
This returns #Ecto.Query<from u in "users", where: u.is_deleted == false, select: u.id>

When you try to get the rows using MyApp.Repo.all(soft_deleted_query), this will return
all the non deleted entries
  • delete_entity(struct)

This function returns a changeset with the entity deleted.

Example -

Get the struct you want to delete as follows -
entity = MyRepo.Repo.get(MyRepo.User, 1)

pass this entity to delete_entity function as follows -
deleted_entity = SoftDeleteHelperModule.Schema.delete_entity(entity)
This will return a changeset with is_deleted set to true and deleted_at set to now time

Now update the deleted_entity using -
MyRepo.Repo.update(deleted_entity)

This will update the DB table with is_deleted set to true and deleted_at set to now time.
  • Index on is_deleted column using create_index_on_soft_delete This will create an index on is_deleted column of the table

Examples -

Your migration file should look like this -

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

        import SoftDeleteHelperModule.Migration

        def change do
          create table(:users) do
            add :firt_name, :string

            soft_delete_columns()
          end
          
          create_index_on_soft_delete("users")
        end
      end

Hex package -

https://hex.pm/packages/soft_delete_helper_module

phoenix-soft-delete-helper-module's People

Contributors

cosmos-sajal avatar

Stargazers

 avatar  avatar  avatar

Watchers

 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.