AWS Developer Tools Blog
Announcing the Amazon DynamoDB Document Client in the AWS SDK for JavaScript
Version 2.2.0
of the AWS SDK for JavaScript introduces support for the document client abstraction in the AWS.DynamoDB
namespace. The document client abstraction makes it easier to read and write data to Amazon DynamoDB with the AWS SDK for JavaScript. Now you can use native JavaScript objects without annotating them as AttributeValue
types.
This article describes how to use the document client abstraction to make requests to Amazon DynamoDB.
Making Requests with the Document Client
The following example shows a PutItem
request to Amazon DynamoDB with the document client. Note that you can use native JavaScript objects without annotating them as AttributeValue
types. The document client annotates the JavaScript object that you provide as input with AttributeValue
types before making a request to DynamoDB.
For a list of supported API operations, you can check out the API documentation.
Example
var docClient = new AWS.DynamoDB.DocumentClient({region: 'us-west-2'});
var params = {
Item: {
hashkey: 'key',
boolAttr: true,
listAttr: [1, 'baz', true]
mapAttr: {
foo: 'bar'
}
},
TableName: 'table'
};
docClient.put(params, function(err, data){
if (err) console.log(err);
else console.log(data);
});
Support for Sets
The AWS.DynamoDB.DocumentClient.createSet()
is a convenience method for creating a set. This method accepts a JavaScript array and a map of options. The type of set is inferred from the type of the first element in the list. Amazon DynamoDB currently supports three types of sets: string sets, number sets, and binary sets.
Example
var docClient = new AWS.DynamoDB.DocumentClient({region: 'us-west-2'});
var params = {
Item: {
hashkey: 'key',
stringSet: docClient.createSet(['a', 'b']);
numberSet: docClient.createSet([1, 2]);
binarySet: docClient.createSet([new Buffer(5), new Uint8Array(5)]);
},
TableName: 'table'
};
docClient.put(params, function(err, data){
if (err) console.log(err);
else console.log(data);
});
You can also validate the uniformity of the supplied list by setting validate: true
in the options passed in to the createSet()
method.
// This is a valid string set
var validSet = docClient.createSet(['a', 'b'], {validate: true});
// This is an invalid number set
var invalidSet = docClient.createSet([1, 'b'], {validate: true});
Using Response Data from the Document Client
The document client also unmarshalls response data annotated with AttributeValue
types from DynamoDB to native JavaScript objects that can be easily used with other JavaScript code.
Example
var docClient = new AWS.DynamoDB.DocumentClient({region: 'us-west-2'});
var params = {
Key: {
hashkey: 'key',
},
TableName: 'table'
};
docClient.get(params, function(err, data){
if (err) console.log(err);
else console.log(data);
/**
* {
* Item: {
* hashkey: 'key'
* boolAttr: true,
* listAttr: [1, 'baz', true]
* mapAttr: {
* foo: 'bar'
* }
* }
* }
**/
});
For more information about the document client and its supported operations, see the API documentation.
We hope this simplifies the development of applications with the AWS SDK for JavaScript and Amazon DynamoDB. We’d love to hear what you think about the document client abstraction, so leave us a comment here, or on GitHub, or tweet about it @awsforjs.