How Do I Get the Latest 10 Entries in a model? Also, is it possible to paginate?

I’m fairly new to Contentful. I am utilizing the JS SDK to link my content to my website. I want to ask though, is it possible to get the last 10 created entries for a model, for example? And is it possible to paginate and get the next 10 starting at the 30th entry, for example?

Thanks.

Hi @pluy ,

It should be possible to get the entries as you mention.
For the ordering by created entries, see the Ordering as explained on our documentation:
https://www.contentful.com/developers/docs/references/content-delivery-api/#/reference/search-parameters/order

Here a snippet to implement pagination:

  const client = contentful.createClient({
    accessToken: 'your-token-here'
  })

  let varSpace, varEnvironment
  await client
    .getSpace('your-space-id')
    .then(space => (varSpace = space))
  await varSpace
    .getEnvironment('your-environment-id')
    .then(environment => (varEnvironment = environment))

  let shouldLoop = true
  let varSkip = 0
  let varLimit = 10
  let entriesResult = []

  do {
    await varEnvironment
      .getEntries({
        content_type: 'your-content-type-id',
       order: 'sys.createdAt',
        skip: varSkip,
        limit: varLimit
      })
      .then(entries => (varEntries = entries))

    if (varEntries.items.length > 0) {
      entriesResult = entriesResult.concat(varEntries.items)
      varSkip += varLimit
    } else {
      shouldLoop = false
    }
  } while (shouldLoop)

return entriesResult

Hope it can help :clap: