diff --git a/lib/dataloader/ecto.ex b/lib/dataloader/ecto.ex index 16cff07..a630464 100644 --- a/lib/dataloader/ecto.ex +++ b/lib/dataloader/ecto.ex @@ -335,6 +335,10 @@ if Code.ensure_loaded?(Ecto) do end end + defp load_rows(nil, _inputs, _queryable, query, repo, repo_opts) do + repo.all(query, repo_opts) + end + defp load_rows(col, inputs, queryable, query, repo, repo_opts) do pk = queryable.__schema__(:primary_key) @@ -572,6 +576,10 @@ if Code.ensure_loaded?(Ecto) do """ end + defp normalize_value(_queryable, []) do + {:not_primary, nil, nil} + end + defp normalize_value(queryable, [{col, value}]) do case queryable.__schema__(:primary_key) do [^col] -> diff --git a/test/dataloader/ecto/limit_query_test.exs b/test/dataloader/ecto/limit_query_test.exs index f0d598d..b6f2f43 100644 --- a/test/dataloader/ecto/limit_query_test.exs +++ b/test/dataloader/ecto/limit_query_test.exs @@ -78,6 +78,35 @@ defmodule Dataloader.LimitQueryTest do assert [post3] == Dataloader.get(loader, Test, args, user_id: user2.id) end + test "Query limit without filters", %{loader: loader} do + user1 = %User{username: "Ben Wilson"} |> Repo.insert!() + user2 = %User{username: "Bruce Williams"} |> Repo.insert!() + + [post1, post2, _post3, _post4] = + [ + %Post{user_id: user1.id, title: "foo"}, + %Post{user_id: user1.id, title: "baz"}, + %Post{user_id: user2.id, title: "bar"}, + %Post{user_id: user2.id, title: "qux"} + ] + |> Enum.map(&Repo.insert!/1) + + args0 = {{:one, Post}, %{limit: 1, order_by: [asc: :id]}} + args1 = {{:many, Post}, %{limit: 1, order_by: [asc: :id]}} + args2 = {{:many, Post}, %{limit: 2, order_by: [asc: :id]}} + + loader = + loader + |> Dataloader.load(Test, args0, []) + |> Dataloader.load(Test, args1, []) + |> Dataloader.load(Test, args2, []) + |> Dataloader.run() + + assert post1 == Dataloader.get(loader, Test, args0, []) + assert [post1] == Dataloader.get(loader, Test, args1, []) + assert [post1, post2] == Dataloader.get(loader, Test, args2, []) + end + test "Load has-many association with limit", %{loader: loader} do user1 = %User{username: "Ben Wilson"} |> Repo.insert!() user2 = %User{username: "Bruce Williams"} |> Repo.insert!() diff --git a/test/dataloader/ecto_test.exs b/test/dataloader/ecto_test.exs index 4625970..b6cb1d1 100644 --- a/test/dataloader/ecto_test.exs +++ b/test/dataloader/ecto_test.exs @@ -308,6 +308,26 @@ defmodule Dataloader.EctoTest do assert message =~ "Cardinality" end + test "basic loading of all things", %{loader: loader} do + user1 = %User{username: "Ben Wilson"} |> Repo.insert!() + user2 = %User{username: "Bruce Williams"} |> Repo.insert!() + + [post1, post2, post3] = + [ + %Post{user_id: user1.id, title: "foo"}, + %Post{user_id: user1.id, title: "baz"}, + %Post{user_id: user2.id, title: "bar"} + ] + |> Enum.map(&Repo.insert!/1) + + loader = + loader + |> Dataloader.load(Test, {:many, Post}, []) + |> Dataloader.run() + + assert [post1, post2, post3] == Dataloader.get(loader, Test, {:many, Post}, []) + end + describe "many_to_many" do test "basic loading works", %{loader: loader} do user1 = %User{username: "Ben Wilson"} |> Repo.insert!()