Skip to content

Commit 48de293

Browse files
authored
improvement: add paginate_by_default? option to read actions (#2648)
1 parent 6351c1c commit 48de293

File tree

5 files changed

+28
-1
lines changed

5 files changed

+28
-1
lines changed

.formatter.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ spark_locals_without_parens = [
223223
on: 1,
224224
only_when_valid?: 1,
225225
page: 1,
226+
paginate_by_default?: 1,
226227
pagination: 0,
227228
pagination: 1,
228229
parse_attribute: 1,

documentation/dsls/DSL-Ash.Resource.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1554,6 +1554,7 @@ Adds pagination options to a resource
15541554
| [`max_page_size`](#actions-read-pagination-max_page_size){: #actions-read-pagination-max_page_size } | `pos_integer` | `250` | The maximum amount of records that can be requested in a single page |
15551555
| [`stable_sort`](#actions-read-pagination-stable_sort){: #actions-read-pagination-stable_sort } | `any` | | A stable sort statement to add to a query (after any existing sorts). Only added if the sort does not already contain a stable sort (sorting on fields that uniquely identify a record). Defaults to the primary key. |
15561556
| [`required?`](#actions-read-pagination-required?){: #actions-read-pagination-required? } | `boolean` | `true` | Whether or not pagination can be disabled (by passing `page: false` to `Ash.Api.read!/2`, or by having `required?: false, default_limit: nil` set). Only relevant if some pagination configuration is supplied. |
1557+
| [`paginate_by_default?`](#actions-read-pagination-paginate_by_default?){: #actions-read-pagination-paginate_by_default? } | `boolean` | `false` | Whether or not to paginate by default when pagination is not required and no page parameters are provided. |
15571558

15581559

15591560

lib/ash/actions/read/read.ex

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3832,7 +3832,9 @@ defmodule Ash.Actions.Read do
38323832
action.pagination.default_limit ->
38333833
Keyword.put(page_opts, :limit, action.pagination.default_limit)
38343834

3835-
is_nil(page_opts) and action.pagination.required? and not relationship? ->
3835+
is_nil(page_opts) and
3836+
(action.pagination.required? or action.pagination.paginate_by_default?) and
3837+
not relationship? ->
38363838
if action.pagination.default_limit do
38373839
[limit: action.pagination.default_limit]
38383840
else

lib/ash/resource/actions/read.ex

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,12 @@ defmodule Ash.Resource.Actions.Read do
149149
doc:
150150
"Whether or not pagination can be disabled (by passing `page: false` to `Ash.Api.read!/2`, or by having `required?: false, default_limit: nil` set). Only relevant if some pagination configuration is supplied.",
151151
default: true
152+
],
153+
paginate_by_default?: [
154+
type: :boolean,
155+
doc:
156+
"Whether or not to paginate by default when pagination is not required and no page parameters are provided.",
157+
default: false
152158
]
153159
]
154160

@@ -160,6 +166,7 @@ defmodule Ash.Resource.Actions.Read do
160166
countable: false,
161167
stable_sort: nil,
162168
required?: false,
169+
paginate_by_default?: false,
163170
keyset?: false,
164171
offset?: false,
165172
__spark_metadata__: nil

test/actions/pagination_test.exs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ defmodule Ash.Actions.PaginationTest do
6464
pagination offset?: true, countable: true, required?: false
6565
end
6666

67+
read :paginate_by_default do
68+
pagination offset?: true,
69+
countable: true,
70+
required?: false,
71+
paginate_by_default?: true,
72+
default_limit: 3
73+
end
74+
6775
read :offset_countable_by_default do
6876
pagination offset?: true, countable: :by_default, required?: false
6977
end
@@ -308,6 +316,14 @@ defmodule Ash.Actions.PaginationTest do
308316

309317
assert %{results: [%{name: "3"}]} = Ash.page!(page, :self)
310318
end
319+
320+
test "paginate_by_default? applies default limit when no page opts" do
321+
assert %Ash.Page.Offset{results: results} =
322+
User
323+
|> Ash.read!(action: :paginate_by_default)
324+
325+
assert Enum.count(results) == 3
326+
end
311327
end
312328

313329
describe "keyset pagination with nil fields" do

0 commit comments

Comments
 (0)