drewolson / scrivener

Pagination for the Elixir ecosystem

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to use :options

ah0y opened this issue · comments

Hey I know in 2.20 :options were introduced, but I was just wondering how I would go about actually using them? I want to limit the total number of pages returned for queries that can return millions of results. I've tried looking at the hexdocs but still can't figure it out.

@ah0y the :options key is intended to be used for libraries that implement Scrivener's Paginator protocol. In your case, you should use Ecto's built in limit to limit the results of your query before paginating it.

Sorry I should've mentioned this in my initial issue but I've already tried using something like this

query_params = from b in Book, where: ilike(b.summary, "some summary"), limit: 100

page = Metro.Repo.paginate(query_params)

with query_params returning an Ecto.Query like this:

#Ecto.Query<from b0 in Metro.Location.Book, where: ilike(b0.summary, "some summary"), limit: 100>

but when I use paginate to actually run the query, this is what is actually run against my database:

SELECT b0."isbn", b0."image", b0."title", b0."pages", b0."summary", b0."year", b0."author_id", b0."inserted_at", b0."updated_at" FROM "books" AS b0 WHERE (b0."summary" ILIKE 'some summary') LIMIT $1 OFFSET $2 [10, 0]

And my results for page still look like this (all 1 million entries have a summary with text "some summary")

entries: [...]
 page_number: 1,
 page_size: 10,
 total_entries: 1000000,
 total_pages: 100000
]

Is there something wrong with my Ecto.Query?