docs: Add some examples to the Identities guide, with a link to the DSL docs (#1097)

This commit is contained in:
Rebecca Le 2024-05-02 23:25:50 +08:00 committed by GitHub
parent 245ee998b4
commit c1fd3f36ad
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -2,7 +2,28 @@
Identities are a way to declare that a record (an instance of a resource) can be uniquely identified by a set of attributes. This information can be used in various ways throughout the framework. The primary key of the resource does not need to be listed as an identity. Identities are a way to declare that a record (an instance of a resource) can be uniquely identified by a set of attributes. This information can be used in various ways throughout the framework. The primary key of the resource does not need to be listed as an identity.
## Using Ash.get ## Defining an identity
Identities are defined at the top level of a resource module, eg.
```elixir
defmodule MyApp.MyResource do
use Ash.Resource #, ...
# ...
identities do
# If the `email` attribute must be unique across all records
identity :unique_email, [:email]
# If the `username` attribute must be unique for every record with a given `site` value
identity :special_usernames, [:username, :site]
end
end
```
See `d:Ash.Resource.Dsl.identities` for the full range of options available when defining identities.
## Using `Ash.get`
This will allow these fields to be passed to `Ash.get/3`, e.g `Ash.get(Resource, %{email: "foo"})`. This will allow these fields to be passed to `Ash.get/3`, e.g `Ash.get(Resource, %{email: "foo"})`.