The AWS SDK for JavaScript V3 API Reference Guide describes in detail all the API operations for the AWS SDK for JavaScript version 3 (V3).
DynamoDB document client
Basic usage of DynamoDB document client in v3
-
In v2, you can use the
AWS.DynamoDB.DocumentClient
class to call DynamoDB APIs with native JavaScript types like Array, Number, and Object. It thus simplifies working with items in Amazon DynamoDB by abstracting away the notion of attribute values. -
In v3, the equivalent
@aws-sdk/lib-dynamodb
client is available. It's similar to normal service clients from v3 SDK, with the difference that it takes a basic DynamoDB client in its constructor.
Example:
import { DynamoDBClient } from "@aws-sdk/client-dynamodb"; // ES6 import
// const { DynamoDBClient } = require("@aws-sdk/client-dynamodb"); // CommonJS import
import { DynamoDBDocumentClient, PutCommand } from "@aws-sdk/lib-dynamodb"; // ES6 import
// const { DynamoDBDocumentClient, PutCommand } = require("@aws-sdk/lib-dynamodb"); // CommonJS import
// Bare-bones DynamoDB Client
const client = new DynamoDBClient({});
// Bare-bones document client
const ddbDocClient = DynamoDBDocumentClient.from(client); // client is DynamoDB client
await ddbDocClient.send(
new PutCommand({
TableName,
Item: {
id: "1",
content: "content from DynamoDBDocumentClient",
},
})
);
Undefined
values in when
marshalling
-
In v2,
undefined
values in objects were automatically omitted during the marshalling process to DynamoDB. -
In v3, the default marshalling behavior in
@aws-sdk/lib-dynamodb
has changed: objects withundefined
values are no longer omitted. To align with v2's functionality, developers must explicitly set theremoveUndefinedValues
totrue
in themarshallOptions
of the DynamoDB Document Client.
Example:
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
import { DynamoDBDocumentClient, PutCommand } from "@aws-sdk/lib-dynamodb";
const client = new DynamoDBClient({});
// The DynamoDBDocumentClient is configured to handle undefined values properly
const ddbDocClient = DynamoDBDocumentClient.from(client, {
marshallOptions: {
removeUndefinedValues: true
}
});
await ddbDocClient.send(
new PutCommand({
TableName,
Item: {
id: "123",
content: undefined // This value will be automatically omitted.
array: [1, undefined], // The undefined value will be automatically omitted.
map: { key: undefined }, // The "key" will be automatically omitted.
set: new Set([1, undefined]), // The undefined value will be automatically omitted.
};
})
);
More examples and configurations are available in the
package
README