DataLoader for Elixir
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
config Ecto 3 migration Dec 19, 2018
lib Fix run_batch function specs Jan 18, 2019
priv/test_repo/migrations Fix loading for nested through associations Dec 27, 2018
test Merge pull request #63 from milmazz/ecto_sql Jan 4, 2019
.formatter.exs use task async stream for parallelism Jan 27, 2018
.gitignore Use a MapSet for keys, to keep it consistent with Dataloader.Ecto, an… Mar 15, 2018
.tool-versions
.tool-versions-e my tool versions Sep 12, 2018
.travis.yml
CHANGELOG.md version bump Feb 7, 2019
LICENSE.md Add License. Fixes #22 Mar 8, 2018
README.md Fix typo in README Aug 4, 2018
mix.exs version bump Feb 7, 2019
mix.lock Ecto 3 migration Dec 19, 2018

README.md

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 suit Elixir use cases.

Installation

The package can be installed by adding dataloader to your list of dependencies in mix.exs:

def deps do
  [
    {:dataloader, "~> 1.0.0"}
  ]
end

Usage

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

Sources

Dataloader ships with two different built in sources. The first is the Ecto source for easily pulling out data with ecto. The other is a simple KV key value source. See each module for its respective documentation.

Anything that implements the Dataloader.Source protocol can act as a source.

Documentation can be generated with ExDoc. The docs can be found at https://hexdocs.pm/dataloader.