DynamoDB putitem in NodeJs - arrays of objects

21,850

Solution 1

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
});

Solution 2

You could convert the object to DynamoDb record first

const AWS = require('aws-sdk');
var tableName = "GDCCompetition";
var datetime = new Date().getTime().toString();
const members = [
  {
    "email": "[email protected]",
    "name": "Bob"
  },
  {
    "email": "[email protected]",
    "name": "Alice"
  }
];
const marshalled = AWS.DynamoDB.Converter.marshall({ members });
const params = {
  "TableName": tableName,
  "Item": {
    "datetime": {
      "N": datetime
    },
    "teamName": {
      "S": event.teamName
    },
    "members": marshalled.members,
  },
}
AWS.DynamoDB.putItem(params, function (err) {
  if (err) {
    return throw err;
  }
});
Share:
21,850

Related videos on Youtube

Erik Honn
Author by

Erik Honn

Updated on August 18, 2020

Comments

  • Erik Honn
    Erik Honn over 3 years

    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": ???
            }
        });
    }
    
    • Nikhil Manapure
      Nikhil Manapure almost 6 years
      Hi Erik, what was AttributeType of members while creating table?
  • Erik Honn
    Erik Honn over 8 years
    Thanks for the fast answer, but I don't understand how I build that dynamically. I updated the question with my current test code.
  • Vsevolod Goloviznin
    Vsevolod Goloviznin over 8 years
    @ErikHonn with DocumentClient you don't need to build the object dynamically! You just pass your regular object into Item property and sdk will do the transformation for you.
  • Erik Honn
    Erik Honn over 8 years
    Sounds good, but all I get is "Process exited before completing request" for some reason.
  • Vsevolod Goloviznin
    Vsevolod Goloviznin over 8 years
    @ErikHonn hm, no idea why that's happening. May be you could try to put a simple object into DDB and see if that's working at all
  • Erik Honn
    Erik Honn over 8 years
    It's strange. Built as in my example above, with putItem, it works if I don't have members there. If I do I get errors depending on how I try to write it. I haven't gotten any request with DocumentClient and put to work so far, all of them exit.
  • Vsevolod Goloviznin
    Vsevolod Goloviznin over 8 years
    May be you could investigate what error you get? console.log(err)and see what it outputs
  • Erik Honn
    Erik Honn over 8 years
    Oh lord, I just did and I'm an idiot :) I have the table set up to expect datetime as a number (part of the primary key), and I am calling to string on it. Works like a charm when I removed that! Thanks!
  • Vsevolod Goloviznin
    Vsevolod Goloviznin over 8 years
    Awesome! And in fact that's strange that amazon doesn't have DocumentClient documentation found easily. Previously I had custom code to transform an object into aws representation
  • Erik Honn
    Erik Honn over 8 years
    Huh, spoke to soon. It works, it get's into dynamodb, but it still reports it as an error. Perhaps that´'s just an error using the test console, I'll have to try with real API calls.
  • Vsevolod Goloviznin
    Vsevolod Goloviznin over 8 years
    @ErikHonn hm, usually if it get's inside DDB, then everything is fine, though I haven't worked with test console
  • Erik Honn
    Erik Honn over 8 years
    Yeah, it's mostly a minor thing. I just don't want to get spammed by Amazon because it thinks 100% of my requests fail :P
  • Erik Honn
    Erik Honn over 8 years
    Ah, nothing more than that I didn't have a context.succeed call at the end to let Lambda know it was done. Now it works.
  • Amulya Kashyap
    Amulya Kashyap about 7 years
    But can we update any object(map) within that array(set) ? i cannot see any way to update object within array ? please if you find anyway post it. thanks
  • Bob Yuan
    Bob Yuan over 6 years
    This answer saves me from the "Invalid attribute value type" exception which confuse me for hours. I didn't find the document client can directly use an object, so I did the type notation with my self and then stuck with the confused error.
  • Priya
    Priya almost 6 years
    This never returns the inserted record it seems.
  • Vsevolod Goloviznin
    Vsevolod Goloviznin almost 6 years
    @Priya and it shouldn't, however as far as I remember, the inserted value is available as a second parameter to the callback
  • AllenC
    AllenC over 5 years
    This solution is nice, I learn something new today. No need to pass the data type to params of dynamodb. Thanks for this solution!