Files
decentralised_book_index/lib/decentralised_book_index/metadata/dbi_server.ex
T

121 lines
2.6 KiB
Elixir

defmodule DecentralisedBookIndex.Metadata.DBIServer do
use Ash.Resource,
otp_app: :decentralised_book_index,
domain: DecentralisedBookIndex.Metadata,
data_layer: AshPostgres.DataLayer,
authorizers: [Ash.Policy.Authorizer],
extensions: [AshJsonApi.Resource]
alias DecentralisedBookIndex.Metadata
json_api do
type "dbi_server"
end
postgres do
table "dbi_servers"
repo DecentralisedBookIndex.Repo
custom_indexes do
index "name gin_trgm_ops", name: "server_name_gin_index", using: "GIN"
end
end
resource do
description "An DBIServer's metadata."
end
actions do
defaults [:read, :destroy]
create :create do
primary? true
accept [:name, :url, :sync_on?, :dbi_server_id]
end
create :sync_create do
accept [:id, :name, :url, :inserted_at, :updated_at, :dbi_server_id]
end
update :update do
primary? true
accept [:name, :url, :sync_on?]
end
update :sync do
accept [:name, :url, :inserted_at, :updated_at, :dbi_server_id]
end
read :by_id do
description "Return the DBIServer by its id."
argument :id, :uuid, allow_nil?: false
get? true
filter expr(id == ^arg(:id))
end
read :search do
description "Return a list of DBIServers, optionally filtering by name."
argument :name, :ci_string do
description "Return DBIServers, which names includes the name."
constraints allow_empty?: true
default ""
end
filter expr(contains(name, ^arg(:name)))
pagination offset?: true, default_limit: 10
end
end
policies do
bypass actor_attribute_equals(:role, :admin) do
authorize_if always()
end
policy action_type(:read) do
authorize_if always()
end
end
attributes do
uuid_primary_key :id, writable?: true
attribute :name, :string do
allow_nil? false
public? true
end
attribute :url, :string do
allow_nil? false
public? true
end
attribute :sync_on?, :boolean do
allow_nil? true
default false
end
timestamps do
writable? true
public? true
end
end
relationships do
belongs_to :dbi_server, Metadata.DBIServer do
public? true
end
has_many :author, Metadata.Author
has_many :author_alias_registries, Metadata.AuthorAliasRegistry
has_many :author_roles, Metadata.AuthorRole
has_many :books, Metadata.Book
has_many :bids, Metadata.BookId
has_many :book_editions_registries, Metadata.BookEditionsRegistry
has_many :publisher, Metadata.Publisher
end
end