NestJS materials

Write an awesome doc for NestJS tips, tricks, notes, things which are not in the doc (or are but not really obvious) and experimented to figure them out and use them.

View on GitHub

Pagination

[!IMPORTANT]

Here I did not go into details about pagination in SQL and what kind of issues you need to be aware of in SQL since I have written an extensive comprehensible doc here for it.

Pagination from a user’s perspective

3 kinds of pagination from a technical point of view

  1. # Server-driven pagination (cursor-based pagination):
    • This gives our server more control and can be utilized where we are serving different clients and server is not up to the task of dealing with pagination on top of other tasks it has to complete.
    • In the example above the nextLink is URL to the next page,
      • It can contains query strings.
      • It can be the ID of next element, or offset.
    • Note: It is best to keep the cursor opaque:
      • Cursor’s internal representation or meaning is hidden and not meant to be understood or relied upon by clients..
      • Encode the cursor with a base64 algorithm.
    • No nextField means that we’ve reach the end of the road.
    • You can see how it is done in GraphQL here.
  2. Client-driven pagination (offset-based pagination): enables our client to have a finer grasp over what is being returned. Good for when we have a very tight requirements in our client app.
  3. Or we can use both.

Numbered pagination (OFFSET & LIMIT)

Filtering, sorting, and paginating:

  1. Server must evaluate the filtering first (so we have applied filters).
  2. We need to apply order bys specified by client.
    • Caution: Order by is supper expensive. So you might wanna consider to not implement it.
  3. We can skip part of it.
    • Caution: It’s easy to skipping an item.
    • Caution: It’s easy to display same data twice!
  4. Lastly we wanna return the page that user have asked for it.

I am not totally sold on the idea that these SQL/MongoDB queries but nonetheless I thought it would be really helpful:

// users
SELECT *
FROM users
WHERE status = 'active'
ORDER BY created_at DESC, username ASC
LIMIT 10
OFFSET 20;

# total
SELECT COUNT(*)
FROM users
WHERE status = 'active';
db.collection("users")
  .find({ status: "active" })
  .sort({ created_at: -1, username: 1 })
  .skip(20)
  .limit(10);
GET https://meta.discourse.org/latest.json?page=2

Page info:

Cursor-based pagination

https://www.reddit.com/
https://www.reddit.com/?count=25&after=t3_49i88b

Again read this doc for an in-depth explanation.

Relay pagination

For this you can learn more here.

Ref