Skip to content

Repository files navigation

AshDefaultSort

Configure a default sort to apply when a read action or a has_many relationship has no sort.

Installation

Add ash_default_sort to your list of dependencies in mix.exs:

defdepsdo[{:ash_default_sort,"~> 0.2.0"}]end

Usage

defmodulePostdouseAsh.Resource,data_layer: Ash.DataLayer.Postgres,extensions: [AshDefaultSort]relationshipsdohas_many:comments,Commenthas_many:tags,Tagdosort[id: :asc]endendactionsdoread:readdoprimary?trueendread:read_sorteddopreparebuild(sort: [id: :desc])endread:read_alldoendread:read_everydoendenddefault_sortdosort[like_count: :desc,created_at: :desc]has_many_sort[id: :desc]include_primary_read?falseexcept[:read_all]endend

In the example above, [like_count: :desc, created_at: :desc] is applied to read_every.

This is because read is excluded by include_primary_read? false, read_sorted is not affected because it already includes a sort, and read_all is excluded by the except option.

And the default_sort for comments is set to [id: :desc] because has_many_sort is present, and comments has no sort or default_sort options.

License

MIT

About

Configure a default sort to apply when a read action has no sort.

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages