Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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)
- Host: GitHub
- URL: https://github.com/danilamihailov/differ_ecto
- Owner: DanilaMihailov
- License: apache-2.0
- Created: 2020-02-01T19:35:02.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2020-02-14T19:24:25.000Z (almost 5 years ago)
- Last Synced: 2024-11-16T10:18:47.134Z (about 2 months ago)
- Topics: diff, differ, elixir, elixir-library
- Language: Elixir
- Homepage:
- Size: 30.3 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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
```