Skip to content

Commit 9c9eb0d

Browse files
Add option not fetch entries
1 parent 22f6bc1 commit 9c9eb0d

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

lib/quarto.ex

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ defmodule Quarto do
3131
* `:cursor` - Module to use for encoding/decoding the cursor
3232
* `:cursor_fields` - Module to use for building the cursor from a record
3333
* `:include_total_count` - Set this to true to return the total number of records matching the query. Note that this number will be capped by :total_count_limit. Defaults to false.
34+
* `:include_entries` - Set this to false to skip fetching the records. Useful if you only need the total counts. Defaults to true.
3435
* `:total_count_primary_key_field` - Running count queries on specified column of the table
3536
* `:limit` - Limits the number of records returned per page. Note that this number will be capped by :maximum_limit. Defaults to `50`.
3637
* `:maximum_limit` - Sets a maximum cap for :limit. This option can be useful when :limit is set dynamically (e.g from a URL param set by a user) but you still want to enfore a maximum. Defaults to 500.
@@ -201,6 +202,10 @@ defmodule Quarto do
201202
Enum.count(sorted_entries) <= limit
202203
end
203204

205+
defp entries(_, %{include_entries: false}, _, _) do
206+
[]
207+
end
208+
204209
defp entries(queryable, config, repo, repo_opts) do
205210
queryable
206211
|> Quarto.Ecto.Query.paginate(config)

lib/quarto/config.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ defmodule Quarto.Config do
1313
:custom_options,
1414
:transform,
1515
:include_total_count,
16+
:include_entries,
1617
:limit,
1718
:maximum_limit,
1819
:queryable,
@@ -41,6 +42,7 @@ defmodule Quarto.Config do
4142
coalesce: opts[:coalesce] || fn _field, _position_, _value -> nil end,
4243
cursor_builder: {Quarto.CursorValue, :build, []},
4344
cursor_fields: cursor_fields.build(queryable, opts),
45+
include_entries: Keyword.get(opts, :include_entries, true),
4446
include_total_count: opts[:include_total_count] || false,
4547
total_count_primary_key_field:
4648
opts[:total_count_primary_key_field] || @default_total_count_primary_key_field,

test/quarto_test.exs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,28 @@ defmodule QuartoTest do
482482
end)
483483
end
484484

485+
describe "with include_entries" do
486+
test "when set to false" do
487+
%Page{metadata: metadata, entries: entries} =
488+
posts_by_published_at(:desc)
489+
|> paginate(
490+
limit: 5,
491+
total_count_limit: :infinity,
492+
include_total_count: true,
493+
include_entries: false
494+
)
495+
496+
assert entries == []
497+
498+
assert metadata == %Metadata{
499+
before: nil,
500+
limit: 5,
501+
total_count: 12,
502+
total_count_cap_exceeded: false
503+
}
504+
end
505+
end
506+
485507
describe "with include_total_count" do
486508
test "when set to :infinity", %{
487509
posts: {_p1, _p2, _p3, _p4, p5, _p6, _p7, _p8, _p9, _p10, _p11, _p12}
@@ -649,8 +671,10 @@ defmodule QuartoTest do
649671

650672
assert_receive({:query, queryable})
651673

652-
assert "#Ecto.Query<from p0 in Quarto.Post, where: coalesce(p0.position, ^100) == ^3 and (coalesce(p0.id, ^10) > ^10 and ^true) or coalesce(p0.position, ^100) < ^3 and ^true and ^true, order_by: [desc: coalesce(p0.position, ^100), asc: coalesce(p0.id, ^10)], limit: ^6>" ==
653-
inspect(queryable)
674+
assert """
675+
#Ecto.Query<from p0 in Quarto.Post, where: (coalesce(p0.position, ^100) == ^3 and (coalesce(p0.id, ^10) > ^10 and ^true)) or
676+
(coalesce(p0.position, ^100) < ^3 and ^true and ^true), order_by: [desc: coalesce(p0.position, ^100), asc: coalesce(p0.id, ^10)], limit: ^6>
677+
""" == inspect(queryable) <> "\n"
654678
end
655679
end
656680

0 commit comments

Comments
 (0)