Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return the inserted item in dynamoDB

I am using nodeJS sdk to put the item to dynamoDB, The item is:

{    "eventId": date + '-' + eventName + '-' + eventPurpose,    "eventName": eventName,    "eventPurpose": eventPurpose,    "eventDates": eventDates,    "attendees": attendees  } 

The present code for the putting the item in dynamoDB:

  const params = {     TableName: "event",     Item: {         "eventId": date + '-' + eventName + '-' + eventPurpose,         "eventName": eventName,         "eventPurpose": eventPurpose,         "eventDates": eventDates,         "attendees": attendees     },     ReturnValues: "ALL_OLD"   };    dynamo.put(params, (err, data) => {     console.log("coming here");     if (err) {       console.log("error : " + JSON.stringify(err));     }     console.log("data" + JSON.stringify(data));     cb(null, data);   }); 

The insertion happens correctly and the return value is an empty object.

I would like to return the inserted item. I found this doc. But this returns only in case of updating the old value. I could not find any other useful info other than this.

Is there any work around or we simply need to query using get method with the primary key?

like image 861
Lakshman Diwaakar Avatar asked Sep 12 '16 13:09

Lakshman Diwaakar


People also ask

How do I get items from DynamoDB?

To read an item from a DynamoDB table, use the GetItem operation. You must provide the name of the table, along with the primary key of the item you want. The following AWS CLI example shows how to read an item from the ProductCatalog table. With GetItem , you must specify the entire primary key, not just part of it.

Does DynamoDB put item overwrite?

With the PutItem call, you provide an entire Item to be placed into your DynamoDB table. This action will create a new Item if no Item with the given primary key exists, or it will overwrite an existing Item if an Item with that primary key already exists.

Which statement retrieves an item from the music collection table in DynamoDB?

In SQL, you use the SELECT statement to retrieve data from a table. You can request one or more columns in the result (or all of them, if you use the * operator). The WHERE clause determines which rows to return. The following is a SELECT statement to retrieve a single row from the Music table.


2 Answers

The link you posted is, sadly, the only real answer at this time (API Version 2012-08-10). PutItem may return items just before they were updated or none at all.

The ReturnValues parameter is used by several DynamoDB operations; however, PutItem does not recognize any values other than NONE or ALL_OLD.

In short, the only reliable way to retrieve your inserted object is to GetItem, just as you surmised.

like image 58
Diego Ferri Avatar answered Sep 21 '22 20:09

Diego Ferri


Just pass the params.Item in the callback :

 dynamo.put(params, (err, data) => {         if (err) {           cb(err);         }         cb(null, params.Item);       }); 

Pass the err in the callback too ;)

like image 30
Sigma Avatar answered Sep 20 '22 20:09

Sigma