Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DynamoDB putitem in NodeJs - arrays of objects

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": ???         }     }); } 
like image 595
Erik Honn Avatar asked Oct 26 '15 09:10

Erik Honn


People also ask

Can we store array in DynamoDB?

DynamoDB can store JSON data , also the data format you wish to insert.

What is PutItem in DynamoDB?

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.

Does PutItem overwrite?

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.


1 Answers

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 }); 
like image 68
Vsevolod Goloviznin Avatar answered Sep 18 '22 03:09

Vsevolod Goloviznin