Skip to content

Commit 0c9a5df

Browse files
committed
Allow queries without batch keys
1 parent f1db9ae commit 0c9a5df

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

lib/dataloader/ecto.ex

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,10 @@ if Code.ensure_loaded?(Ecto) do
335335
end
336336
end
337337

338+
defp load_rows(nil, _inputs, _queryable, query, repo, repo_opts) do
339+
repo.all(query, repo_opts)
340+
end
341+
338342
defp load_rows(col, inputs, queryable, query, repo, repo_opts) do
339343
pk = queryable.__schema__(:primary_key)
340344

@@ -564,6 +568,10 @@ if Code.ensure_loaded?(Ecto) do
564568
"""
565569
end
566570

571+
defp normalize_value(_queryable, []) do
572+
{:not_primary, nil, nil}
573+
end
574+
567575
defp normalize_value(queryable, [{col, value}]) do
568576
case queryable.__schema__(:primary_key) do
569577
[^col] ->

test/dataloader/ecto/limit_query_test.exs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,35 @@ defmodule Dataloader.LimitQueryTest do
7878
assert [post3] == Dataloader.get(loader, Test, args, user_id: user2.id)
7979
end
8080

81+
test "Query limit without filters", %{loader: loader} do
82+
user1 = %User{username: "Ben Wilson"} |> Repo.insert!()
83+
user2 = %User{username: "Bruce Williams"} |> Repo.insert!()
84+
85+
[post1, post2, _post3, _post4] =
86+
[
87+
%Post{user_id: user1.id, title: "foo"},
88+
%Post{user_id: user1.id, title: "baz"},
89+
%Post{user_id: user2.id, title: "bar"},
90+
%Post{user_id: user2.id, title: "qux"}
91+
]
92+
|> Enum.map(&Repo.insert!/1)
93+
94+
args0 = {{:one, Post}, %{limit: 1, order_by: [asc: :id]}}
95+
args1 = {{:many, Post}, %{limit: 1, order_by: [asc: :id]}}
96+
args2 = {{:many, Post}, %{limit: 2, order_by: [asc: :id]}}
97+
98+
loader =
99+
loader
100+
|> Dataloader.load(Test, args0, [])
101+
|> Dataloader.load(Test, args1, [])
102+
|> Dataloader.load(Test, args2, [])
103+
|> Dataloader.run()
104+
105+
assert post1 == Dataloader.get(loader, Test, args0, [])
106+
assert [post1] == Dataloader.get(loader, Test, args1, [])
107+
assert [post1, post2] == Dataloader.get(loader, Test, args2, [])
108+
end
109+
81110
test "Load has-many association with limit", %{loader: loader} do
82111
user1 = %User{username: "Ben Wilson"} |> Repo.insert!()
83112
user2 = %User{username: "Bruce Williams"} |> Repo.insert!()

test/dataloader/ecto_test.exs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,26 @@ defmodule Dataloader.EctoTest do
308308
assert message =~ "Cardinality"
309309
end
310310

311+
test "basic loading of all things", %{loader: loader} do
312+
user1 = %User{username: "Ben Wilson"} |> Repo.insert!()
313+
user2 = %User{username: "Bruce Williams"} |> Repo.insert!()
314+
315+
[post1, post2, post3] =
316+
[
317+
%Post{user_id: user1.id, title: "foo"},
318+
%Post{user_id: user1.id, title: "baz"},
319+
%Post{user_id: user2.id, title: "bar"}
320+
]
321+
|> Enum.map(&Repo.insert!/1)
322+
323+
loader =
324+
loader
325+
|> Dataloader.load(Test, {:many, Post}, [])
326+
|> Dataloader.run()
327+
328+
assert [post1, post2, post3] == Dataloader.get(loader, Test, {:many, Post}, [])
329+
end
330+
311331
describe "has_many through:" do
312332
test "basic loading works", %{loader: loader} do
313333
user1 = %User{username: "Ben Wilson"} |> Repo.insert!()

0 commit comments

Comments
 (0)