-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Load rows for limit/offset queries with lateral join
Queries that do not rely on limit/offset continue to use the existing approach, where a simple where clause is added to the query. Complex queries are converted into a lateral subquery. The outer query contains the inputs. Ideally the query would use `unnest` or a similar function to efficiently load an array of values into a query, but Ecto doesn't offer such functionality in a portable way. The workaround here is to run a distinct query that selects the column from the queryable where the value is any of the inputs.
- Loading branch information
Showing
3 changed files
with
96 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
defmodule Dataloader.LimitQueryTest do | ||
use ExUnit.Case, async: true | ||
|
||
alias Dataloader.{User, Post} | ||
import Ecto.Query | ||
alias Dataloader.TestRepo, as: Repo | ||
|
||
setup do | ||
:ok = Ecto.Adapters.SQL.Sandbox.checkout(Repo) | ||
|
||
test_pid = self() | ||
|
||
source = | ||
Dataloader.Ecto.new( | ||
Repo, | ||
query: &query(&1, &2, test_pid) | ||
) | ||
|
||
loader = | ||
Dataloader.new() | ||
|> Dataloader.add_source(Test, source) | ||
|
||
{:ok, loader: loader} | ||
end | ||
|
||
defp query(Post, %{limit: limit}, test_pid) do | ||
send(test_pid, :querying) | ||
|
||
Post | ||
|> where([p], is_nil(p.deleted_at)) | ||
|> order_by(asc: :id) | ||
|> limit(^limit) | ||
end | ||
|
||
defp query(queryable, _args, test_pid) do | ||
send(test_pid, :querying) | ||
queryable | ||
end | ||
|
||
test "Query limit does not apply globally", %{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) | ||
|
||
args = {{:many, Post}, %{limit: 1}} | ||
|
||
loader = | ||
loader | ||
|> Dataloader.load(Test, args, user_id: user1.id) | ||
|> Dataloader.load(Test, args, user_id: user2.id) | ||
|> Dataloader.run() | ||
|
||
assert [post1] == Dataloader.get(loader, Test, args, user_id: user1.id) | ||
assert [post3] == Dataloader.get(loader, Test, args, user_id: user2.id) | ||
end | ||
end |