title: Pagination in DynamoDB using Node.js?
body: <p>I've had a read through <a href="https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Query.Pagination.html" rel="noreferrer">AWS's docs around pagination</a>:</p>
  
  <p>As their docs specify:</p>
  
  <blockquote>
    <p>In a response, DynamoDB returns all the matching results within the scope of the Limit value. For example, if you issue a Query or a Scan request with a Limit value of 6 and without a filter expression, DynamoDB returns the first six items in the table that match the specified key conditions in the request (or just the first six items in the case of a Scan with no filter)</p>
  </blockquote>
  
  <p>Which means that given I have a table called <code>Questions</code> with an attribute called <code>difficulty</code>(that can take any numeric value ranging from <code>0</code> to <code>2</code>) I might end up with the following conundrum:</p>
  
  <ul>
  <li>A client makes a request, think <code>GET /questions?difficulty=0&amp;limit=3</code> </li>
  <li>I forward that <code>3</code> to the DynamoDB query, which might return <code>0</code> items as the first 3 in the collection might not be of <code>difficulty == 0</code></li>
  <li>I then have to perform a new query to fetch more <code>questions</code> that match that criteria without knowing I might return duplicates</li>
  </ul>
  
  <p>How can I then paginate based on a query correctly? Something where I'll get as many results as I asked for whilst having the correct offset</p>