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
1 Answer
Reset to default 7The 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
版权声明:本文标题:javascript - How store an object in Dynamodb? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744208489a2595306.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论