Michael Litvin
Michael Litvin

Reputation: 4136

Amplify Gen2 + React: How to query a DB sorted by a key with pagination?

I have this data model:

const schema = a.schema({
  Messages: a
    .model({
      Timestamp: a.string().required(),
      From: a.string().required(),
      To: a.string().required(),
      Body: a.string().required(),
    ])
});

I want to query it and display the latest 10 messages, and paginate to display older messages.

How can I achieve that?

I tried adding:

    .model({
...
      Dummy: a.integer().default(0),
    }).secondaryIndexes(index => [
      index('Dummy').sortKeys(['Timestamp']).queryField('listByTimestamp')
    ])

But then listByTimestamp() doesn't have a limit argument as in simple list().

Upvotes: 1

Views: 68

Answers (1)

Evgeny Urubkov
Evgeny Urubkov

Reputation: 151

Looks like it has to come as options in the second argument when using the listBy* queries.

E.g.

client.model.Messages.listByTimestamp({
  // fields you need
}, {
  limit: 10,
  nextToken: nextToken,
  sortDirection: "DESC"
});

Upvotes: 0

Related Questions