Basics

Backend module

defmodule MyApp.Flop do
  use Flop,
    adapter_opts: [repo: MyApp.Repo],
    default_limit: 25
end

Usage

MyApp.Flop.validate_and_run(
  MyApp.Pet,
  params,
  for: MyApp.Pet
)

Application environment

import Config

config :flop,
  adapter_opts: [
    repo: MyApp.Repo
  ]

Look-up order

  1. Function arguments
  2. Schema options with @derive Flop.Schema
  3. Backend module with use Flop
  4. Application environment
  5. Library defaults

Options

Pagination

use Flop,
  adapter_opts: [repo: MyApp.Repo],
  default_limit: 25,
  max_limit: 100,
  pagination_types: [:page, :first],
  default_pagination_type: :page

Disabling features

use Flop,
  adapter_opts: [repo: MyApp.Repo],
  filtering: false,
  ordering: false,
  pagination: false

Parameters of a disabled feature are ignored. ordering: false still applies the default order, and pagination: false still applies the default limit.

Invalid parameters

use Flop, adapter_opts: [repo: MyApp.Repo], replace_invalid_params: true

Invalid parameters are replaced or removed instead of returning errors.

Cursor values

use Flop,
  adapter_opts: [repo: MyApp.Repo],
  cursor_value_func: &MyApp.Cursors.get_cursor/2,
  max_cursor_size: 8_192

Defaults

OptionDefault
adapter_opts[]
cursor_value_funcFlop.Cursor.get_cursor_from_node/2
default_limit50
max_limit1000
max_filters20
default_pagination_type:offset
pagination_types[:offset, :page, :first, :last]
filteringtrue
orderingtrue
paginationtrue

default_limit, max_limit and max_filters also take false. max_filters is not a schema option.

Ecto adapter

Repo and query options

use Flop,
  adapter_opts: [
    repo: MyApp.Repo,
    query_opts: [prefix: "public", timeout: 30_000]
  ]

repo is required.

Overriding per call

MyApp.Flop.all(MyApp.Pet, flop, repo: MyApp.ReplicaRepo)

Adapter options are merged, so query_opts is kept.

Legacy form

use Flop, repo: MyApp.Repo, query_opts: [prefix: "public"]

Both forms end up under adapter_opts.

Call-time options

These options are not accepted by use Flop.

Flop.validate_and_run(MyApp.Pet, params,
  for: MyApp.Pet,
  count_query: count_query,
  extra_opts: [timezone: "Asia/Tokyo"]
)
OptionPurpose
forSchema module that derives Flop.Schema
default_orderAlso a schema option
countKnown count, skips the count query
count_querySeparate query for counting
extra_optsPassed on to custom filter functions