2

So I wanted to fetch the last item/row of my dynamodb table but i am not finding resources. My primary key is id having series of incremented numbers such as 1,2,3... for each row respectively.

This is my function.

async function readMessage(){
   const params = {
        TableName: table,
    };
    return dynamo.getItem(params).promise();
}

I am not sure as to what i should be adding in my params.

md shoaib
  • 125
  • 1
  • 10

3 Answers3

3

DynamoDB has two types of primary keys:

  • Partition key – A simple primary key, composed of one attribute known as the partition key.

  • Partition key and sort key – Referred to as a composite primary key, this type of key is composed of two attributes. The first attribute is the partition key, and the second attribute is the sort key.

When fetching an item by partition key, you need to specify the exact partition key. You cannot fetch the max/min partition key.

Instead, you may want to create a sort key with a timestamp (or the ID if it's a sequential number) and use the sort key to fetch the last item.

Check out the AWS docs on Choosing the Right Partition Key for more info.

Seth Geoghegan
  • 5,372
  • 2
  • 8
  • 23
2

The proper way to design a table in DynamoDB is based on its expected access patterns; if this is something you need perhaps you should consider using this id as Sort Key instead of Primary Key and then query the table in descending order while also limiting the amount of items to 1.

If instead you don't want to change the schema of your items and you don't care about making at least two operations to do this you have two, not optimal options:

  • If none of your items ever gets deleted, just make a count first and use that information to know what's the latest item that was written.

  • Alternatively, if you could consider keeping a "special" record in your DynamoDB table that is basically a count that gets always incremented/written when one of your "other" items gets written. Upon retrieval you first retrieve the value of this special record and use this info to retrieve the actual one.

Andre.IDK
  • 1,951
  • 13
  • 17
1

The combination of the partition key and sort key, makes the primary key of your item in the dynamoDB, so their combination must be unique, otherwise the item will be overwritten.

In almost all my use-cases, I select the primary key as an object attribute, like the brand, an email or a class and then, for the sort key I select the TimeStamp. So in this way, you always know the partition key, we need it to retrieve the values and then you can query your dynamoDB by making some filters by the sort key. For more extensive examples using Python, check the AWS page: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GettingStarted.Python.04.html, where it shows, how you can query your DynamoDB items. There is also other ways to define the keys in your Dynamo and for that I advise you to check https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/bp-sort-keys.html

João Ramos
  • 75
  • 1
  • 6