• Stars
    star
    551
  • Rank 77,896 (Top 2 %)
  • Language
    Elixir
  • License
    MIT License
  • Created about 8 years ago
  • Updated 11 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Paginate your Ecto queries with Scrivener

Scrivener.Ecto

Build Status

Low Maintenance Warning

This library is in low maintenance mode, which means the author is currently only responding to pull requests and breaking issues.

Usage

Scrivener.Ecto allows you to paginate your Ecto queries with Scrivener. It gives you useful information such as the total number of pages, the current page, and the current page's entries. It works nicely with Phoenix as well.

First, you'll want to use Scrivener in your application's Ecto Repo. This will add a paginate function to your Repo. This paginate function expects to be called with, at a minimum, an Ecto query. It will then paginate the query and execute it, returning a Scrivener.Page. Defaults for page_size can be configured when you use Scrivener. If no page_size is provided, Scrivener will use 10 by default.

You may also want to call paginate with a params map along with your query. If provided with a params map, Scrivener will use the values in the keys "page" and "page_size" before using any configured defaults.

Note: Scrivener.Ecto only supports Ecto backends that allow subqueries (e.g. PostgreSQL).

Example

defmodule MyApp.Repo do
  use Ecto.Repo, otp_app: :my_app, adapter: Ecto.Adapters.Postgres
  use Scrivener, page_size: 10
end
defmodule MyApp.Person do
  use Ecto.Schema

  schema "people" do
    field :name, :string
    field :age, :integer

    has_many :friends, MyApp.Person
  end
end
def index(conn, params) do
  page =
    MyApp.Person
    |> where([p], p.age > 30)
    |> order_by(desc: :age)
    |> preload(:friends)
    |> MyApp.Repo.paginate(params)

  render conn, :index,
    people: page.entries,
    page_number: page.page_number,
    page_size: page.page_size,
    total_pages: page.total_pages,
    total_entries: page.total_entries
end
page =
  MyApp.Person
  |> where([p], p.age > 30)
  |> order_by(desc: :age)
  |> preload(:friends)
  |> MyApp.Repo.paginate(page: 2, page_size: 5)

Installation

Add scrivener_ecto to your mix.exs deps.

defp deps do
  [
    {:scrivener_ecto, "~> 2.0"}
  ]
end

Contributing

First, you'll need to build the test database.

MIX_ENV=test mix db.reset

This task assumes you have postgres installed and that the postgres user can create / drop databases. If you'd prefer to use a different user, you can specify it with the environment variable SCRIVENER_ECTO_DB_USER.

With the database built, you can now run the tests.

mix test

More Repositories

1

scrivener

Pagination for the Elixir ecosystem
Elixir
550
star
2

testflight

Painless http testing in Go
Go
149
star
3

ex_spec

BDD-like syntax for ExUnit
Elixir
98
star
4

vim_dotfiles

my vim dotfiles
Vim Script
43
star
5

ruby_focused_unit_test_vim

run a focused ruby unit test in vim
Vim Script
40
star
6

blox

Example app using Phoenix and Ecto. Obviously it's a blog. An ugly one.
Elixir
19
star
7

pipe

RSS filtering and aggregation
Ruby
17
star
8

epicbot

A Slack Bot for Epic Card Game
PureScript
12
star
9

dotfiles

dotfiles
Shell
12
star
10

diggr

ruby wrapper for the digg api with nice syntax
Ruby
9
star
11

def_macro

macros for ruby
Ruby
6
star
12

aoc-hs

Advent of Code in Haskell
Haskell
5
star
13

purescript-biscotti-cookie

Utilities for parsing and generating cookie headers in PureScript
PureScript
5
star
14

geologist

talk to your minecraft server with http
JavaScript
4
star
15

gnodego

A node wrapper around GNU Go
CoffeeScript
3
star
16

epicbot-hs

A Slack Bot for Epic Card Game -- in Haskell!
Haskell
3
star
17

params_cleaner

Don't use this, use strong parameters
Ruby
3
star
18

bridge-diagrams

Generate plain text bridge diagrams
Haskell
3
star
19

effectful-scotty-example

A simple example application using effectful and scotty
Haskell
2
star
20

purescript-biscotti-session

PureScript Session Management
PureScript
2
star
21

block-chainable

easier ruby DSLs with blocks
Ruby
2
star
22

aoc-ocaml

Advent of Code in OCaml
OCaml
2
star
23

concur-testing

Dhall
1
star
24

pie-highlight.vim

Vim Script
1
star
25

newtype-stack-problems

Dhall
1
star
26

uofc-graphql

Python
1
star
27

advent_of_code_2017

Advent of Code 2017
Elixir
1
star
28

timebomb

make your cucumber step definitions time aware
Ruby
1
star
29

vimux-elixir-test

Run ExUnit tests in vimux
Vim Script
1
star
30

ctf_3_solutions

Solutions to Stripe CTF 3
Go
1
star
31

purescript-httpure-contrib-biscotti

Helpers for using Biscotti Cookies and Sessions with HTTPure applications
PureScript
1
star
32

purescript-deques

PureScript Deques
PureScript
1
star
33

advent_of_code_2016

Advent of Code 2016 in Elixir
Elixir
1
star