GraphQL pagination returning same results from dynamoDB

0

I'm pagianting list of data from dynamoDB table but it is returning same object every time:

Example : Dynamdb table:

id=1 title=hello

id=2 title=good

id=3 title=okay

My pagination limit is 3:

so it's will give item sequence like:

id=1 title=hello

id=2 title=good

id=3 title=okay

When I'm creating 3 more new item in table and querying list of items, instead of fetching the new item first the items are in between the previous list.

If your answer is write a smart algorithm then fetch data my app is new app there is nothing to do analytics, better to write some filter that it will show new items to user. If I will add a filter as createdDate then the possibility is that the loop of data will end because of less post and users. So if you know how to write a perfect filter of new app/startup then please suggest me.

1 Antwort
0

You can use the following code to achieve what you need:

import boto3

# get a list of items from dynamodb using pagination, default page size is 25 return all items
def get_items_from_dynamo_db(table_name, page_size=25, dynamodb=None):
    if not dynamodb:
        dynamodb = boto3.resource("dynamodb")
    table = dynamodb.Table(table_name)
    response = table.scan(Limit=page_size)
    items = response["Items"]
    while "LastEvaluatedKey" in response:
        response = table.scan(ExclusiveStartKey=response["LastEvaluatedKey"], Limit=page_size)
        items.extend(response["Items"])
    return items

For your reference, this code was generated by CodeWhisperer using the following 2 lines:

import boto3

# get a list of items from dynamodb using pagination, default page size is 25 return all items
AWS
EXPERTE
beantwortet vor 7 Monaten

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen