ash_postgres v0.36.1 AshPostgres.DataLayer View Source
A postgres data layer that levereges Ecto's postgres capabilities.
Table of Contents
- postgres
- manage_tenant
- references
- reference
postgres
Postgres data layer configuration
- manage_tenant
- references
- reference
Examples:
postgres do
repo MyApp.Repo
table "organizations"
end
:repo
- Required. The repo that will be used to fetch your data. See theAshPostgres.Repo
documentation for more:migrate?
- Whether or not to include this resource in the generated migrations withmix ash.generate_migrations
The default value istrue
.:base_filter_sql
- A raw sql version of the base_filter, e.grepresentative = true
. Required if trying to create a unique constraint on a resource with a base_filter:skip_unique_indexes
- Skip generating unique indexes when generating migrations The default value isfalse
.:unique_index_names
- A list of unique index names that could raise errors, or an mfa to a function that takes a changeset and returns the list. Must be in the format{[:affected, :keys], "name_of_constraint"}
or{[:affected, :keys], "name_of_constraint", "custom error message"}
The default value is[]
.:foreign_key_names
- A list of foreign keys that could raise errors, or an mfa to a function that takes a changeset and returns the list. Must be in the format{:key, "name_of_constraint"}
or{:key, "name_of_constraint", "custom error message"}
The default value is[]
.:table
- The table to store and read the resource from. Required unlesspolymorphic?
is true.:polymorphic?
- Declares this resource as polymorphic.
Polymorphic resources cannot be read or updated unless the table is provided in the query/changeset context.
For example:
PolymorphicResource |> Ash.Query.set_context(%{data_layer: %{table: "table"}}) |> MyApi.read!()
When relating to polymorphic resources, you'll need to use thecontext
option on relationships, e.g
belongs_to :polymorphic_association, PolymorphicResource, context: %{data_layer: %{table: "table"}} The default value isfalse
.
manage_tenant
Configuration for the behavior of a resource that manages a tenant
Examples:
manage_tenant do
template ["organization_", :id]
create? true
update? false
end
:template
- Required. A template that will cause the resource to create/manage the specified schema.
Use this if you have a resource that, when created, it should create a new tenant for you. For example, if you have acustomer
resource, and you want to create a schema for each customer based on their id, e.gcustomer_10
set this option to["customer_", :id]
. Then, when this is created, it will create a schema called["customer_", :id]
, and run your tenant migrations on it. Then, if you were to change that customer's id to20
, it would rename the schema tocustomer_20
. Generally speaking you should avoid changing the tenant id.:create?
- Whether or not to automatically create a tenant when a record is created The default value istrue
.:update?
- Whether or not to automatically update the tenant name if the record is udpated The default value istrue
.
references
A section for configuring the references (foreign keys) in resource migrations.
This section is only relevant if you are using the migration generator with this resource. Otherwise, it has no effect.
Examples:
references do
reference :post, on_delete: :delete, on_update: :update, name: "comments_to_posts_fkey"
end
:polymorphic_on_delete
- For polymorphic resources, configures the on_delete behavior of the automatically generated foreign keys to source tables.:polymorphic_on_update
- For polymorphic resources, configures the on_update behavior of the automatically generated foreign keys to source tables.:polymorphic_name
- For polymorphic resources, configures the on_update behavior of the automatically generated foreign keys to source tables.
reference
Configures the reference for a relationship in resource migrations.
Keep in mind that multiple relationships can theoretically involve the same destination and foreign keys.
In those cases, you only need to configure the reference
behavior for one of them. Any conflicts will result
in an error, across this resource and any other resources that share a table with this one. For this reason,
instead of adding a reference configuration for :nothing
, its best to just leave the configuration out, as that
is the default behavior if no relationship anywhere has configured the behavior of that reference.
Introspection Target:
Examples:
reference :post, on_delete: :delete, on_update: :update, name: "comments_to_posts_fkey"
:relationship
- Required. The relationship to be configured:on_delete
- What should happen to records of this resource when the referenced record of the destination resource is deleted.
The difference between:nothing
and:restrict
is subtle and, if you are unsure, choose:nothing
(the default behavior).:restrict
will prevent the deletion from happening before the end of the database transaction, whereas:nothing
allows the transaction to complete before doing so. This allows for things like deleting the destination row and then deleting the source row.Important!
No resource logic is applied with this operation! No authorization rules or validations take place, and no notifications are issued. This operation happens directly in the database.
This option is calledon_delete
, instead ofon_destroy
, because it is hooking into the database level deletion, not adestroy
action in your resource.:on_update
- What should happen to records of this resource when the referenced destination_field of the destination record is update.
The difference between:nothing
and:restrict
is subtle and, if you are unsure, choose:nothing
(the default behavior).:restrict
will prevent the deletion from happening before the end of the database transaction, whereas:nothing
allows the transaction to complete before doing so. This allows for things like updating the destination row and then updating the reference as long as you are in a transaction.Important!
No resource logic is applied with this operation! No authorization rules or validations take place, and no notifications are issued. This operation happens directly in the database.
:name
- The name of the foreign key to generate in the database. Defaults to <table>_<source_field>_fkey