Skip to content
Merged
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
1 change: 1 addition & 0 deletions .formatter.exs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ spark_locals_without_parens = [
on: 1,
only_when_valid?: 1,
page: 1,
paginate_by_default?: 1,
pagination: 0,
pagination: 1,
parse_attribute: 1,
Expand Down
7 changes: 4 additions & 3 deletions documentation/dsls/DSL-Ash.Resource.md
Original file line number Diff line number Diff line change
Expand Up @@ -1037,7 +1037,7 @@ prepare build(sort: [:foo, :bar])

| Name | Type | Default | Docs |
|------|------|---------|------|
| [`on`](#actions-action-prepare-on){: #actions-action-prepare-on } | `:read \| :action \| list(:read \| :action)` | `[:read]` | The action types the preparation should run on. By default, preparations only run on read actions. Use `:action` to run on generic actions. |
| [`on`](#actions-action-prepare-on){: #actions-action-prepare-on } | `:read \| :action \| :create \| :update \| :destroy \| list(:read \| :action \| :create \| :update \| :destroy)` | `[:read]` | The action types the preparation should run on. By default, preparations only run on read actions. Use `:action` to run on generic actions. |
| [`where`](#actions-action-prepare-where){: #actions-action-prepare-where } | `(any, any -> any) \| module \| list((any, any -> any) \| module)` | `[]` | Validations that should pass in order for this preparation to apply. Any of these validations failing will result in this preparation being ignored. |
| [`only_when_valid?`](#actions-action-prepare-only_when_valid?){: #actions-action-prepare-only_when_valid? } | `boolean` | `false` | If the preparation should only run on valid queries. |

Expand Down Expand Up @@ -1473,7 +1473,7 @@ prepare build(sort: [:foo, :bar])

| Name | Type | Default | Docs |
|------|------|---------|------|
| [`on`](#actions-read-prepare-on){: #actions-read-prepare-on } | `:read \| :action \| list(:read \| :action)` | `[:read]` | The action types the preparation should run on. By default, preparations only run on read actions. Use `:action` to run on generic actions. |
| [`on`](#actions-read-prepare-on){: #actions-read-prepare-on } | `:read \| :action \| :create \| :update \| :destroy \| list(:read \| :action \| :create \| :update \| :destroy)` | `[:read]` | The action types the preparation should run on. By default, preparations only run on read actions. Use `:action` to run on generic actions. |
| [`where`](#actions-read-prepare-where){: #actions-read-prepare-where } | `(any, any -> any) \| module \| list((any, any -> any) \| module)` | `[]` | Validations that should pass in order for this preparation to apply. Any of these validations failing will result in this preparation being ignored. |
| [`only_when_valid?`](#actions-read-prepare-only_when_valid?){: #actions-read-prepare-only_when_valid? } | `boolean` | `false` | If the preparation should only run on valid queries. |

Expand Down Expand Up @@ -1554,6 +1554,7 @@ Adds pagination options to a resource
| [`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 |
| [`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. |
| [`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. |
| [`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. |
Copy link

Copilot AI Mar 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This DSL reference entry for paginate_by_default? should mention that clients can still disable pagination with page: false, and that the option only has an effect when a default_limit is configured.

Suggested change
| [`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. |
| [`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. Has no effect unless a `default_limit` is configured; clients can still disable pagination by passing `page: false`. |

Copilot uses AI. Check for mistakes.



Expand Down Expand Up @@ -2699,7 +2700,7 @@ prepare build(sort: [:foo, :bar])

| Name | Type | Default | Docs |
|------|------|---------|------|
| [`on`](#preparations-prepare-on){: #preparations-prepare-on } | `:read \| :action \| list(:read \| :action)` | `[:read]` | The action types the preparation should run on. By default, preparations only run on read actions. Use `:action` to run on generic actions. |
| [`on`](#preparations-prepare-on){: #preparations-prepare-on } | `:read \| :action \| :create \| :update \| :destroy \| list(:read \| :action \| :create \| :update \| :destroy)` | `[:read]` | The action types the preparation should run on. By default, preparations only run on read actions. Use `:action` to run on generic actions. |
| [`where`](#preparations-prepare-where){: #preparations-prepare-where } | `(any, any -> any) \| module \| list((any, any -> any) \| module)` | `[]` | Validations that should pass in order for this preparation to apply. Any of these validations failing will result in this preparation being ignored. |
| [`only_when_valid?`](#preparations-prepare-only_when_valid?){: #preparations-prepare-only_when_valid? } | `boolean` | `false` | If the preparation should only run on valid queries. |

Expand Down
4 changes: 3 additions & 1 deletion lib/ash/actions/read/read.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3832,7 +3832,9 @@ defmodule Ash.Actions.Read do
action.pagination.default_limit ->
Keyword.put(page_opts, :limit, action.pagination.default_limit)

is_nil(page_opts) and action.pagination.required? and not relationship? ->
is_nil(page_opts) and
(action.pagination.required? or action.pagination.paginate_by_default?) and
not relationship? ->
if action.pagination.default_limit do
[limit: action.pagination.default_limit]
else
Expand Down
7 changes: 7 additions & 0 deletions lib/ash/resource/actions/read.ex
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,12 @@ defmodule Ash.Resource.Actions.Read do
doc:
"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.",
default: true
],
paginate_by_default?: [
type: :boolean,
doc:
"Whether or not to paginate by default when pagination is not required and no page parameters are provided.",
default: false
]
Comment on lines +153 to 158
Copy link

Copilot AI Mar 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new paginate_by_default? option docs don’t mention how to opt out (e.g. page: false) and effectively require default_limit to be set to have any impact. Consider updating the docstring to include both of those details so users understand how to get full results and what configuration is needed.

Copilot uses AI. Check for mistakes.
]

Expand All @@ -160,6 +166,7 @@ defmodule Ash.Resource.Actions.Read do
countable: false,
stable_sort: nil,
required?: false,
paginate_by_default?: false,
keyset?: false,
offset?: false,
__spark_metadata__: nil
Expand Down
16 changes: 16 additions & 0 deletions test/actions/pagination_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ defmodule Ash.Actions.PaginationTest do
pagination offset?: true, countable: true, required?: false
end

read :paginate_by_default do
pagination offset?: true,
countable: true,
required?: false,
paginate_by_default?: true,
default_limit: 3
end

read :offset_countable_by_default do
pagination offset?: true, countable: :by_default, required?: false
end
Expand Down Expand Up @@ -308,6 +316,14 @@ defmodule Ash.Actions.PaginationTest do

assert %{results: [%{name: "3"}]} = Ash.page!(page, :self)
end

test "paginate_by_default? applies default limit when no page opts" do
assert %Ash.Page.Offset{results: results} =
User
|> Ash.read!(action: :paginate_by_default)

assert Enum.count(results) == 3
Copy link

Copilot AI Mar 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add an assertion that page: false still returns the full, unpaginated result set for the :paginate_by_default action. The new option is intended to paginate only by default; callers should still be able to explicitly disable pagination, and this behavior isn’t currently covered by the new test.

Suggested change
assert Enum.count(results) == 3
assert Enum.count(results) == 3
results =
User
|> Ash.read!(action: :paginate_by_default, page: false)
assert is_list(results)
assert Enum.count(results) == 5

Copilot uses AI. Check for mistakes.
end
end

describe "keyset pagination with nil fields" do
Expand Down
Loading