Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use vogels js to implement pagination

I am implementing a website with a dynamodb + nodejs backend. I use Vogels.js in server side to query dynamodb and show results on a webpage. Because my query returns a lot of results, I would like to return only N (such as 5) results back to a user initially, and return the next N results when the user asks for more.

Is there a way I can run two vogels queries with the second query starts from the place where the first query left off ? Thanks.

like image 651
Chuang Liu Avatar asked Dec 19 '22 21:12

Chuang Liu


1 Answers

Yes, vogels fully supports pagination on both query and scan operations.

For example:

var Tweet = vogels.define('tweet', {
  hashKey  : 'UserId',
  rangeKey : 'PublishedDateTime',
  schema : {
    UserId            : Joi.string(),
    PublishedDateTime : Joi.date().default(Date.now),
    content           : Joi.string()
  }
});

// Fetch the 5 most recent tweets from user with id 555:
Tweet.query(555).limit(5).descending().exec(function (err, data) {
  var paginationKey = data.LastEvaluatedKey;

  // Fetch the next page of 5 tweets
  Tweet.query(555).limit(5).descending().startKey(paginationKey).exec()
});
like image 148
Ryan Fitzgerald Avatar answered Mar 15 '23 12:03

Ryan Fitzgerald