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

21,249

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)
Share:
21,249
bozzmob
Author by

bozzmob

Simple living, High Thinking. Tech Enthusiast. I'm high on Javascript! Simply love Mozilla and Android.

Updated on July 09, 2022

Comments

  • bozzmob
    bozzmob almost 2 years

    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.

  • bozzmob
    bozzmob over 7 years
    This is exactly something I was looking for. Will test it and update. Thanks a lot.
  • bozzmob
    bozzmob over 7 years
    Please can you tell me what Attribute type should I be using for each object in the array? Should it be a List?
  • idbehold
    idbehold over 7 years
    @bozzmob if you're using the DocumentClient like I am in my answer then you shouldn't need to care about the DyanmoDB internal types at all. The DocumentClient would cast each item in the markedLocations List to a Map type internally. So markedLocations would end up being a List of Maps.
  • Phil D.
    Phil D. over 3 years
    I didn't realize the object to append had to be wrapped in an array. It seems more like an array merge than an append.
  • idbehold
    idbehold over 3 years
    @PhilD. a merge implies that values in the "target" array could be replaced/overwritten. That is not the case here. I agree a better term might have been list_concat. But in either case the fact that this function takes an array as input make it is more flexible than one which only took a single item to append. That way if you had more than one item to append you can do it in a single call instead of multiple.