improvement: make default page type configurable, defaulting to :offset

The getting started guide and ash installer set it to `:keyset`

closes #1413
This commit is contained in:
Zach Daniel 2024-08-30 17:18:50 -04:00
parent 9b0fb15ecb
commit f4dc9620ae
5 changed files with 17 additions and 41 deletions

View file

@ -253,7 +253,9 @@ import Config
config :helpdesk, :ash_domains, [Helpdesk.Support]
config :ash, :include_embedded_source_by_default?, true
config :ash,
include_embedded_source_by_default?: false,
default_page_type: :keyset
```
### Try our first resource out

View file

@ -1737,7 +1737,13 @@ defmodule Ash.Actions.Read do
Ash.Page.Keyset.new(data, count, sort, original_query, more?, opts)
action.pagination.offset? && action.pagination.keyset? ->
case Application.get_env(:ash, :default_page_type, :offset) do
:keyset ->
Ash.Page.Keyset.new(data, count, sort, original_query, more?, opts)
:offset ->
Ash.Page.Offset.new(data, count, original_query, more?, opts)
end
action.pagination.offset? ->
Ash.Page.Offset.new(data, count, original_query, more?, opts)

View file

@ -1498,7 +1498,6 @@ defmodule Ash.Resource.Dsl do
Ash.Resource.Verifiers.VerifyIdentityFields,
Ash.Resource.Verifiers.EnsureAggregateFieldIsAttributeOrCalculation,
Ash.Resource.Verifiers.ValidateRelationshipAttributes,
Ash.Resource.Verifiers.CountableActions,
Ash.Resource.Verifiers.NoReservedFieldNames,
Ash.Resource.Verifiers.ValidateAccept,
Ash.Resource.Verifiers.ValidateActionTypesSupported,

View file

@ -1,37 +0,0 @@
defmodule Ash.Resource.Verifiers.CountableActions do
@moduledoc """
Ensures that countable paginated actions do not exist for resources that are not countable
"""
use Spark.Dsl.Verifier
alias Spark.Dsl.Verifier
# sobelow_skip ["DOS.BinToAtom"]
def verify(dsl_state) do
dsl_state
|> Verifier.get_entities([:actions])
|> Enum.filter(fn action ->
action.type == :read && action.pagination && action.pagination.countable
end)
|> case do
[] ->
:ok
[action | _] ->
data_layer = Verifier.get_persisted(dsl_state, :data_layer)
resource = Verifier.get_persisted(dsl_state, :module)
if data_layer && data_layer.can?(resource, {:query_aggregate, :count}) do
:ok
else
{:error,
Spark.Error.DslError.exception(
module: resource,
path: [:actions, action.name],
message:
"Action cannot be countable, as the datalayer does not support counting queries"
)}
end
end
end
end

View file

@ -57,7 +57,13 @@ defmodule Mix.Tasks.Ash.Install do
"config.exs",
:ash,
[:include_embedded_source_by_default?],
true
false
)
|> Igniter.Project.Config.configure(
"config.exs",
:ash,
[:default_page_type],
:keyset
)
|> then(fn igniter ->
if "--example" in argv do