ash_archival/documentation/topics/unarchiving.md

43 lines
1.2 KiB
Markdown
Raw Normal View History

2024-01-13 09:02:02 +13:00
# Un-archiving
2023-01-18 11:16:52 +13:00
2024-03-30 03:19:24 +13:00
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):
2023-01-18 11:16:52 +13:00
```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`
2024-03-30 03:19:24 +13:00
Ash.Changeset.after_action(changeset, fn changeset, nil ->
2023-01-18 11:16:52 +13:00
id = Ash.Changeset.get_argument(changeset, :id)
ResourceWithoutArchival
|> Ash.Query.filter(id == ^id)
2024-03-30 03:19:24 +13:00
|> Ash.read_one()
2023-01-18 11:16:52 +13:00
|> case do
{:ok, nil} ->
# not found error
{:ok, found} ->
# unarchive
found
|> Ash.Changeset.for_update(:update, %{archived_at: nil})
2024-03-30 03:19:24 +13:00
|> Ash.update!()
2023-01-18 11:16:52 +13:00
2024-03-30 03:19:24 +13:00
{:ok, Ash.get!(changeset.resource, id)}
2023-01-18 11:16:52 +13:00
end
end)
end
2024-03-30 03:19:24 +13:00
```