DynamoDB putitem in NodeJs - arrays of objects
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;
}
});
Related videos on Youtube
Erik Honn
Updated on August 18, 2020Comments
-
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 almost 6 yearsHi Erik, what was
AttributeType
ofmembers
while creating table?
-
-
Erik Honn over 8 yearsThanks 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 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 over 8 yearsSounds good, but all I get is "Process exited before completing request" for some reason.
-
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 over 8 yearsIt'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 over 8 yearsMay be you could investigate what error you get?
console.log(err)
and see what it outputs -
Erik Honn over 8 yearsOh 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 over 8 yearsAwesome! 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 over 8 yearsHuh, 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 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 over 8 yearsYeah, 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 over 8 yearsAh, 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 about 7 yearsBut 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 over 6 yearsThis 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 almost 6 yearsThis never returns the inserted record it seems.
-
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 over 5 yearsThis solution is nice, I learn something new today. No need to pass the data type to params of dynamodb. Thanks for this solution!