Skip to content

Commit fc69f5e

Browse files
committed
Extract advanced features
1 parent 46fdd2a commit fc69f5e

File tree

2 files changed

+63
-58
lines changed

2 files changed

+63
-58
lines changed

usage-rules.md

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -10,64 +10,6 @@ SPDX-License-Identifier: MIT
1010

1111
AshPostgres is the PostgreSQL data layer for Ash Framework. It's the most fully-featured Ash data layer and should be your default choice unless you have specific requirements for another data layer. Any PostgreSQL version higher than 13 is fully supported.
1212

13-
## Advanced Features
14-
15-
### Manual Relationships
16-
17-
For complex relationships that can't be expressed with standard relationship types:
18-
19-
```elixir
20-
defmodule MyApp.Post.Relationships.HighlyRatedComments do
21-
use Ash.Resource.ManualRelationship
22-
use AshPostgres.ManualRelationship
23-
24-
def load(posts, _opts, context) do
25-
post_ids = Enum.map(posts, & &1.id)
26-
27-
{:ok,
28-
MyApp.Comment
29-
|> Ash.Query.filter(post_id in ^post_ids)
30-
|> Ash.Query.filter(rating > 4)
31-
|> MyApp.read!()
32-
|> Enum.group_by(& &1.post_id)}
33-
end
34-
35-
def ash_postgres_join(query, _opts, current_binding, as_binding, :inner, destination_query) do
36-
{:ok,
37-
Ecto.Query.from(_ in query,
38-
join: dest in ^destination_query,
39-
as: ^as_binding,
40-
on: dest.post_id == as(^current_binding).id,
41-
on: dest.rating > 4
42-
)}
43-
end
44-
45-
# Other required callbacks...
46-
end
47-
48-
# In your resource:
49-
relationships do
50-
has_many :highly_rated_comments, MyApp.Comment do
51-
manual MyApp.Post.Relationships.HighlyRatedComments
52-
end
53-
end
54-
```
55-
56-
### Using Multiple Repos (Read Replicas)
57-
58-
Configure different repos for reads vs mutations:
59-
60-
```elixir
61-
postgres do
62-
repo fn resource, type ->
63-
case type do
64-
:read -> MyApp.ReadReplicaRepo
65-
:mutate -> MyApp.WriteRepo
66-
end
67-
end
68-
end
69-
```
70-
7113
## Best Practices
7214

7315
1. **Organize migrations**: Run `mix ash.codegen` after each meaningful set of resource changes with a descriptive name:

usage-rules/advanced_features.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<!--
2+
SPDX-FileCopyrightText: 2020 Zach Daniel
3+
4+
SPDX-License-Identifier: MIT
5+
-->
6+
7+
# Advanced Features
8+
9+
## Manual Relationships
10+
11+
For complex relationships that can't be expressed with standard relationship types:
12+
13+
```elixir
14+
defmodule MyApp.Post.Relationships.HighlyRatedComments do
15+
use Ash.Resource.ManualRelationship
16+
use AshPostgres.ManualRelationship
17+
18+
def load(posts, _opts, context) do
19+
post_ids = Enum.map(posts, & &1.id)
20+
21+
{:ok,
22+
MyApp.Comment
23+
|> Ash.Query.filter(post_id in ^post_ids)
24+
|> Ash.Query.filter(rating > 4)
25+
|> MyApp.read!()
26+
|> Enum.group_by(& &1.post_id)}
27+
end
28+
29+
def ash_postgres_join(query, _opts, current_binding, as_binding, :inner, destination_query) do
30+
{:ok,
31+
Ecto.Query.from(_ in query,
32+
join: dest in ^destination_query,
33+
as: ^as_binding,
34+
on: dest.post_id == as(^current_binding).id,
35+
on: dest.rating > 4
36+
)}
37+
end
38+
39+
# Other required callbacks...
40+
end
41+
42+
# In your resource:
43+
relationships do
44+
has_many :highly_rated_comments, MyApp.Comment do
45+
manual MyApp.Post.Relationships.HighlyRatedComments
46+
end
47+
end
48+
```
49+
50+
## Using Multiple Repos (Read Replicas)
51+
52+
Configure different repos for reads vs mutations:
53+
54+
```elixir
55+
postgres do
56+
repo fn resource, type ->
57+
case type do
58+
:read -> MyApp.ReadReplicaRepo
59+
:mutate -> MyApp.WriteRepo
60+
end
61+
end
62+
end
63+
```

0 commit comments

Comments
 (0)