AlecStewart1#1125
AlecStewart1#1125
AEAsh Elixir
Created by AlecStewart1#1125 on 11/29/2023 in #support
Simple policy checks based with relationships
Oh whoops! Of course!
6 replies
AEAsh Elixir
Created by AlecStewart1#1125 on 11/29/2023 in #support
Simple policy checks based with relationships
And of course... A Role resource:
defmodule MyApp.Accounts.Role do
use Ash.Resource,
data_layer: AshPostgres.DataLayer

postgres do
table "roles"
repo MyApp.Repo
end

attributes do
uuid_primary_key :id
attribute :name, :string do
allow_nil? false
end
attribute :description, :string do
allow_nil? false
end
end

relationships do
has_many :users, MyApp.Accounts.User
many_to_many :permissions, MyApp.Accounts.Role do
through MyApp.Accounts.RolePermission
source_attribute_on_join_resource :role_id
destination_attribute_on_join_resource :permission_id
end
end
end
defmodule MyApp.Accounts.Role do
use Ash.Resource,
data_layer: AshPostgres.DataLayer

postgres do
table "roles"
repo MyApp.Repo
end

attributes do
uuid_primary_key :id
attribute :name, :string do
allow_nil? false
end
attribute :description, :string do
allow_nil? false
end
end

relationships do
has_many :users, MyApp.Accounts.User
many_to_many :permissions, MyApp.Accounts.Role do
through MyApp.Accounts.RolePermission
source_attribute_on_join_resource :role_id
destination_attribute_on_join_resource :permission_id
end
end
end
A Permission resource:
defmodule MyApp.Accounts.Permission do
use Ash.Resource,
data_layer: AshPostgres.DataLayer

postgres do
table "permissiones"
repo MyApp.Repo
end

attributes do
uuid_primary_key :id
attribute :name, :string do
allow_nil? false
end
attribute :description, :string do
allow_nil? false
end
end

relationships do
many_to_many :roles, MyApp.Accounts.Permission do
through MyApp.Accounts.RolePermission
source_attribute_on_join_resource :permission_id
destination_attribute_on_join_resource :role_id
end
many_to_many :users, MyApp.Accounts.Permission do
through MyApp.Accounts.UserPermission
source_attribute_on_join_resource :permission_id
destination_attribute_on_join_resource :user_id
end
end
end
defmodule MyApp.Accounts.Permission do
use Ash.Resource,
data_layer: AshPostgres.DataLayer

postgres do
table "permissiones"
repo MyApp.Repo
end

attributes do
uuid_primary_key :id
attribute :name, :string do
allow_nil? false
end
attribute :description, :string do
allow_nil? false
end
end

relationships do
many_to_many :roles, MyApp.Accounts.Permission do
through MyApp.Accounts.RolePermission
source_attribute_on_join_resource :permission_id
destination_attribute_on_join_resource :role_id
end
many_to_many :users, MyApp.Accounts.Permission do
through MyApp.Accounts.UserPermission
source_attribute_on_join_resource :permission_id
destination_attribute_on_join_resource :user_id
end
end
end
6 replies
AEAsh Elixir
Created by AlecStewart1#1125 on 11/29/2023 in #support
Simple policy checks based with relationships
A RolePermission resource:
defmodule MyApp.Accounts.RolePermission do
use Ash.Resource,
data_layer: AshPostgres.DataLayer

postgres do
table "role_permissions"
repo MyApp.Repo
end

relationships do
belongs_to :role, MyApp.Accounts.Role do
primary_key? true
allow_nil? false
end
belongs_to :permission, MyApp.Accounts.Permission do
primary_key? true
allow_nil? false
end
end
end
defmodule MyApp.Accounts.RolePermission do
use Ash.Resource,
data_layer: AshPostgres.DataLayer

postgres do
table "role_permissions"
repo MyApp.Repo
end

relationships do
belongs_to :role, MyApp.Accounts.Role do
primary_key? true
allow_nil? false
end
belongs_to :permission, MyApp.Accounts.Permission do
primary_key? true
allow_nil? false
end
end
end
A UserPermission resource:
defmodule MyApp.Accounts.UserPermission do
use Ash.Resource,
data_layer: AshPostgres.DataLayer

postgres do
table "user_permissions"
repo MyApp.Repo
end

relationships do
belongs_to :user, MyApp.Accounts.User do
primary_key? true
allow_nil? false
end
belongs_to :permission, MyApp.Accounts.Permission do
primary_key? true
allow_nil? false
end
end
end
defmodule MyApp.Accounts.UserPermission do
use Ash.Resource,
data_layer: AshPostgres.DataLayer

postgres do
table "user_permissions"
repo MyApp.Repo
end

relationships do
belongs_to :user, MyApp.Accounts.User do
primary_key? true
allow_nil? false
end
belongs_to :permission, MyApp.Accounts.Permission do
primary_key? true
allow_nil? false
end
end
end
6 replies
AEAsh Elixir
Created by AlecStewart1#1125 on 11/29/2023 in #support
Simple policy checks based with relationships
To give some more clarity, here's some other resources:
6 replies
AEAsh Elixir
Created by AlecStewart1#1125 on 6/6/2023 in #support
Aggregates with resource relationships
Oh shoot. I forgot about the Oban library. That could fit our use cases pretty well if we don't feel like having cron jobs that run mix.
25 replies
AEAsh Elixir
Created by AlecStewart1#1125 on 6/6/2023 in #support
Aggregates with resource relationships
Also I guess there's always the solution of creating materialized views with SQL in the migrations.
25 replies
AEAsh Elixir
Created by AlecStewart1#1125 on 6/6/2023 in #support
Aggregates with resource relationships
Well looks like I found what I might need. https://ash-hq.org/docs/dsl/ash-resource#postgres-custom_indexes
25 replies
AEAsh Elixir
Created by AlecStewart1#1125 on 6/6/2023 in #support
Aggregates with resource relationships
Oh okay. Well it says that field is required but it defaults to the primary ID so I was confused as to whether or not it's actually required.
25 replies
AEAsh Elixir
Created by AlecStewart1#1125 on 6/6/2023 in #support
Aggregates with resource relationships
I guess that's more of a question about Postgres than Ash...
25 replies
AEAsh Elixir
Created by AlecStewart1#1125 on 6/6/2023 in #support
Aggregates with resource relationships
Well I'll try that and see if works. How exactly do aggregates get created and stored? So a state could have like 20000 cities, so if I call for that aggregate somewhere with Ash am I going to have to wait for that aggregate to be initialized the first time?
25 replies
AEAsh Elixir
Created by AlecStewart1#1125 on 6/6/2023 in #support
Aggregates with resource relationships
Do I need to just have list :cities_in_state, :cities, :id?
25 replies
AEAsh Elixir
Created by AlecStewart1#1125 on 6/6/2023 in #support
Aggregates with resource relationships
The field to aggregate. Defaults to the first field in the primary key of the resource
25 replies
AEAsh Elixir
Created by AlecStewart1#1125 on 6/6/2023 in #support
Aggregates with resource relationships
So for list, if the default field is the primary ID
25 replies
AEAsh Elixir
Created by AlecStewart1#1125 on 6/6/2023 in #support
Aggregates with resource relationships
That would also include fields for actual relationships between resources.
25 replies
AEAsh Elixir
Created by AlecStewart1#1125 on 6/6/2023 in #support
Aggregates with resource relationships
The relationship or relationship path to use for the aggregate
25 replies
AEAsh Elixir
Created by AlecStewart1#1125 on 6/6/2023 in #support
Aggregates with resource relationships
Okay, I didn't quite know if when it said
25 replies
AEAsh Elixir
Created by AlecStewart1#1125 on 2/27/2023 in #support
Ash Way of Doing Password Hashing and Storage for Phoenix.
Oh okay, I looked at the ash_hq project and it's making more sense to me now.
22 replies
AEAsh Elixir
Created by AlecStewart1#1125 on 2/27/2023 in #support
Ash Way of Doing Password Hashing and Storage for Phoenix.
Oh okay, as an example, it's abstracting some of the CRUD actions you'd have to do for creating/updating a user password, right? Because I have the create :signup but I'd also need an update action for resetting the password etc.
22 replies
AEAsh Elixir
Created by AlecStewart1#1125 on 2/27/2023 in #support
Ash Way of Doing Password Hashing and Storage for Phoenix.
Mostly just flexibility?
22 replies
AEAsh Elixir
Created by AlecStewart1#1125 on 2/27/2023 in #support
Ash Way of Doing Password Hashing and Storage for Phoenix.
Ah okay, I think I'm following. What are the advantages to using the ash_authentication as opposed to doing it yourself?
22 replies