I'm trying to set up a small api from AWS Lambda to DynamoDB and I am having trouble figuring out if and how I can write an array of objects into a key.
I have an object like
{ "teamName": "Team Awesome", "members": [ { "email": "[email protected]", "name": "Bob" }, { "email": "[email protected]", "name": "Alice" } ] }
The members array is giving me issues, in the docs it looks like it can be done considering the list types, but there is just no example how HOW to do it, and I am running out of ways to try it.
So is it possible to write something in this format at all and how do you in that case do it?
Example code - what do I put at ???
var AWS = require('aws-sdk'); var dynamodb = new AWS.DynamoDB(); exports.handler = function(event, context) { var tableName = "GDCCompetition"; var datetime = new Date().getTime().toString(); DynamoDB.putItem({ "TableName": tableName, "Item": { "datetime": { "N": datetime }, "teamName": { "S": event.teamName }, "members": ??? } }); }
DynamoDB can store JSON data , also the data format you wish to insert.
PDF. Creates a new item, or replaces an old item with a new item. If an item that has the same primary key as the new item already exists in the specified table, the new item completely replaces the existing item.
Previously, we used the PutItem operation to insert Items into our DynamoDB table. We saw that this operation completely overwrites any existing Item in the table.
The documentation is not really obvious, but there is a thing called DocClient
, you can pass a usual JS object to it and it will do all the parsing and transformation into AWS object (with all the types). You can use it like this:
var AWS = require("aws-sdk"); var DynamoDB = new AWS.DynamoDB.DocumentClient(); var params = { TableName: "MyTable", Item: { "teamName": "Team Awesome", "members": [ { "email": "[email protected]", "name": "Bob" }, { "email": "[email protected]", "name": "Alice" } ] } }; DynamoDB.put(params, function (err) { if (err) { return throw err; } //this is put });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With