Nefcairon
Pagination: which library? (Phoenix 1.4/Ecto 3)
Hello,
Still new to Phoenix I wonder what’s the best way to do pagination (or even better infinite scrolling).
Can you recommend me a library that works with Phoenix 1.4-RC3 (which uses Ecto 3 if I am correct) and is easy enough to use for a beginner? Is there a de-facto standard library in the elixir world?
I hope you do not mind such questions.
Thanks!
Most Liked
Eiji
@Nefcairon My favorite way is to use Repo.stream/2 in Websocket connection (see Channels guide). Since you decide to use WebSocket you will have 2-way communication which means you can query anything and return results one by one in really small and fast messages whenever you want to. This solution is also really well scalable. No matter if you have 100 or 100_000_000 entries returned from query you still only fetch and sends exactly same count of entries at a time (just only number of server messages is changed).
If you want to paginate query you can use ecto’s Query API, but before you decide to use Ecto.Query.limit/3 make sure you have read why it’s considered as bad practice in some cases:
For more information please see What is the best approach for fetching large amount of records from postgresql with ecto topic.
LostKobrakai
For pagination where I need to show page numbers I usually use scrivener_ecto. For infinite scrolling I use GitHub - duffelhq/paginator: Cursor-based pagination for Elixir Ecto · GitHub.
Eiji
Number does not matter since you are using Stream.
Here we have few assumptions:
WebSocket(or other bi-directional communication way) is required as well asPhoenixchannel API (or equivalent)- Since we are using
Channelwe can assign any data for eachWebSocketconnection - Since we are using Ecto.Repo.stream/2 we have only one database row at a time in memory.
- Each
WebSocketconnection have it’s own process
From this we can create scenario like:
-
Client request
GET /posts -
Server sends basic
HTMLDOM with aJavaScriptcode -
/postsis list so we are automatically appendingtableelement -
Client is requesting all
Postfrom database -
Server is generating per-client query id (for simplicity let’s say it’s counter) - example return:
{"id": "query1", "status": "OK"}(which means that query with id1is valid and therefore will be send). -
Client is setting to
tablepropertyidwith valuequery1and propertydata-modelwith valuepost -
Server is calling Ecto.Repo.stream/2 in background and in Stream.each/2 it’s sending data like:
{data: {…}, "id": "query1", "type": "append"}.
Note: You can optionally use Stream.chunk_every/2 before Stream.each/2 if you want to send more than one database row at a time. -
Client receives such response and it’s creating
trDOM element for it with id:query1-#{resonse.data.id} -
If there is created, deleted or updated any
Postthen server could send something like:{data: {…}, "model": "post", type: "delete|update"} -
Client accepts such event and it’s looking for
table[data-model="post"] > tbody > tr[id^="query"][id$="-#{response.data.id}"].
With that we are sending always same amount of data for each client request at a time. Later it’s possible to limit number of requests per client (using Channel assigns).
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









