View Source Dataloader.KV (dataloader v2.0.0)

Simple KV based Dataloader source.

This module is a simple key value based data loader source. You must supply a function that accepts a batch key and list of ids, and returns a map of values keyed by id.

Values may optionally be returned as :ok / :error tuples to indicate success of the operation.

example

Example

def datasource do
  Dataloader.KV.new(&query/2, max_concurrency: 1)
end

def query(:comments, posts) do
  Map.new(posts, fn %{id: post_id} = post ->
    {post, Comments.find_by(post_id: post_id)}
  end)
end

Link to this section Summary

Functions

Create a KV Dataloader source.

Link to this section Functions

Link to this function

new(load_function, opts \\ [])

View Source

Create a KV Dataloader source.

Dataloader runs tasks concurrently using Task.async_stream/3. The concurrency of a KV Dataloader source and the time tasks are allowed to run can be controlled via options (see the "Options" section below).

options

Options

  • :max_concurrency - sets the maximum number of tasks to run at the same time. Defaults to twice the number of schedulers online (see System.schedulers_online/0).
  • :timeout - the maximum amount of time (in milliseconds) a task is allowed to execute for. Defaults to 30000.
  • :async? - set to false to disable the asynchronous behavior mentioned above.