Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow queries without batch keys #170

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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