feat: Support destroying records.
This commit is contained in:
parent
5470268810
commit
2eab92a76a
|
@ -22,7 +22,7 @@ AshCubDb is still a work in progress. Feel free to give it a go.
|
||||||
| Read (calculations) | ❌ |
|
| Read (calculations) | ❌ |
|
||||||
| Read (aggregates) | ❌ |
|
| Read (aggregates) | ❌ |
|
||||||
| Update | ✅ |
|
| Update | ✅ |
|
||||||
| Destroy | ❌ |
|
| Destroy | ✅ |
|
||||||
|
|
||||||
## Github Mirror
|
## Github Mirror
|
||||||
|
|
||||||
|
|
|
@ -53,6 +53,7 @@ defmodule AshCubDB.DataLayer do
|
||||||
def can?(resource, :create), do: Dir.writable?(resource)
|
def can?(resource, :create), do: Dir.writable?(resource)
|
||||||
def can?(resource, :update), do: Dir.writable?(resource)
|
def can?(resource, :update), do: Dir.writable?(resource)
|
||||||
def can?(resource, :upsert), do: Dir.writable?(resource)
|
def can?(resource, :upsert), do: Dir.writable?(resource)
|
||||||
|
def can?(resource, :destroy), do: Dir.writable?(resource)
|
||||||
def can?(resource, :read), do: Dir.readable?(resource)
|
def can?(resource, :read), do: Dir.readable?(resource)
|
||||||
def can?(_, :multitenancy), do: true
|
def can?(_, :multitenancy), do: true
|
||||||
def can?(_, :filter), do: true
|
def can?(_, :filter), do: true
|
||||||
|
@ -136,6 +137,21 @@ defmodule AshCubDB.DataLayer do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@doc false
|
||||||
|
@impl true
|
||||||
|
def destroy(resource, changeset) do
|
||||||
|
with :ok <- validate_tenant_configuration(resource, changeset.tenant),
|
||||||
|
{:ok, db} <- start(resource),
|
||||||
|
{:ok, key, _data} <- Serde.serialise(changeset.data),
|
||||||
|
{:ok, key} <- maybe_wrap_in_tenant(key, changeset),
|
||||||
|
true <- CubDB.has_key?(db, key) do
|
||||||
|
CubDB.delete(db, key)
|
||||||
|
else
|
||||||
|
false -> {:error, StaleRecord.exception(resource: resource)}
|
||||||
|
{:error, reason} -> {:error, reason}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
@doc false
|
@doc false
|
||||||
@impl true
|
@impl true
|
||||||
def run_query(query, resource, parent \\ nil) do
|
def run_query(query, resource, parent \\ nil) do
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
defmodule AshCubDB.DataLayerTest do
|
defmodule AshCubDB.DataLayerTest do
|
||||||
use ExUnit.Case, async: true
|
use ExUnit.Case, async: true
|
||||||
alias Ash.Query
|
alias Ash.{Error.Query.NotFound, Query}
|
||||||
alias AshCubDB.Info
|
alias AshCubDB.Info
|
||||||
alias Support.{Api, Author, Post}
|
alias Support.{Api, Author, Post}
|
||||||
import Support.Factory
|
import Support.Factory
|
||||||
|
@ -157,6 +157,15 @@ defmodule AshCubDB.DataLayerTest do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "destroy" do
|
||||||
|
test "records can be destroyed" do
|
||||||
|
post = insert!(Post)
|
||||||
|
|
||||||
|
assert :ok = Post.destroy(post)
|
||||||
|
assert {:error, %NotFound{}} = Post.get(post.id)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
defp dump(resource) do
|
defp dump(resource) do
|
||||||
resource
|
resource
|
||||||
|> via()
|
|> via()
|
||||||
|
|
|
@ -16,8 +16,7 @@ defmodule Support.Post do
|
||||||
end
|
end
|
||||||
|
|
||||||
actions do
|
actions do
|
||||||
# defaults ~w[create read update destroy]a
|
defaults ~w[create read update destroy]a
|
||||||
defaults ~w[create read update]a
|
|
||||||
end
|
end
|
||||||
|
|
||||||
relationships do
|
relationships do
|
||||||
|
@ -31,5 +30,6 @@ defmodule Support.Post do
|
||||||
define :read
|
define :read
|
||||||
define :update
|
define :update
|
||||||
define :get, action: :read, get_by: [:id]
|
define :get, action: :read, get_by: [:id]
|
||||||
|
define :destroy
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue