How to return an inserted element to dynamoDB

I use nodeJS sdk to put an element in dynamoDB, element:

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

Real code to put an element 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 is correct, and the return value is an empty object.

I would like to return the inserted element. I found this doc . But this is only returned if the old value is updated. I could not find other useful information besides this.

Is there any work, or do we just need a request using the get method with the primary key?

+5
source share
3 answers

The link you posted is unfortunately the only real answer this time (API version 2012-08-10). PutItem can return items just before they are updated or nothing at all.

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

In short, the only reliable way to get your inserted object is GetItem , you guessed it.

+5
source

Just pass params.Item in the callback:

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

Pass err in the callback too;)

+6
source

Please note that this is the element you are inserting that you already have access to:

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

You can simply change your code to this, and then you will have an element already inserted in the item variable:

 var item = { "eventId": date + '-' + eventName + '-' + eventPurpose, "eventName": eventName, "eventPurpose": eventPurpose, "eventDates": eventDates, "attendees": attendees }; const params = { TableName: "event", Item: item, ReturnValues: "ALL_OLD" }; 

You seem to be confused about what you are inserting because you start your question by showing an object that you say you are inserting, but the code you entered inserts a slightly different object.

+2
source

All Articles