peopledatalabs / peopledatalabs-js

A universal JS client with TypeScript support for the People Data Labs API
https://docs.peopledatalabs.com/docs/javascript-sdk
MIT License
22 stars 9 forks source link

How to create pagination using `scroll_token`? #250

Closed alamenai closed 3 months ago

alamenai commented 3 months ago

Hi,

I just learned about scroll_token property however due to the lack of examples in the documentation, I would know how to use it in real world example.

I have a Next.js application where I would request (display) 10 (1-10) developers in the beginning and when the user click on show more button, I would display from 11-20 and so on. On each click, I display 10 developers.



// How to call this function each time and send scroll_token as a parameter
const fetchPersons = async (
  searchFilters: z.infer<typeof pdlPersonSearchSchema>
) => {
  const sqlQuery = buildQuery(searchFilters)

  const params = {
    searchQuery: sqlQuery,
    pretty: true,
    dataset: "all",
    size: 1,
  }

  try {
    const data = await PDLJSClient.person.search.sql(params)

    console.info(data.data)

    return data.data
  } catch (error) {
    if (error instanceof Error) {
      throw new Error(error.message)
    }
  }
}```
vvillait88 commented 3 months ago

@alamenai As part of your response you will receive a scroll_token which you can then pass back into your next request to the search API which will retrieve the next set of results. You can control the page size by adjusting your size param.

alamenai commented 3 months ago

Hi @vvillait88 , so what I understand, when I set the size to 10 and pass the scroll_token. Each time it will display the next 10 persons, right ?

vvillait88 commented 3 months ago

That is correct. Every call will have a new scroll token that you can pass into the next response which will get the next 10.