Packages
ash
1.50.16
3.29.3
3.29.2
3.29.1
3.29.0
3.28.0
3.27.8
3.27.7
3.27.6
3.27.5
3.27.4
3.27.3
3.27.2
3.27.1
3.27.0
3.26.0
3.25.2
3.25.1
3.25.0
retired
3.24.7
3.24.6
3.24.5
3.24.4
3.24.3
3.24.2
3.24.1
3.24.0
3.23.1
3.23.0
3.22.2
3.22.1
3.22.0
3.21.3
3.21.2
3.21.1
3.21.0
3.20.0
3.19.3
3.19.2
3.19.1
3.19.0
3.18.0
3.17.1
3.17.0
3.16.0
3.15.0
3.14.1
3.14.0
retired
3.13.2
3.13.1
3.13.0
3.12.0
3.11.3
3.11.2
3.11.1
3.11.0
3.10.1
3.10.0
3.9.0
3.8.0
3.7.6
3.7.5
3.7.4
3.7.3
3.7.2
3.7.1
3.7.0
retired
3.6.3
retired
3.6.2
3.6.1
3.6.0
3.5.43
3.5.42
3.5.41
3.5.40
3.5.39
3.5.38
3.5.37
3.5.36
3.5.35
3.5.34
3.5.33
3.5.32
3.5.31
3.5.30
3.5.29
3.5.28
3.5.27
3.5.26
3.5.25
3.5.24
3.5.23
3.5.22
3.5.21
3.5.20
3.5.19
3.5.18
3.5.17
3.5.16
3.5.15
3.5.14
3.5.13
3.5.12
3.5.11
3.5.10
3.5.9
3.5.8
3.5.7
3.5.6
3.5.5
3.5.4
3.5.3
3.5.2
3.5.1
3.5.0
3.4.74
retired
3.4.73
3.4.72
3.4.71
3.4.70
3.4.69
3.4.68
3.4.67
3.4.66
3.4.65
3.4.64
3.4.63
3.4.62
3.4.61
3.4.60
3.4.59
3.4.58
3.4.57
3.4.56
3.4.55
3.4.54
3.4.53
3.4.52
3.4.51
3.4.50
3.4.49
3.4.48
3.4.47
3.4.46
3.4.45
3.4.44
3.4.43
3.4.42
3.4.41
3.4.40
3.4.39
3.4.38
3.4.37
3.4.36
3.4.35
3.4.34
3.4.33
3.4.32
3.4.31
3.4.30
3.4.29
3.4.28
3.4.27
3.4.26
3.4.25
3.4.24
3.4.23
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.19.6
2.19.5
2.19.4
2.19.3
retired
2.19.2
retired
2.19.1
retired
2.19.0
retired
2.18.2
2.18.1
2.18.0
2.17.24
2.17.23
2.17.22
2.17.21
2.17.20
2.17.19
2.17.18
2.17.17
2.17.16
2.17.15
2.17.14
2.17.13
2.17.12
2.17.11
2.17.10
2.17.9
2.17.8
2.17.7
2.17.6
2.17.5
2.17.4
2.17.3
2.17.2
2.17.1
2.17.0
2.16.1
2.16.0
2.15.20
2.15.19
2.15.18
2.15.17
2.15.16
2.15.15
2.15.14
2.15.13
2.15.12
2.15.11
2.15.10
2.15.9
2.15.8
2.15.7
2.15.6
2.15.5
2.15.4
2.15.2
2.15.1
2.15.0
2.14.21
2.14.20
2.14.19
2.14.18
2.14.17
2.14.16
2.14.15
2.14.14
2.14.13
2.14.12
2.14.11
2.14.10
2.14.9
2.14.8
2.14.7
2.14.6
2.14.5
2.14.4
2.14.3
2.14.2
2.14.1
2.14.0
2.13.4
retired
2.13.3
2.13.2
2.13.1
2.13.0
2.12.1
2.12.0
2.11.11
2.11.10
2.11.9
2.11.8
2.11.7
2.11.6
2.11.5
2.11.4
2.11.3
2.11.2
2.11.1
2.11.0
2.11.0-rc.3
2.11.0-rc.2
2.11.0-rc.1
2.11.0-rc.0
2.10.2
2.10.1
2.10.0
2.9.29
2.9.28
2.9.27
2.9.26
2.9.25
2.9.24
2.9.23
2.9.22
2.9.21
2.9.20
2.9.19
2.9.18
2.9.17
2.9.16
2.9.15
2.9.14
2.9.13
2.9.12
2.9.11
2.9.10
2.9.9
2.9.8
2.9.7
2.9.6
2.9.5
2.9.4
2.9.3
2.9.2
2.9.1
2.9.0
2.8.1
2.8.0
2.7.1
2.7.0
2.6.31
2.6.30
2.6.29
2.6.28
2.6.27
2.6.26
2.6.25
2.6.24
2.6.23
2.6.22
2.6.21
2.6.20
2.6.19
2.6.18
2.6.17
2.6.16
2.6.15
2.6.14
2.6.13
2.6.11
2.6.10
2.6.9
2.6.8
2.6.7
2.6.6
2.6.5
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50.15
1.50.14
1.50.13
1.50.12
1.50.11
1.50.10
1.50.9
1.50.8
1.50.7
1.50.6
1.50.5
1.50.4
1.50.3
1.50.2
1.50.1
1.50.0
1.49.0
1.48.0-rc.30
1.48.0-rc.29
1.48.0-rc.28
1.48.0-rc.27
1.48.0-rc.26
1.48.0-rc.25
1.48.0-rc.24
1.48.0-rc.23
1.48.0-rc.22
1.48.0-rc.21
1.48.0-rc.20
1.48.0-rc.19
1.48.0-rc.18
1.48.0-rc.17
1.48.0-rc.16
1.48.0-rc.15
1.48.0-rc.14
1.48.0-rc.13
1.48.0-rc.12
1.48.0-rc.11
1.48.0-rc.10
1.48.0-rc.9
1.48.0-rc.8
1.48.0-rc.7
1.48.0-rc.6
1.48.0-rc.5
1.48.0-rc.4
1.48.0-rc.3
1.48.0-rc.2
1.48.0-rc.1
1.48.0-rc.0
1.47.12
1.47.11
1.47.10
1.47.9
1.47.8
1.47.7
1.47.6
1.47.5
1.47.4
1.47.3
1.47.2
1.47.1
1.47.0
1.46.13
1.46.12
1.46.11
1.46.10
1.46.9
1.46.8
1.46.7
1.46.6
1.46.5
1.46.4
1.46.3
1.46.2
1.46.1
1.46.0
1.45.0-rc9
1.45.0-rc8
1.45.0-rc7
1.45.0-rc6
1.45.0-rc5
1.45.0-rc4
1.45.0-rc3
1.45.0-rc20
1.45.0-rc2
1.45.0-rc19
1.45.0-rc18
1.45.0-rc17
1.45.0-rc16
1.45.0-rc15
1.45.0-rc14
1.45.0-rc13
1.45.0-rc12
1.45.0-rc11
1.45.0-rc10
1.45.0-rc1
1.45.0-rc0
1.44.13
1.44.12
1.44.11
1.44.10
1.44.9
1.44.8
1.44.7
1.44.6
1.44.5
1.44.4
1.44.3
1.44.2
1.44.1
1.44.0
1.43.12
1.43.11
1.43.10
1.43.9
1.43.8
1.43.7
1.43.6
1.43.5
1.43.4
1.43.3
1.43.2
1.43.1
1.43.0
1.42.0
1.41.12
1.41.11
1.41.10
1.41.9
1.41.8
1.41.7
1.41.6
1.41.5
1.41.4
1.41.3
1.41.2
1.41.1
1.41.0
1.40.0
1.39.7
1.39.6
1.39.5
1.39.4
1.39.3
1.39.2
1.39.1
1.39.0
1.38.0
1.37.2
1.37.1
1.37.0
1.36.22
1.36.21
1.36.19
1.36.18
1.36.17
1.36.16
1.36.15
1.36.14
1.36.13
1.36.12
1.36.11
1.36.10
1.36.9
1.36.8
1.36.7
1.36.6
1.36.5
1.36.4
1.36.3
1.36.2
1.36.0
1.35.1
1.35.0
1.34.9
1.34.8
1.34.7
1.34.6
1.34.5
1.34.4
1.34.3
1.34.2
1.34.1
1.34.0
1.33.0
1.32.2
1.32.1
1.32.0
1.31.1
1.31.0
1.30.2
1.30.1
1.29.0-rc1
1.29.0-rc0
1.28.1
1.28.0
1.27.1
1.27.0
1.26.13
1.26.12
1.26.11
1.26.10
1.26.9
1.26.8
1.26.7
1.26.6
1.26.5
1.26.4
1.26.2
1.26.1
1.26.0
1.25.8
1.25.7
1.25.6
1.25.5
1.25.4
1.25.3
1.25.2
1.25.1
1.25.0
1.24.2
1.24.1
1.24.0
1.23.3
1.23.2
1.23.1
1.23.0
1.22.1
1.22.0
1.20.1
1.20.0
1.19.1
1.19.0
1.18.1
1.18.0
1.17.1
1.17.0
1.16.2
1.15.1
1.15.0
1.14.0
1.13.4
1.13.3
1.13.2
1.13.1
1.13.0
1.12.0
1.11.1
1.11.0
1.10.0
1.9.0
1.8.0
1.7.0
1.6.8
1.6.7
1.6.6
1.6.5
1.6.4
1.6.3
1.6.2
1.6.1
1.6.0
1.5.1
1.5.0
1.4.1
1.4.0
1.3.1
1.3.0
1.2.1
1.2.0
1.1.3
1.1.2
1.1.0
1.0.3
1.0.2
1.0.1
1.0.0
0.13.1
0.13.0
0.12.0
0.10.0
0.9.1
0.9.0
0.8.0
0.7.0
0.6.5
0.6.4
0.6.3
0.6.2
0.6.1
0.6.0
0.5.2
0.5.1
0.5.0
0.4.0
0.3.0
0.2.0
0.1.9
0.1.8
0.1.3
0.1.1
0.1.0
A declarative, extensible framework for building Elixir applications.
Security advisory:
This version has known vulnerabilities.
View advisories
Current section
Files
Jump to
Current section
Files
lib/ash/resource/dsl.ex
defmodule Ash.Resource.Dsl do
@attribute %Ash.Dsl.Entity{
name: :attribute,
describe: """
Declares an attribute on the resource
Type can be either a built in type (see `Ash.Type`) for more, or a module
implementing the `Ash.Type` behaviour.
*Strings are trimmed by default*. If you want to retain whitespace, use
`attribute :foo, :string, constraints: [trim?: false]`
""",
examples: [
"""
attribute :first_name, :string do
primary_key? true
end
"""
],
target: Ash.Resource.Attribute,
args: [:name, :type],
schema: Ash.Resource.Attribute.attribute_schema()
}
@create_timestamp %Ash.Dsl.Entity{
name: :create_timestamp,
describe: """
Declares a non-writable attribute with a create default of `&DateTime.utc_now/0`
""",
examples: [
"create_timestamp :inserted_at"
],
target: Ash.Resource.Attribute,
args: [:name],
schema: Ash.Resource.Attribute.create_timestamp_schema()
}
@update_timestamp %Ash.Dsl.Entity{
name: :update_timestamp,
describe: """
Declares a non-writable attribute with a create and update default of `&DateTime.utc_now/0`
""",
examples: [
"update_timestamp :inserted_at"
],
target: Ash.Resource.Attribute,
schema: Ash.Resource.Attribute.update_timestamp_schema(),
args: [:name]
}
@timestamps %Ash.Dsl.Entity{
name: :timestamps,
describe: """
Declares non-writable `inserted_at` and `updated_at` attributes whith create and update defaults of `&DateTime.utc_now/0`.
""",
examples: [
"timestamps()"
],
target: Ash.Resource.Attribute,
auto_set_fields: [
name: :__timestamps__
]
}
@integer_primary_key %Ash.Dsl.Entity{
name: :integer_primary_key,
describe: """
Declares a generated (set by the data layer), non writable, non nil, primary key column of type integer.
Using `integer_primary_key`, `allow_nil?` is automatically set to `false`.
""",
examples: [
"integer_primary_key :id"
],
args: [:name],
target: Ash.Resource.Attribute,
schema: Ash.Resource.Attribute.integer_primary_key_schema(),
auto_set_fields: [allow_nil?: false]
}
@uuid_primary_key %Ash.Dsl.Entity{
name: :uuid_primary_key,
describe: """
Declares a non writable, non nil, primary key column of type uuid, which defaults to `Ash.UUID.generate/0`.
Using `uuid_primary_key`, `allow_nil?` is automatically set to `false`.
""",
examples: [
"uuid_primary_key :id"
],
args: [:name],
target: Ash.Resource.Attribute,
schema: Ash.Resource.Attribute.uuid_primary_key_schema(),
auto_set_fields: [allow_nil?: false]
}
@attributes %Ash.Dsl.Section{
name: :attributes,
describe: """
A section for declaring attributes on the resource.
Attributes are fields on an instance of a resource. The two required
pieces of knowledge are the field name, and the type.
""",
examples: [
"""
attributes do
uuid_primary_key :id
attribute :first_name, :string do
allow_nil? false
end
attribute :last_name, :string do
allow_nil? false
end
attribute :email, :string do
allow_nil? false
constraints [
match: ~r/^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$/
]
end
attribute :type, :atom do
constraints [
one_of: [:admin, :teacher, :student]
]
end
create_timestamp :inserted_at
update_timestamp :updated_at
end
"""
],
entities: [
@attribute,
@create_timestamp,
@update_timestamp,
@timestamps,
@integer_primary_key,
@uuid_primary_key
]
}
@has_one %Ash.Dsl.Entity{
name: :has_one,
describe: """
Declares a has_one relationship. In a relationsal database, the foreign key would be on the *other* table.
Generally speaking, a `has_one` also implies that the destination table is unique on that foreign key.
""",
examples: [
"""
# In a resource called `Word`
has_one :dictionary_entry, DictionaryEntry do
source_field :text
destination_field :word_text
end
"""
],
modules: [:destination],
target: Ash.Resource.Relationships.HasOne,
schema: Ash.Resource.Relationships.HasOne.opt_schema(),
args: [:name, :destination]
}
@has_many %Ash.Dsl.Entity{
name: :has_many,
describe: """
Declares a has_many relationship. There can be any number of related entities.
""",
examples: [
"""
# In a resource called `Word`
has_many :definitions, DictionaryDefinition do
source_field :text
destination_field :word_text
end
"""
],
target: Ash.Resource.Relationships.HasMany,
modules: [:destination],
schema: Ash.Resource.Relationships.HasMany.opt_schema(),
args: [:name, :destination]
}
@many_to_many %Ash.Dsl.Entity{
name: :many_to_many,
describe: """
Declares a many_to_many relationship. Many to many relationships require a join table.
A join table is typically a table who's primary key consists of one foreign key to each resource.
""",
examples: [
"""
# In a resource called `Word`
many_to_many :books, Book do
through BookWord
source_field :text
source_field_on_join_table :word_text
destination_field :id
destination_field_on_join_table :book_id
end
# And in `BookWord` (the resource that defines the join table)
belongs_to :book, Book, primary_key?: true, required?: true
belongs_to :word, Word, primary_key?: true, required?: true
"""
],
modules: [:destination, :through],
target: Ash.Resource.Relationships.ManyToMany,
schema: Ash.Resource.Relationships.ManyToMany.opt_schema(),
transform: {Ash.Resource.Relationships.ManyToMany, :transform, []},
args: [:name, :destination]
}
@belongs_to %Ash.Dsl.Entity{
name: :belongs_to,
describe: """
Declares a belongs_to relationship. In a relational database, the foreign key would be on the *source* table.
This creates a field on the resource with the corresponding name and type, unless `define_field?: false` is provided.
""",
examples: [
"""
# In a resource called `Word`
belongs_to :dictionary_entry, DictionaryEntry do
source_field :text,
destination_field :word_text
end
"""
],
modules: [:destination],
target: Ash.Resource.Relationships.BelongsTo,
schema: Ash.Resource.Relationships.BelongsTo.opt_schema(),
args: [:name, :destination]
}
@relationships %Ash.Dsl.Section{
name: :relationships,
describe: """
A section for declaring relationships on the resource.
Relationships are a core component of resource oriented design. Many components of Ash
will use these relationships. A simple use case is loading relationships (done via the `Ash.Query.load/2`).
""",
examples: [
"""
relationships do
belongs_to :post, MyApp.Post do
primary_key? true
end
belongs_to :category, MyApp.Category do
primary_key? true
end
end
""",
"""
relationships do
belongs_to :author, MyApp.Author
many_to_many :categories, MyApp.Category do
through MyApp.PostCategory
destination_field_on_join_table :category_id
source_field_on_join_table :post_id
end
end
""",
"""
relationships do
has_many :posts, MyApp.Post do
destination_field :author_id
end
has_many :composite_key_posts, MyApp.CompositeKeyPost do
destination_field :author_id
end
end
"""
],
imports: [
Ash.Filter.TemplateHelpers
],
entities: [
@has_one,
@has_many,
@many_to_many,
@belongs_to
]
}
@action_change %Ash.Dsl.Entity{
name: :change,
describe: """
A change to be applied to the changeset after it is generated. They are run in order, from top to bottom.
To implement your own, see `Ash.Resource.Change`.
To use it, you can simply refer to the module and its options, like so:
`change {MyChange, foo: 1}`
But for readability, you may want to define a function elsewhere and import it,
so you can say something like:
`change my_change(1)`
For destroys, `changes` are not applied unless `soft?` is set to true.
""",
examples: [
"change relate_actor(:reporter)",
"change {MyCustomChange, :foo}"
],
target: Ash.Resource.Change,
transform: {Ash.Resource.Change, :transform, []},
schema: Ash.Resource.Change.action_schema(),
args: [:change]
}
@action_argument %Ash.Dsl.Entity{
name: :argument,
describe: """
Declares an argument on the action
The type can be either a built in type (see `Ash.Type`) for more, or a module implementing
the `Ash.Type` behaviour.
""",
examples: [
"argument :password_confirmation, :string"
],
target: Ash.Resource.Actions.Argument,
args: [:name, :type],
schema: Ash.Resource.Actions.Argument.schema()
}
@metadata %Ash.Dsl.Entity{
name: :metadata,
describe: """
A special kind of attribute that is only added to specific actions. Nothing sets this value, it must be set in a custom
change via `Ash.Resource.Info.put_metadata/3`.
""",
examples: [
"""
metadata :api_token, :string, allow_nil?: false
""",
"""
metadata :operation_id, :string, allow_nil?: false
"""
],
target: Ash.Resource.Actions.Metadata,
args: [:name, :type],
schema: Ash.Resource.Actions.Metadata.schema()
}
@change %Ash.Dsl.Entity{
name: :change,
describe: """
A change to be applied to the changeset after it is generated. They are run in order, from top to bottom.
To implement your own, see `Ash.Resource.Change`.
To use it, you can simply refer to the module and its options, like so:
`change {MyChange, foo: 1}`
But for readability, you may want to define a function elsewhere and import it,
so you can say something like:
`change my_change(1)`
For destroys, `changes` are not applied unless `soft?` is set to true.
""",
examples: [
"change relate_actor(:reporter)",
"change {MyCustomChange, :foo}"
],
target: Ash.Resource.Change,
transform: {Ash.Resource.Change, :transform, []},
schema: Ash.Resource.Change.schema(),
args: [:change]
}
@validate %Ash.Dsl.Entity{
name: :validate,
describe: """
Declares a validation for creates and updates.
""",
examples: [
"validate {Mod, [foo: :bar]}",
"validate at_least_one_of_present([:first_name, :last_name])"
],
target: Ash.Resource.Validation,
schema: Ash.Resource.Validation.opt_schema(),
transform: {Ash.Resource.Validation, :transform, []},
args: [:validation]
}
@action_validate %Ash.Dsl.Entity{
name: :validate,
describe: """
Declares a validation for the current action
""",
examples: [
"validate changing(:email)"
],
target: Ash.Resource.Validation,
schema: Ash.Resource.Validation.action_schema(),
transform: {Ash.Resource.Validation, :transform, []},
args: [:validation]
}
@create %Ash.Dsl.Entity{
name: :create,
describe: """
Declares a `create` action. For calling this action, see the `Ash.Api` documentation.
""",
examples: [
"""
create :register do
primary? true
end
"""
],
target: Ash.Resource.Actions.Create,
schema: Ash.Resource.Actions.Create.opt_schema(),
entities: [
changes: [
@action_change,
@action_validate
],
arguments: [
@action_argument
],
metadata: [
@metadata
]
],
args: [:name]
}
@preparation %Ash.Dsl.Entity{
name: :prepare,
describe: """
Declares a preparation, which can be used to prepare a query for a read action.
""",
examples: [
"""
prepare default_sort([:foo, :bar])
"""
],
target: Ash.Resource.Preparation,
schema: Ash.Resource.Preparation.schema(),
args: [:preparation],
transform: {Ash.Resource.Preparation, :transform, []}
}
@read %Ash.Dsl.Entity{
name: :read,
describe: """
Declares a `read` action. For calling this action, see the `Ash.Api` documentation.
## Pagination
#{Ash.OptionsHelpers.docs(Ash.Resource.Actions.Read.pagination_schema())}
""",
examples: [
"""
read :read_all do
primary? true
end
"""
],
target: Ash.Resource.Actions.Read,
schema: Ash.Resource.Actions.Read.opt_schema(),
entities: [
arguments: [
@action_argument
],
preparations: [
@preparation
]
],
args: [:name]
}
@update %Ash.Dsl.Entity{
name: :update,
describe: """
Declares a `update` action. For calling this action, see the `Ash.Api` documentation.
""",
examples: [
"update :flag_for_review, primary?: true"
],
entities: [
changes: [
@action_change,
@action_validate
],
metadata: [
@metadata
],
arguments: [
@action_argument
]
],
target: Ash.Resource.Actions.Update,
schema: Ash.Resource.Actions.Update.opt_schema(),
args: [:name]
}
@destroy %Ash.Dsl.Entity{
name: :destroy,
describe: """
Declares a `destroy` action. For calling this action, see the `Ash.Api` documentation.
""",
examples: [
"""
destroy :soft_delete do
primary? true
end
"""
],
entities: [
changes: [
@action_change,
@action_validate
],
metadata: [
@metadata
],
arguments: [
@action_argument
]
],
target: Ash.Resource.Actions.Destroy,
schema: Ash.Resource.Actions.Destroy.opt_schema(),
args: [:name]
}
@actions %Ash.Dsl.Section{
name: :actions,
describe: """
A section for declaring resource actions.
All manipulation of data through the underlying data layer happens through actions.
There are four types of action: `create`, `read`, `update`, and `destroy`. You may
recognize these from the acronym `CRUD`. You can have multiple actions of the same
type, as long as they have different names. This is the primary mechanism for customizing
your resources to conform to your business logic. It is normal and expected to have
multiple actions of each type in a large application.
## Primary actions
If you have multiple actions of the same type, one of them must be designated as the
primary action for that type, via the `primary?` option. This tells the ash what to do
if an action of that type is requested, but no specific action name is given. This is how
many relationship changes will happen, by utilizing the primary actions. For this reason,
** when defining actions, you usually want to ensure that the primary action takes no required
arguments **. Without that, relationship changes to your resources might fail due to missing
arguments. This does, however, allow you to customize exactly how related entities are read/
created.
## Turning primary actions off
If you want an extremely explicit experience with actions, you can specify the following two options:
```elixir
defaults []
primary_actions? false
```
This will prevent Ash from adding a default implementation of each action type, as well as cause any calls
to `Ash.Resource.Info.primary_action/2` to raise an error. This is experimental, but any errors raised in appropriate
places can be addressed, just report them as issues.
""",
imports: [
Ash.Resource.Change.Builtins,
Ash.Resource.Preparation.Builtins,
Ash.Resource.Validation.Builtins,
Ash.Filter.TemplateHelpers
],
schema: [
primary_actions?: [
type: {:in, [true, false, :only_read]},
default: true,
doc: """
Causes any calls to `Ash.Resource.Info.primary_action/2` to raise an error. This is experimental, but any errors raised in appropriate
places can be addressed, just report them as issues. Use `:read_only` to allow primary actions only for reads.
This can be useful to avoid constantly having to provide the basic `read` action of a given resource, when you need
to do things like loading data.
"""
],
defaults: [
type: {:list, {:in, [:create, :read, :update, :destroy]}},
default: [:create, :read, :update, :destroy],
doc: """
By default, an action of each type is added to each resource.
If any other actions of that same type are added, the default of that type is *not*
added. If you wish to skip adding defaults of certain types, specify this option
with the defaults that you *do* want implemented.
"""
]
],
examples: [
"""
actions do
create :signup do
argument :password, :string
argument :password_confirmation, :string
validate confirm(:password, :password_confirmation)
change {MyApp.HashPassword, []} # A custom implemented Change
end
read :me do
# An action that auto filters to only return the user for the current user
filter [id: actor(:id)]
end
update :update do
accept [:first_name, :last_name]
end
destroy do
change set_attribute(:deleted_at, &DateTime.utc_now/0)
# This tells it that even though this is a delete action, it
# should be treated like an update because `deleted_at` is set.
# This should be coupled with a `base_filter` on the resource
# or with the read actions having a `filter` for `is_nil: :deleted_at`
soft? true
end
end
"""
],
entities: [
@create,
@read,
@update,
@destroy
]
}
@identity %Ash.Dsl.Entity{
name: :identity,
describe: """
Represents a unique constraint on the resource.
Used for indicating that some set of attributes, calculations or aggregates uniquely identify a resource.
This will allow these fields to be passed to `c:Ash.Api.get/3`, e.g `get(Resource, [some_field: 10])`,
if all of the keys are filterable. Otherwise they are purely descriptive at the moment.
The primary key of the resource does not need to be listed as an identity.
""",
examples: [
"identity :name, [:name]",
"identity :full_name, [:first_name, :last_name]"
],
target: Ash.Resource.Identity,
schema: Ash.Resource.Identity.schema(),
args: [:name, :keys]
}
@identities %Ash.Dsl.Section{
name: :identities,
describe: """
Unique identifiers for the resource
""",
examples: [
"""
identities do
identity :full_name, [:first_name, :last_name]
identity :email, [:email]
end
"""
],
entities: [
@identity
]
}
@resource %Ash.Dsl.Section{
name: :resource,
describe: """
Resource-wide configuration
""",
examples: [
"""
resource do
description "A description of this resource"
base_filter [is_nil: :deleted_at]
end
"""
],
imports: [Ash.Filter.TemplateHelpers],
schema: [
description: [
type: :string,
doc: "A human readable description of the resource, to be used in generated documentation"
],
base_filter: [
type: :any,
doc: "A filter statement to be applied to any queries on the resource"
],
default_context: [
type: :any,
doc: "Default context to apply to any queries/changesets generated for this resource."
]
]
}
@define %Ash.Dsl.Entity{
name: :define,
describe: """
Defines a function on the Api with the corresponding name and arguments.
If the action is an update or destroy, it will take a record or a changeset as its *first* argument.
If the action is a read action, it will take a starting query as an *opt in the last* argument.
All functions will have an optional last argument that accepts options. Those options are:
#{Ash.OptionsHelpers.docs(Ash.Resource.Interface.interface_options(nil))}
For reads:
* `:query` - a query to start the action with, can be used to filter/sort the results of the action.
For creates:
* `:changeset` - a changeset to start the action with
They will also have an optional second to last argument that is a freeform map to provide action input. It *must be a map*.
If it is a keyword list, it will be assumed that it is actually `options` (for convenience).
This allows for the following behaviour:
```elixir
# Because the 3rd argument is a keyword list, we use it as options
Api.register_user(username, password, [tenant: "organization_22"])
# Because the 3rd argument is a keyword list, we use it as action input
Api.register_user(username, password, %{key: "val"})
# When all are provided it is unambiguous
Api.register_user(username, password, %{key: "val"}, [tenant: "organization_22"])
```
""",
examples: [
"define :get_user_by_id, action: :get_by_id, args: [:id], get?: true"
],
target: Ash.Resource.Interface,
schema: Ash.Resource.Interface.schema(),
transform: {Ash.Resource.Interface, :transform, []},
args: [:name]
}
@code_interface %Ash.Dsl.Section{
name: :code_interface,
describe: """
Functions that will be defined on the Api module to interact with this resource.
""",
examples: [
"""
code_interface do
define_for MyApp.Api
define :create_user, action: :create
define :get_user_by_id, action: :get_by_id, args: [:id], get?: true
end
"""
],
schema: [
define_for: [
type: {:behaviour, Ash.Api},
doc:
"Defines the code interface on the resource module directly, using the provided Api.",
default: false
]
],
entities: [
@define
]
}
@validations %Ash.Dsl.Section{
name: :validations,
describe: """
Declare validations prior to performing actions against the resource
""",
imports: [Ash.Resource.Validation.Builtins],
examples: [
"""
validations do
validate {Mod, [foo: :bar]}
validate at_least_one_of_present([:first_name, :last_name])
end
"""
],
entities: [
@validate
]
}
@changes %Ash.Dsl.Section{
name: :changes,
describe: """
Declare changes that occur on create/update/destroy actions against the resource
""",
imports: [Ash.Resource.Validation.Builtins, Ash.Resource.Change.Builtins],
examples: [
"""
changes do
change {Mod, [foo: :bar]}
change set_context(%{some: :context})
end
"""
],
entities: [
@change
]
}
@preparations %Ash.Dsl.Section{
name: :preparations,
describe: """
Declare preparations that occur on all read actions for a given resource
""",
imports: [Ash.Resource.Validation.Builtins],
examples: [
"""
preparations do
prepare {Mod, [foo: :bar]}
prepare set_context(%{some: :context})
end
"""
],
entities: [
@preparation
]
}
@count %Ash.Dsl.Entity{
name: :count,
describe: """
Declares a named count aggregate on the resource
Supports `filter`, but not `sort` (because that wouldn't affect the count)
""",
examples: [
"""
count :assigned_ticket_count, :assigned_tickets do
filter [active: true]
end
"""
],
target: Ash.Resource.Aggregate,
args: [:name, :relationship_path],
schema: Keyword.delete(Ash.Resource.Aggregate.schema(), :sort),
auto_set_fields: [kind: :count]
}
@first %Ash.Dsl.Entity{
name: :first,
describe: """
Declares a named `first` aggregate on the resource
First aggregates return the first value of the related record
that matches. Supports both `filter` and `sort`.
""",
examples: [
"""
first :first_assigned_ticket_subject, :assigned_tickets, :subject do
filter [active: true]
sort [:subject]
end
"""
],
target: Ash.Resource.Aggregate,
args: [:name, :relationship_path, :field],
schema: Ash.Resource.Aggregate.schema(),
auto_set_fields: [kind: :first]
}
@sum %Ash.Dsl.Entity{
name: :sum,
describe: """
Declares a named `sum` aggregate on the resource
Supports `filter`, but not `sort` (because that wouldn't affect the sum)
""",
examples: [
"""
sum :assigned_ticket_price_sum, :assigned_tickets, :price do
filter [active: true]
end
"""
],
target: Ash.Resource.Aggregate,
args: [:name, :relationship_path, :field],
schema: Keyword.delete(Ash.Resource.Aggregate.schema(), :sort),
auto_set_fields: [kind: :sum]
}
@list %Ash.Dsl.Entity{
name: :list,
describe: """
Declares a named `list` aggregate on the resource.
A list aggregate simply selects the list of all values for the given field
and relationship combination.
""",
examples: [
"""
list :assigned_ticket_prices, :assigned_tickets, :price do
filter [active: true]
end
"""
],
target: Ash.Resource.Aggregate,
args: [:name, :relationship_path, :field],
schema: Ash.Resource.Aggregate.schema(),
auto_set_fields: [kind: :list]
}
@aggregates %Ash.Dsl.Section{
name: :aggregates,
describe: """
Declare named aggregates on the resource.
These are aggregates that can be loaded only by name using `Ash.Query.load/2`.
They are also available as top level fields on the resource.
""",
examples: [
"""
aggregates do
count :assigned_ticket_count, :reported_tickets do
filter [active: true]
end
end
"""
],
imports: [Ash.Filter.TemplateHelpers],
entities: [
@count,
@first,
@sum,
@list
]
}
@argument %Ash.Dsl.Entity{
name: :argument,
describe: """
An argument to be passed into the calculation's arguments map
""",
examples: [
"""
argument :params, :map do
default %{}
end
""",
"""
argument :retries, :integer do
allow_nil? false
end
"""
],
target: Ash.Resource.Calculation.Argument,
args: [:name, :type],
schema: Ash.Resource.Calculation.Argument.schema()
}
@calculation %Ash.Dsl.Entity{
name: :calculate,
describe: """
Declares a named calculation on the resource.
Takes a module that must adopt the `Ash.Calculation` behaviour. See that module
for more information.
To ensure that the necessary fields are selected:
1.) Specifying the `select` option on a calculation in the resource.
2.) Define a `select/2` callback in the calculation module
3.) Set `always_select?` on the attribute in question
""",
examples: [
{
"`Ash.Calculation` implementation example:",
"calculate :full_name, :string, {MyApp.FullName, keys: [:first_name, :last_name]}, select: [:first_name, :last_name]"
},
{
"`expr/1` example:",
"calculate :full_name, :string, expr(first_name <> \" \" <> last_name "
}
],
target: Ash.Resource.Calculation,
args: [:name, :type, :calculation],
entities: [
arguments: [@argument]
],
schema: Ash.Resource.Calculation.schema()
}
@calculations %Ash.Dsl.Section{
name: :calculations,
describe: """
Declare named calculations on the resource.
These are calculations that can be loaded only by name using `Ash.Query.load/2`.
They are also available as top level fields on the resource.
""",
examples: [
"""
calculations do
calculate :full_name, :string, MyApp.MyResource.FullName
end
"""
],
imports: [Ash.Resource.Calculation.Builtins, Ash.Filter.TemplateHelpers],
entities: [
@calculation
]
}
@multitenancy %Ash.Dsl.Section{
name: :multitenancy,
describe: """
Options for configuring the multitenancy behavior of a resource.
To specify a tenant, use `Ash.Query.set_tenant/2` or
`Ash.Changeset.set_tenant/2` before passing it to an operation.
""",
examples: [
"""
multitenancy do
strategy :attribute
attribute :organization_id
global? true
end
"""
],
schema: [
strategy: [
type: {:in, [:context, :attribute]},
default: :context,
doc: """
Determine how to perform multitenancy. `:attribute` will expect that an
attribute matches the given `tenant`, e.g `org_id`. `context` (the default)
implies that the tenant will be passed to the datalayer as context. How a
given data layer handles multitenancy will differ depending on the implementation.
See the datalayer documentation for more.
"""
],
attribute: [
type: :atom,
doc: """
If using the `attribute` strategy, the attribute to use, e.g `org_id`
"""
],
global?: [
type: :boolean,
doc: """
Whether or not the data also exists outside of each tenant. This allows running queries
and making changes without setting a tenant. This may eventually be extended to support
describing the relationship to global data. For example, perhaps the global data is
shared among all tenants (requiring "union" support in data layers), or perhaps global
data is "merged" using some strategy (also requiring "union" support).
""",
default: false
],
parse_attribute: [
type: :mfa,
doc:
"An mfa ({module, function, args}) pointing to a function that takes a tenant and returns the attribute value",
default: {__MODULE__, :identity, []}
]
]
}
@sections [
@identities,
@attributes,
@relationships,
@actions,
@resource,
@changes,
@preparations,
@validations,
@aggregates,
@calculations,
@multitenancy,
@code_interface
]
@transformers [
Ash.Resource.Transformers.ValidateManagedRelationshipOpts,
Ash.Resource.Transformers.RequireUniqueActionNames,
Ash.Resource.Transformers.SetRelationshipSource,
Ash.Resource.Transformers.BelongsToAttribute,
Ash.Resource.Transformers.BelongsToSourceField,
Ash.Resource.Transformers.HasDestinationField,
Ash.Resource.Transformers.CreateJoinRelationship,
Ash.Resource.Transformers.CachePrimaryKey,
Ash.Resource.Transformers.ReplaceTimestamps,
Ash.Resource.Transformers.SetPrimaryActions,
Ash.Resource.Transformers.ValidateActionTypesSupported,
Ash.Resource.Transformers.CountableActions,
Ash.Resource.Transformers.ValidateMultitenancy,
Ash.Resource.Transformers.DefaultPrimaryKey,
Ash.Resource.Transformers.DefaultAccept,
Ash.Resource.Transformers.SetTypes,
Ash.Resource.Transformers.ValidateRelationshipAttributes
]
@moduledoc """
The built in resource DSL. The core DSL components of a resource are:
# Table of Contents
#{Ash.Dsl.Extension.doc_index(@sections)}
#{Ash.Dsl.Extension.doc(@sections)}
"""
use Ash.Dsl.Extension,
sections: @sections,
transformers: @transformers
@doc false
def identity(x), do: x
end