dataloader v1.0.1 Dataloader

Dataloader

Dataloader provides an easy way efficiently load data in batches. It’s inspired by https://github.com/facebook/dataloader, although it makes some small API changes to better suite Elixir use cases.

Central to Dataloader is the idea of a source. A single Dataloader struct can have many different sources, which represent different ways to load data.

Here’s an example of a data loader using an ecto source, and then loading some organization data.

source = Dataloader.Ecto.new(MyApp.Repo)

# setup the loader
loader = Dataloader.new |> Dataloader.add_source(:db, source)

# load some things
loader =
  loader
  |> Dataloader.load(:db, Organization, 1)
  |> Dataloader.load_many(:db, Organization, [4, 9])

# actually retrieve them
loader = Dataloader.run(loader)

# Now we can get whatever values out we want
organizations = Dataloader.get_many(loader, :db, Organization, [1,4])

This will do a single SQL query to get all organizations by ids 1,4, and 9. You can load multiple batches from multiple sources, and then when run/1 is called batch will be loaded concurrently.

Here we named the source :db within our dataloader. More commonly though if you’re using Phoenix you’ll want to name it after one of your contexts, and have a different source used for each context. This provides an easy way to enforce data access rules within each context. See the DataLoader.Ecto moduledocs for more details

Link to this section Summary

Link to this section Types

Link to this type option()
option() :: {:timeout, pos_integer()}
Link to this type source_name()
source_name() :: any()
Link to this type t()
t() :: %Dataloader{options: [option()], sources: %{optional(source_name()) => Dataloader.Source.t()}}

Link to this section Functions

Link to this function add_source(loader, name, source)
add_source(t(), source_name(), Dataloader.Source.t()) :: t()
Link to this function get(loader, source, batch_key, item_key)
get(t(), source_name(), any(), any()) :: any() | no_return()
Link to this function get_many(loader, source, batch_key, item_keys)
get_many(t(), source_name(), any(), any()) :: [any()] | no_return()
Link to this function load(loader, source_name, batch_key, val)
load(t(), source_name(), any(), any()) :: t() | no_return()
Link to this function load_many(loader, source_name, batch_key, vals)
load_many(t(), source_name(), any(), [any()]) :: t() | no_return()
Link to this function new(opts \\ [])
new([option()]) :: t()
Link to this function pending_batches?(loader)
pending_batches?(t()) :: boolean()
Link to this function put(loader, source_name, batch_key, item_key, result)
Link to this function run(dataloader)
run(t()) :: t() | no_return()