How to return the inserted item in dynamoDB How to return the inserted item in dynamoDB node.js node.js

How to return the inserted item in dynamoDB


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.


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 ;)


You can use UpdateItem instead of PutItem. UpdateItem creates a new item if it doesn't exist already, plus you can set ReturnValues to ALL_NEW to return the whole item as it appears in the database after the operation:

const params = {    TableName: "event",    Key: {        "eventId": date + '-' + eventName + '-' + eventPurpose    },    UpdateExpression: "SET eventName = :eventName, eventPurpose = :eventPurpose, eventDates = :eventDates, attendees = :attendees",    ExpressionAttributeValues: {        ":eventName": eventName,        ":eventPurpose": eventPurpose,        ":eventDates": eventDates,        ":attendees": attendees    },    ReturnValues: "ALL_NEW"};

Using GetItem right after PutItem is not a good idea unless you are using strong consistency in DynamodDB. If you have eventual consistency (default) then GetItem right after PutItem can still return empty.