mirror of
https://github.com/ash-project/ash_archival.git
synced 2024-09-21 14:02:55 +12:00
42 lines
No EOL
1.2 KiB
Markdown
42 lines
No EOL
1.2 KiB
Markdown
# Un-archiving
|
|
|
|
At the moment, there is no way to unarchive an entry with a simple action on that resource. However, if you define a simple resource that uses the same storage under the hood (e.g same database table), but does *not* use the archival extension. You could then fabricate unarchival with something like this (this is not vetted, it is a pseudo-code example):
|
|
|
|
```elixir
|
|
# on the archived resource
|
|
# we model it as a create because there is no input record
|
|
create :unarchive do
|
|
manual? true
|
|
argument :id, :uuid do
|
|
allow_nil? false
|
|
end
|
|
|
|
change Unarchive
|
|
end
|
|
```
|
|
|
|
with an `Unarchive` change like this
|
|
|
|
```elixir
|
|
def change(changeset, _, _) do
|
|
# no data yet, so match on result being `nil`
|
|
Ash.Changeset.after_action(changeset, fn changeset, nil ->
|
|
id = Ash.Changeset.get_argument(changeset, :id)
|
|
|
|
ResourceWithoutArchival
|
|
|> Ash.Query.filter(id == ^id)
|
|
|> Api.read_one()
|
|
|> case do
|
|
{:ok, nil} ->
|
|
# not found error
|
|
{:ok, found} ->
|
|
# unarchive
|
|
found
|
|
|> Ash.Changeset.for_update(:update, %{archived_at: nil})
|
|
|> Api.update!()
|
|
|
|
{:ok, Api.get!(changeset.resource, id)}
|
|
end
|
|
end)
|
|
end
|
|
``` |