Append a new object to a JSON Array in DynamoDB using NodeJS

JavascriptJsonnode.jsAmazon Web-ServicesAmazon Dynamodb

Javascript Problem Overview


I am using a JSON structure to store details of a person. Basically name, phone, registeredTimestamp etc., will be other attributes, and Primary key will be the email address.

markedLocations, visitedLocations, searchHistory and recommendation are the lists or Map as AWS calls it. It will contain many JSON objects.

JSON Structure-

{
	"name":"Mama Miya",
	"phone":"9383883223",
	"registeredTimestamp":"some-time",
	"lastActiveTimestamp":"some-time",
	"signinType":"facebook",
	"additionalSigninData":{
		"key1":"value1",
		"key2":"value2"
	},
	"markedLocations":[
		{
			"latitude":"77.33",
			"longitude":"12.33",
			"name":"Home",
			"description":"my new home",
			"placeid":"55334433",
			"image":"url/abc.png"
		}
	]
}

From the app, if the user visits a new location, I need to add a new JSON Object to the markedLocations. So the markedLocations should look something like-

"markedLocations":[
	{
		"latitude":"77.33",
		"longitude":"12.33",
		"name":"Home",
		"description":"my new home",
		"placeid":"55334433",
		"image":"url/abc.png"
	},{
		"latitude":"22.11",
		"longitude":"22.55",
		"name":"Ocean",
		"description":"Ocean",
		"placeid":"32423423",
		"image":"url/icean.png"
	}
]

Code/Schema-

var item = {
        'email': {'S': req.body.email},
        'phone': {'S': req.body.phone},
        'registeredTimestamp': {'S': req.body.registeredTimestamp},
        'lastActiveTimestamp': {'S': req.body.lastActiveTimestamp},
        'signinType': {'S': req.body.signinType},
        'version': {'S': req.body.version},
        'markedLocations': {'L': req.body.markedLocations}
    };

I checked -

  • In DynamoDB how do I append an element to a list field using Java link

  • updating a JSON array in AWS dynamoDB link

  • Updating a Set in Dynamo db using Node Js link

  • how to update item in dynamoDB using nodejs? link

  • And checked AWS docs for UpdateItem API. I didn't find anything relevant to my problem/doubt.

Please can you tell me if there is a way to append the JSON array with a new JSON object in NodeJS? I am not able proceed on how to implement the same.

OR

Should I create separate table for each Array and have email as primary key and timestamp as sort key and proceed?

PS: I am new to DynamoDB, I've worked with MongoDB earlier and there are ways to work with JSON arrays and objects there. Unable to find a way here.

Javascript Solutions


Solution 1 - Javascript

Use list_append() and if_not_exists() together in an UpdateExpression to append to a potentially non-existent list column:

var AWS = require('aws-sdk')
var DB = new AWS.DynamoDB.DocumentClient()

function appendMarkedLocation (personId, location) {
  return DB.update({
    TableName: 'people',
    Key: { id: personId },
    ReturnValues: 'ALL_NEW',
    UpdateExpression: 'set #markedLocations = list_append(if_not_exists(#markedLocations, :empty_list), :location)',
    ExpressionAttributeNames: {
      '#markedLocations': 'markedLocations'
    },
    ExpressionAttributeValues: {
      ':location': [location],
      ':empty_list': []
    }
  }).promise()
}

appendMarkedLocation('somePeronId', {
  latitude: '22.11',
  longitude: '22.55',
  name: 'Ocean',
  description: 'Ocean',
  placeid: '32423423',
  image: 'url/icean.png'
}).then(console.log)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbozzmobView Question on Stackoverflow
Solution 1 - JavascriptidbeholdView Answer on Stackoverflow