Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Can I Store the array of objects in DynamoDB

I know how to insert items into DynamoDB when it was in the below type

eg: { "id" : "10021", "AppName" "testingApp" }

But I want to insert array of objects like in the below example:

eg:

{
"testArray": [
{ "id" : "testID",
  "applicationName" : "Myapp"
},
 {
 "id" : "testID1",
  "applicationName" : "Myapp123"
} ]}

If Dynamo DB cannot store above type of JSON data, then can you please suggest which DB would be better to store this type of input..?

like image 359
devguy Avatar asked Nov 17 '22 08:11

devguy


1 Answers

DynamoDB can store JSON data , also the data format you wish to insert. There are two methods which can be used to format the data while inserting into DynamoDB and also while retrieving the DB data into desired JSON method. https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/Converter.html These methods are : input and output.

I have shared two sample snippets of AWS lambda code to do so, hope this solves your problem.

With the following way you can convert JSON data into DynamoDB supported format and then insert it:

const AWS = require('aws-sdk');
//setting the region
AWS.config.update({region: 'ap-south-1'});

// Create the DynamoDB service object
const ddb = new AWS.DynamoDB.DocumentClient();


const getParams = (updatedTasks)=>{
const params = {
  TableName: 'todo-application',
  Key: {
    "userID" : "[email protected]",
  },
  UpdateExpression: "set tasks = :updatedTasks",
   ExpressionAttributeValues:{
        ":updatedTasks":updatedTasks
    },
    ReturnValues:"UPDATED_NEW"
};
return params;
}

const dbQuery =(newTask)=>{
    let updatedTask = AWS.DynamoDB.Converter.input(newTask, true);
    console.log('updated task formatted ',updatedTask);
 let params = getParams(updatedTask);
  return new Promise((resolve,reject) =>{
  ddb.update(params, function(err, data) {
  if (err) {
    console.log("Error", err);
    reject(err);
  } else {
    console.log("Success", data);
    resolve(data.Item)
  }
});
});
}


exports.updateTodoJobsOfAUser = async (event) => {
    const insertObject = [
    {
    statement:'learn Dynamo DB', id: 23
    },
    {
    statement:'Learn Serverless Architecture',id:24 
    }]
    const data = await dbQuery(insertObject);
    const response = {
        statusCode: 200,
        body: JSON.stringify(data)
    };
    return response;
};

With following way you can convert DynamoDB data into JSON format :

const dbQuery =()=>{
  return new Promise((resolve,reject) =>{
  ddb.get(params, function(err, data) {
  if (err) {
    console.log("Error", err);
    reject(err);
  } else {
    console.log("Success", data);
    resolve(data.Item)
  }
});
});
}


exports.getAllTodoJobsOfAUser = async (event) => {
    const data = await dbQuery();
    const formattedData =  AWS.DynamoDB.Converter.output(data, true);
    const response = {
        statusCode: 200,
        body: JSON.stringify(formattedData)
    };
    return response;
};

Happy to help

like image 77
Anand Ujjwal Avatar answered Nov 23 '22 23:11

Anand Ujjwal