Skip to content

Commit

Permalink
Allow queries without batch keys
Browse files Browse the repository at this point in the history
  • Loading branch information
arnodirlam committed Oct 4, 2024
1 parent dff6fd3 commit 2dfe3c5
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/dataloader/ecto.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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] ->
Expand Down
29 changes: 29 additions & 0 deletions test/dataloader/ecto/limit_query_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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!()
Expand Down
20 changes: 20 additions & 0 deletions test/dataloader/ecto_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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!()
Expand Down

0 comments on commit 2dfe3c5

Please sign in to comment.