From 737a6a5442811cdc3500725a5cda9694f42eef51 Mon Sep 17 00:00:00 2001 From: Zach Daniel Date: Fri, 9 Apr 2021 00:53:50 -0400 Subject: [PATCH] improvement: support new ash select feature --- .github/workflows/elixir.yml | 2 +- lib/data_layer.ex | 11 +++++++++++ mix.exs | 2 +- mix.lock | 2 +- test/select_test.exs | 15 +++++++++++++++ 5 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 test/select_test.exs diff --git a/.github/workflows/elixir.yml b/.github/workflows/elixir.yml index 73f2260..61d88af 100644 --- a/.github/workflows/elixir.yml +++ b/.github/workflows/elixir.yml @@ -18,7 +18,7 @@ jobs: matrix: otp: ["23"] elixir: ["1.11.0"] - ash: ["master", "1.39.3"] + ash: ["master", "1.39.5"] pg_version: ["9.5", "9.6", "11"] env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/lib/data_layer.ex b/lib/data_layer.ex index 790094f..04c2441 100644 --- a/lib/data_layer.ex +++ b/lib/data_layer.ex @@ -262,6 +262,7 @@ defmodule AshPostgres.DataLayer do def can?(_, :aggregate_filter), do: true def can?(_, :aggregate_sort), do: true def can?(_, :create), do: true + def can?(_, :select), do: true def can?(_, :read), do: true def can?(_, :update), do: true def can?(_, :destroy), do: true @@ -760,6 +761,16 @@ defmodule AshPostgres.DataLayer do end) end + @impl true + def select(query, select, resource) do + query = default_bindings(query, resource) + + {:ok, + from(row in query, + select: struct(row, ^select) + )} + end + @impl true def distinct(query, distinct_on, resource) do query = default_bindings(query, resource) diff --git a/mix.exs b/mix.exs index 59cd5f4..eb50479 100644 --- a/mix.exs +++ b/mix.exs @@ -95,7 +95,7 @@ defmodule AshPostgres.MixProject do {:ecto_sql, "~> 3.5"}, {:jason, "~> 1.0"}, {:postgrex, ">= 0.0.0"}, - {:ash, ash_version("~> 1.39 and >= 1.39.3")}, + {:ash, ash_version("~> 1.39 and >= 1.39.5")}, {:git_ops, "~> 2.0.1", only: :dev}, {:ex_doc, "~> 0.22", only: :dev, runtime: false}, {:ex_check, "~> 0.11.0", only: :dev}, diff --git a/mix.lock b/mix.lock index 0ff8202..0fbdb14 100644 --- a/mix.lock +++ b/mix.lock @@ -1,5 +1,5 @@ %{ - "ash": {:hex, :ash, "1.39.3", "249a1c7b7337b80f622433c594cee06c289f36a0dc924fe8ab7b8b3132efe093", [:mix], [{:comparable, "~> 1.0", [hex: :comparable, repo: "hexpm", optional: false]}, {:decimal, "~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:ecto, "~> 3.4", [hex: :ecto, repo: "hexpm", optional: false]}, {:ets, "~> 0.8.0", [hex: :ets, repo: "hexpm", optional: false]}, {:jason, ">= 1.0.0", [hex: :jason, repo: "hexpm", optional: false]}, {:nimble_options, "~> 0.3.5", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:picosat_elixir, "~> 0.1.5", [hex: :picosat_elixir, repo: "hexpm", optional: false]}, {:timex, ">= 3.0.0", [hex: :timex, repo: "hexpm", optional: false]}], "hexpm", "bd7ad8049ef0b39c868c62a6769e70ce10e5ee1771fe052a3b616b50835a7eb0"}, + "ash": {:hex, :ash, "1.39.5", "95fc7860f5b15904d64b5bfc18237bde41bb9da68584287155adee613a1065ad", [:mix], [{:comparable, "~> 1.0", [hex: :comparable, repo: "hexpm", optional: false]}, {:decimal, "~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:ecto, "~> 3.4", [hex: :ecto, repo: "hexpm", optional: false]}, {:ets, "~> 0.8.0", [hex: :ets, repo: "hexpm", optional: false]}, {:jason, ">= 1.0.0", [hex: :jason, repo: "hexpm", optional: false]}, {:nimble_options, "~> 0.3.5", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:picosat_elixir, "~> 0.1.5", [hex: :picosat_elixir, repo: "hexpm", optional: false]}, {:timex, ">= 3.0.0", [hex: :timex, repo: "hexpm", optional: false]}], "hexpm", "20db450e1bcd8020d1aeef1d4c120ad4d2e23e0f9a92a6b6db8916600bf19b4e"}, "bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], [], "hexpm", "7af5c7e09fe1d40f76c8e4f9dd2be7cebd83909f31fee7cd0e9eadc567da8353"}, "certifi": {:hex, :certifi, "2.6.1", "dbab8e5e155a0763eea978c913ca280a6b544bfa115633fa20249c3d396d9493", [:rebar3], [], "hexpm", "524c97b4991b3849dd5c17a631223896272c6b0af446778ba4675a1dff53bb7e"}, "combine": {:hex, :combine, "0.10.0", "eff8224eeb56498a2af13011d142c5e7997a80c8f5b97c499f84c841032e429f", [:mix], [], "hexpm", "1b1dbc1790073076580d0d1d64e42eae2366583e7aecd455d1215b0d16f2451b"}, diff --git a/test/select_test.exs b/test/select_test.exs new file mode 100644 index 0000000..5287b5f --- /dev/null +++ b/test/select_test.exs @@ -0,0 +1,15 @@ +defmodule AshPostgres.SelectTest do + @moduledoc false + use AshPostgres.RepoCase, async: false + alias AshPostgres.Test.{Api, Post} + + require Ash.Query + + test "values not selected in the query are not present in the response" do + Post + |> Ash.Changeset.new(%{title: "title"}) + |> Api.create!() + + assert [%{title: nil}] = Api.read!(Ash.Query.select(Post, :id)) + end +end