• Stars
    star
    572
  • Rank 77,995 (Top 2 %)
  • Language
    Elixir
  • License
    MIT License
  • Created over 8 years ago
  • Updated about 1 month 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

jeet

The most advanced, yet intuitive, grid system available for Sass or Stylus
CSS
2,985
star
2

torch

A rapid admin generator for Elixir & Phoenix
Elixir
1,116
star
3

scrivener

Pagination for the Elixir ecosystem
Elixir
553
star
4

pioneer

Integration Testing
CoffeeScript
527
star
5

stickymojo

A contained fixed positioned sticky sidebar jQuery plugin.
JavaScript
401
star
6

json-type-validation

TypeScript JSON type validation
TypeScript
156
star
7

sass2stylus

Kewl
JavaScript
79
star
8

social-builder

HTML
57
star
9

psd-lint

Clean up your PSDs.
CoffeeScript
38
star
10

pullr

JavaScript
33
star
11

envforcer

Enforce environment variable requirements.
Ruby
30
star
12

capybara-ui

Roles and page objects for Capybara integration testing - Formerly called Dill
Ruby
29
star
13

EPFImporter

A Python CLI tool for importing Apple Enterprise Partner Feed data, available to EPF partners, into a relational database.
Python
29
star
14

backbone.typeahead

Integrates typeahead into backbone collections. Inspired by twitter typeahead.
CoffeeScript
21
star
15

bricks

Ruby
20
star
16

mo

CSS
10
star
17

helios2

Helios 2.0
Elixir
9
star
18

situation-room

Like pingdom, but with more cachet
JavaScript
9
star
19

foodtrucks_bot

Slack bot for checking Foodtrucks in KP
Elixir
8
star
20

backbone.route-helper

Helper for your backbone routes.
CoffeeScript
8
star
21

PrismatestJS

Decouple your front-end application tests from the view implementation and make them easier to read and write.
TypeScript
8
star
22

mojo-images

Packer machine image templates for VMWare, VirtualBox, AWS, and Digital Ocean.
Shell
7
star
23

feedbag

TV first, multiuser Github events dashboard for organizations and teams.
CSS
7
star
24

pioneer-todo-mvc

Pioneer tests todo mvc
JavaScript
7
star
25

mojotech-ui

A set of styled React components for MojoTech.com user interfaces.
TypeScript
7
star
26

pivotal-swimlanes

Swimlanes for Pivotal Tracker Sprints
JavaScript
6
star
27

trani.js

Awesome Jquery Interactive Transcript Plugin for Vimeo
JavaScript
5
star
28

backbone.routerFilters

JavaScript
5
star
29

busbus

Like a bus, but more bus.
Objective-C
5
star
30

blimey_brackets

Magic Sublime Text bracket closing detector.
Python
4
star
31

entwine

Immutable dependency injection in Node.js
JavaScript
4
star
32

rubo_claus

Ruby method pattern matching inspired by functional programming
Ruby
4
star
33

angular-ui-instagram

Angular UI Instagram Directive
CoffeeScript
4
star
34

vire

Vire.js is a jQuery plugin that extends the froogaloop library for the Vimeo player. It allows you to execute code at a given point in time in a video.
JavaScript
4
star
35

pt-estimator

Pivotal Tracker pointing/estimating tool
TypeScript
4
star
36

grunt-pioneer

Grunt runner for pioneer.
CoffeeScript
4
star
37

modernator-haskell

An API server for hosting Reddit AMA style Q&A sessions.
Haskell
4
star
38

semaphorestatus

semaphorestatus
JavaScript
3
star
39

modernator

Moderate.
Clojure
3
star
40

modernator-client-react

A small client for mojotech/modernator-haskell
JavaScript
3
star
41

local

Configure your local dev environment. Boring.
Shell
3
star
42

docker-images

Images of Dockers.
Shell
3
star
43

marionette-ui

UI Toolkit built on Marionette
CSS
3
star
44

pioneer.marionette

Make testing marionette apps with pioneer a pleasant experience
CoffeeScript
3
star
45

modash

Useful lodash mixins
CoffeeScript
3
star
46

habit-api

Habit App
Ruby
3
star
47

lunchbot

Elixir
2
star
48

guestbook

Guestbook app
JavaScript
2
star
49

stylus-redesign

The workgroup for Stylus' homepage redesign
2
star
50

heroku-deploy

Ruby
2
star
51

nearest

A node utility for finding the nearest x minutes for a given time
CoffeeScript
2
star
52

covenant

Ruby
2
star
53

random-run-flutter

Random Run revived with Flutter
Dart
2
star
54

mofun

Like regular fun, but mo'.
JavaScript
1
star
55

helios2-elixir

Helios 2 but... in Elixir
JavaScript
1
star
56

rails-json-errors

Easily parse and handle json error packets from a Rails API
CoffeeScript
1
star
57

rubocop-privacy

Vertical alignment code style checking for ruby files
Ruby
1
star
58

standauf

HTML
1
star
59

docbert

Literate features.
Ruby
1
star
60

haml-coffee-brunch

Brunch plugin to compile haml-coffee templates
JavaScript
1
star
61

mocomments

More comments. Please.
CoffeeScript
1
star
62

pivotal-watch

Npm command line tool for watching a pivotal tracker project
JavaScript
1
star
63

node-selenium-webdriver

Import of the selenium-webdriver for node.js.
JavaScript
1
star
64

mojo-ghost-s3-adapter

Ghost blog AWS S3 storage adapter
JavaScript
1
star
65

rails_db_protect

Protect your production database from destructive rake tasks
Ruby
1
star
66

pioneer-www

JavaScript
1
star
67

batchelor

Override find_in_batches and find_each in ActiveRecord decendant classes that do not have an integer primary key as these will not work when using PostgreSQL.
1
star
68

mojotech-design

MojoTech, but make it design
TypeScript
1
star
69

mojotech-intern-docs

MojoTech Internship Documentation
JavaScript
1
star
70

next-props-wiping-bug-demo

Demonstrating a bug with NextJS wiping props
TypeScript
1
star
71

hive

A documentation-driven development revolution
Ruby
1
star