admin管理员组

文章数量:1400188

I understand that to put an item into a Dynamodb table it has to be in a structure like this but is there an option to be able to input a standard Javascript object without this special structure? If not, is there an existing function in the AWS SDK that would convert my Javascript object into one with this special structure for Dynamodb?

var params = {
  Item: {
   "AlbumTitle": {
     S: "Somewhat Famous"
    }, 
   "Artist": {
     S: "No One You Know"
    }, 
   "SongTitle": {
     S: "Call Me Today"
    }
  }, 
  ReturnConsumedCapacity: "TOTAL", 
  TableName: "Music"
 };

I understand that to put an item into a Dynamodb table it has to be in a structure like this but is there an option to be able to input a standard Javascript object without this special structure? If not, is there an existing function in the AWS SDK that would convert my Javascript object into one with this special structure for Dynamodb?

var params = {
  Item: {
   "AlbumTitle": {
     S: "Somewhat Famous"
    }, 
   "Artist": {
     S: "No One You Know"
    }, 
   "SongTitle": {
     S: "Call Me Today"
    }
  }, 
  ReturnConsumedCapacity: "TOTAL", 
  TableName: "Music"
 };
Share Improve this question edited Sep 24, 2019 at 21:25 Dharman 33.5k27 gold badges101 silver badges147 bronze badges asked Sep 20, 2019 at 22:38 Berry BlueBerry Blue 16.6k22 gold badges77 silver badges147 bronze badges 2
  • Checkout DocumentClient – Giorgi_Mdivani Commented Sep 20, 2019 at 23:25
  • Another option is that you put only the info in attributes that you need to look up the items or for use in secondary indexes. All the other data goes into a JSON attribute on the item. DynamoDB is way more efficient this way. – NoSQLKnowHow Commented Sep 21, 2019 at 3:31
Add a ment  | 

1 Answer 1

Reset to default 7

The AWS SDK does provide a few ways to do this.

The one I am familiar with is the AWS.DynamoDB.Converter. It can be used like so

const AWS = require("aws-sdk");

let record = {
   "AlbumTitle": "Somewhat Famous", 
   "Artist": "No One You Know", 
   "SongTitle": "Call Me Today"
}

// Your DynamoDB representation
let ddbRecord = AWS.DynamoDB.Converter.marshall(record)

/* ddbRecord is now
{ 
  AlbumTitle: { S: 'Somewhat Famous' },
  Artist: { S: 'No One You Know' },
  SongTitle: { S: 'Call Me Today' } 
}
*/

To inverse the operation you would use the unmarshall function. See https://docs.aws.amazon./AWSJavaScriptSDK/latest/AWS/DynamoDB/Converter.html


Alternatively there is a DocumentClient available but I havent used it. The documentation on it is quite good https://aws.amazon./blogs/developer/announcing-the-amazon-dynamodb-document-client-in-the-aws-sdk-for-javascript/

本文标签: javascriptHow store an object in DynamodbStack Overflow