Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/danilamihailov/differ_ecto

Ecto.Type for Differ (and some helpers)
https://github.com/danilamihailov/differ_ecto

diff differ elixir elixir-library

Last synced: about 2 months ago
JSON representation

Ecto.Type for Differ (and some helpers)

Awesome Lists containing this project

README

        

# DifferEcto [![Hex Version](https://img.shields.io/hexpm/v/differ_ecto.svg)](https://hex.pm/packages/differ_ecto) [![docs](https://img.shields.io/badge/docs-hexpm-blue.svg)](https://hexdocs.pm/differ_ecto/) [![Coverage Status](https://coveralls.io/repos/github/DanilaMihailov/differ_ecto/badge.svg?branch=master)](https://coveralls.io/github/DanilaMihailov/differ_ecto?branch=master)

`Ecto.Type` for `Differ` (and some helpers)

## Installation

The package can be installed
by adding `differ_ecto` to your list of dependencies in `mix.exs`:

```elixir
def deps do
[
{:differ_ecto, "~> 0.1-pre"}
]
end
```

## Documentation

Documentation can be found at [https://hexdocs.pm/differ_ecto](https://hexdocs.pm/differ_ecto).

## Usage

You can use json array to store diffs like this

```elixir
defmodule Example do
use Ecto.Schema
# gives us Diffable, Patchable and Diff
use DifferEcto

# we have to skip :diffs field
# skip timestamps as well
@derive [{Diffable, skip: [:updated_at, :diffs]}, Patchable]
schema "example" do
field :body, :string
field :title, :string
# we want to store list of diffs in document
field :diffs, {:array, Diff}, default: []
timestamps()
end
end

```

then when you do updates, you need to calculate diffs

```elixir
def update(%Example{} = ex, attrs) do
import Ecto.Changeset
ex
|> Example.changeset(attrs)
|> Repo.update()
|> case do
# after successful update run diff
{:ok, new_ex} ->
diff = DifferEcto.diff(ex, new_ex)
new_diffs = [diff | List.wrap(ex.diffs)]

# save diff to list of diffs
new_ex
|> cast(%{diffs: new_diffs}, [:diffs])
|> Repo.update()
val ->
val
end
```