Skip to content

Commit 2c71b39

Browse files
committed
use Dataloader for loading activity associations
1 parent 4f2df85 commit 2c71b39

File tree

3 files changed

+22
-28
lines changed

3 files changed

+22
-28
lines changed

lib/algora/activities/activities.ex

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -142,37 +142,29 @@ defmodule Algora.Activities do
142142
|> Algora.Repo.insert()
143143
end
144144

145-
def all_with_assoc(query) do
146-
multi =
147-
Multi.new()
148-
|> Multi.run(:activities, fn repo, _changes ->
149-
{:ok, repo.all(query)}
150-
end)
151-
|> Multi.run(:associations, fn repo, %{activities: activities} ->
152-
associations =
153-
Enum.map(activities, fn activity ->
154-
repo.one(from b in schema_from_table(activity.assoc_name), where: b.id == ^activity.assoc_id)
155-
end)
156-
157-
{:ok, associations}
158-
end)
159-
|> Multi.run(:preloaded, fn _repo, %{activities: activities, associations: assocs} ->
160-
preloaded =
161-
Enum.zip_with(activities, assocs, fn act, assoc ->
162-
Map.put(act, :assoc, assoc)
163-
end)
145+
def dataloader() do
146+
Dataloader.add_source(
147+
Dataloader.new,
148+
:db,
149+
Dataloader.Ecto.new(Algora.Repo)
150+
)
151+
end
164152

165-
{:ok, preloaded}
153+
def all_with_assoc(query) do
154+
activities = Repo.all(query)
155+
loader =
156+
activities
157+
|> Enum.reduce(dataloader(), fn(activity, loader) ->
158+
schema = schema_from_table(activity.assoc_name)
159+
Dataloader.load(loader, :db, schema, activity.assoc_id)
166160
end)
161+
|> Dataloader.run()
167162

168-
case Repo.transaction(multi) do
169-
{:ok, %{preloaded: preloaded} = a} ->
170-
preloaded
171-
172-
{:error, _step, reason, _changes} ->
173-
reason
174-
# Handle error
175-
end
163+
Enum.map(activities, fn(activity) ->
164+
schema = schema_from_table(activity.assoc_name)
165+
assoc = Dataloader.get(loader, :db, schema, activity.assoc_id)
166+
Map.put(activity, :assoc, assoc)
167+
end)
176168
end
177169

178170
def schema_from_table("identity_activities"), do: Algora.Accounts.Identity

mix.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ defmodule Algora.MixProject do
8686
{:chameleon, "~> 2.2.0"},
8787
{:ex_machina, "~> 2.8.0", only: [:dev, :test]},
8888
{:excoveralls, "~> 0.18", only: :test},
89+
{:dataloader, "~> 2.0.0"},
8990
# ex_aws
9091
{:ex_aws, "~> 2.1"},
9192
{:ex_aws_s3, "~> 2.0"},

mix.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"chameleon": {:hex, :chameleon, "2.2.1", "7b3d745ee1abfea26c0160590cabe3d102e7e020bba0c405176b3464c90f42a8", [:mix], [], "hexpm", "2643ececff1824793d607551418c8f79bee01395c13cf14530b27acb2903c6e5"},
1010
"cldr_utils": {:hex, :cldr_utils, "2.28.2", "f500667164a9043369071e4f9dcef31f88b8589b2e2c07a1eb9f9fa53cb1dce9", [:mix], [{:castore, "~> 0.1 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:certifi, "~> 2.5", [hex: :certifi, repo: "hexpm", optional: true]}, {:decimal, "~> 1.9 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}], "hexpm", "c506eb1a170ba7cdca59b304ba02a56795ed119856662f6b1a420af80ec42551"},
1111
"credo": {:hex, :credo, "1.7.11", "d3e805f7ddf6c9c854fd36f089649d7cf6ba74c42bc3795d587814e3c9847102", [:mix], [{:bunt, "~> 0.2.1 or ~> 1.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:file_system, "~> 0.2 or ~> 1.0", [hex: :file_system, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "56826b4306843253a66e47ae45e98e7d284ee1f95d53d1612bb483f88a8cf219"},
12+
"dataloader": {:hex, :dataloader, "2.0.2", "c45075e0692e68638a315e14f747bd8d7065fb5f38705cf980f62d4cd344401f", [:mix], [{:ecto, ">= 3.4.3 and < 4.0.0", [hex: :ecto, repo: "hexpm", optional: true]}, {:opentelemetry_process_propagator, "~> 0.2.1 or ~> 0.3", [hex: :opentelemetry_process_propagator, repo: "hexpm", optional: true]}, {:telemetry, "~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "4c6cabc0b55e96e7de74d14bf37f4a5786f0ab69aa06764a1f39dda40079b098"},
1213
"db_connection": {:hex, :db_connection, "2.7.0", "b99faa9291bb09892c7da373bb82cba59aefa9b36300f6145c5f201c7adf48ec", [:mix], [{:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "dcf08f31b2701f857dfc787fbad78223d61a32204f217f15e881dd93e4bdd3ff"},
1314
"decimal": {:hex, :decimal, "2.3.0", "3ad6255aa77b4a3c4f818171b12d237500e63525c2fd056699967a3e7ea20f62", [:mix], [], "hexpm", "a4d66355cb29cb47c3cf30e71329e58361cfcb37c34235ef3bf1d7bf3773aeac"},
1415
"decorator": {:hex, :decorator, "1.4.0", "a57ac32c823ea7e4e67f5af56412d12b33274661bb7640ec7fc882f8d23ac419", [:mix], [], "hexpm", "0a07cedd9083da875c7418dea95b78361197cf2bf3211d743f6f7ce39656597f"},

0 commit comments

Comments
 (0)