jsonamazon-dynamodbaws-sdk-js-v3

Insert Standard JSON into DynamoDB aws-sdk js v3


I am trying to register Standard JSON in DynamoDB using aws-sdk version 3 for JS in the thumbnail field.

In version 2 it was allowed using the put method, in the version 3 now are commands but I not see an equivalent command.

My code:

const { DynamoDBClient, PutItemCommand } = require('@aws-sdk/client-dynamodb');

const DynamoDBDocClient = new DynamoDBClient({ region: "us-east-1"});
const item = {
    PK: "My_pk",
    SK: "My_sk",
    username: "My_username",
    thumbnail: {
        id: "My_id",
        width: "My_width",
        height: "My_height"
    }
}

const params = {
    Item: item,
    TableName: "My_table"
};

await DynamoDBDocClient.send(new PutItemCommand(params));

The error:

ERROR   TypeError: Cannot read property '0' of undefined
at Object.visit (/var/task/node_modules/@aws-sdk/client-dynamodb/dist/cjs/models/models_0.js:1101:40)
at serializeAws_json1_0AttributeValue (/var/task/node_modules/@aws-sdk/client-dynamodb/dist/cjs/protocols/Aws_json1_0.js:4612:38)
at /var/task/node_modules/@aws-sdk/client-dynamodb/dist/cjs/protocols/Aws_json1_0.js:5514:20
at Array.reduce (<anonymous>)

I can only register the item its use marshall (from @aws-sdk/util-dynamodb) but it registers as DynamoDB JSON:

enter image description here


Solution

  • In v3 you have to use the @aws-sdk/lib-dynamodb library to get similar functionality to the DynamoDB.DocumentClient in the old SDK. It works a bit differently, but it should do what you want.