1

I'm migrating a project with the V3 sdk of AWS for DynamoDB on NodeJS.

In V2 there was no problem retrieving data during the PUT operation with the DocumentClient API. The method returns the item inserted without any problem.

I'm not sure why the V3 putCommand doesn't do the same. The operation happens as expected and I can see the item inserted in my DB but I don't know if it is possible to return the inserted item.

I've spent hours reading the aws docs but I can't find the solution. I have to say that the AWS docs are not pretty clear from my opinion

Here is an example of my code:

const { DynamoDBClient } = require("@aws-sdk/client-dynamodb");
const { DynamoDBDocumentClient, PutCommand } = require('@aws-sdk/lib-dynamodb');

async writeItemHelper(tableName, item) {
        const marshallOptions = {
            convertEmptyValues: true,
            convertClassInstanceToMap: true
        }

        this.rawClient = new DynamoDBClient({ region: process.env.AWS_REGION })
        this.client = DynamoDBDocumentClient.from(this.rawClient, { marshallOptions })

        try {
            const command = new PutCommand({ TableName: tableName, Item: item} )
            const cmdResult = await this.client.send(command)
            return cmdResult;
        } catch (err) {
            console.error(`DynDB::writeItem ${JSON.stringify(err)}`);
            throw {error: err};
        }

And here is the response:

{
  "$metadata": {
    "httpStatusCode": 200,
    "requestId": "698LOHMFMO7NHPC1TG7MLRVE7VVV4KQNSO5AEMVJF66Q9ASUAAJG",
    "attempts": 1,
    "totalRetryDelay": 0
  },
  "Attributes": undefined,
  "ConsumendCapacity": undefined,
  "ItemCollectionMetrics": undefined
}

As you can see the operation works as expected but there is no any info about the item inserted. Do you know if there is any config that I'm missing?

Thank you so much J.

3
  • 2
    You already have what you inserted so how about return item instead of cmdResult.
    – Asdfg
    Commented Sep 6, 2021 at 17:40
  • Yes, of course I can return the item that I've already insert in database. My first try was to keep the same logic and now just know if this is the only way and the cmdResult is just to know if the operation was OK or KO.
    – jfgDev
    Commented Sep 6, 2021 at 20:30
  • 2
    You can append cmdResult to item before returning. Not getting the item back in PutItem call actually saves you money.
    – Asdfg
    Commented Sep 6, 2021 at 20:49

0