feat: Support updating existing records.
This commit is contained in:
parent
98d722f20d
commit
6b1954baf6
|
@ -21,7 +21,7 @@ AshCubDb is still a work in progress. Feel free to give it a go.
|
||||||
| Read (sort) | ✅ |
|
| Read (sort) | ✅ |
|
||||||
| Read (calculations) | ❌ |
|
| Read (calculations) | ❌ |
|
||||||
| Read (aggregates) | ❌ |
|
| Read (aggregates) | ❌ |
|
||||||
| Update | ❌ |
|
| Update | ✅ |
|
||||||
| Destroy | ❌ |
|
| Destroy | ❌ |
|
||||||
|
|
||||||
## Github Mirror
|
## Github Mirror
|
||||||
|
|
|
@ -10,4 +10,6 @@ config :git_ops,
|
||||||
|
|
||||||
if Mix.env() in ~w[dev test]a do
|
if Mix.env() in ~w[dev test]a do
|
||||||
config :ash_cubdb, ash_apis: [Support.Api]
|
config :ash_cubdb, ash_apis: [Support.Api]
|
||||||
|
|
||||||
|
config :spark, :formatter, remove_parens?: true
|
||||||
end
|
end
|
||||||
|
|
|
@ -14,6 +14,7 @@ defmodule AshCubDB.DataLayer do
|
||||||
Changeset,
|
Changeset,
|
||||||
Error,
|
Error,
|
||||||
Error.Changes.InvalidAttribute,
|
Error.Changes.InvalidAttribute,
|
||||||
|
Error.Changes.StaleRecord,
|
||||||
Error.Invalid.TenantRequired,
|
Error.Invalid.TenantRequired,
|
||||||
Filter.Runtime,
|
Filter.Runtime,
|
||||||
Resource
|
Resource
|
||||||
|
@ -50,6 +51,7 @@ defmodule AshCubDB.DataLayer do
|
||||||
@doc false
|
@doc false
|
||||||
@impl true
|
@impl true
|
||||||
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, :upsert), do: Dir.writable?(resource)
|
def can?(resource, :upsert), 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
|
||||||
|
@ -107,6 +109,22 @@ defmodule AshCubDB.DataLayer do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@doc false
|
||||||
|
@impl true
|
||||||
|
def update(resource, changeset) do
|
||||||
|
with :ok <- validate_tenant_configuration(resource, changeset.tenant),
|
||||||
|
{:ok, db} <- start(resource),
|
||||||
|
{:ok, record} <- Changeset.apply_attributes(changeset),
|
||||||
|
{:ok, key, data} <- Serde.serialise(record),
|
||||||
|
true <- CubDB.has_key?(db, key),
|
||||||
|
:ok <- CubDB.put(db, key, data) do
|
||||||
|
{:ok, set_loaded(record)}
|
||||||
|
else
|
||||||
|
false -> {:error, StaleRecord.exception(resource: resource)}
|
||||||
|
{:error, reason} -> {:error, Ash.Error.to_ash_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
|
||||||
|
|
|
@ -129,6 +129,21 @@ defmodule AshCubDB.DataLayerTest do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "update" do
|
||||||
|
test "records can be updated" do
|
||||||
|
post = insert!(Post)
|
||||||
|
params = Post |> params!() |> Map.take([:title])
|
||||||
|
|
||||||
|
assert {:ok, updated} = Post.update(post, params)
|
||||||
|
assert updated.id == post.id
|
||||||
|
assert updated.title == params.title
|
||||||
|
|
||||||
|
assert {:ok, updated} = Post.get(post.id)
|
||||||
|
assert updated.id == post.id
|
||||||
|
assert updated.title == params.title
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
defp dump(resource) do
|
defp dump(resource) do
|
||||||
resource
|
resource
|
||||||
|> via()
|
|> via()
|
||||||
|
|
|
@ -7,27 +7,29 @@ defmodule Support.Post do
|
||||||
end
|
end
|
||||||
|
|
||||||
attributes do
|
attributes do
|
||||||
uuid_primary_key(:id) do
|
uuid_primary_key :id do
|
||||||
writable?(true)
|
writable? true
|
||||||
end
|
end
|
||||||
|
|
||||||
attribute(:title, :string)
|
attribute :title, :string
|
||||||
attribute(:body, :string)
|
attribute :body, :string
|
||||||
end
|
end
|
||||||
|
|
||||||
actions do
|
actions do
|
||||||
# defaults ~w[create read update destroy]a
|
# defaults ~w[create read update destroy]a
|
||||||
defaults(~w[create read]a)
|
defaults ~w[create read update]a
|
||||||
end
|
end
|
||||||
|
|
||||||
relationships do
|
relationships do
|
||||||
belongs_to(:author, Support.Author)
|
belongs_to :author, Support.Author
|
||||||
end
|
end
|
||||||
|
|
||||||
code_interface do
|
code_interface do
|
||||||
define_for(Support.Api)
|
define_for Support.Api
|
||||||
|
|
||||||
define(:create)
|
define :create
|
||||||
define(:read)
|
define :read
|
||||||
|
define :update
|
||||||
|
define :get, action: :read, get_by: [:id]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue