0

I wish to create an Item in DynamoDB that is a list. This is my code:

var list_update_params = {
      TableName: "table01",
      Key: {
        "MachineID": {
          "S": MachineID
        },
        "Hour": {
          "S": Hour
        }
      },
      UpdateExpression: "set var01_list = list_append(var01_list, :ot)",
      ExpressionAttributeValues: {
        ":ot": {"L": [{"N": var01}]}
      },
      ReturnValues: "NONE"
    };
    dynamodb.updateItem(list_update_params, function(err, data) {
      if (err) console.log(err, err.stack);
      else console.log("Updated List to DynamoDB");
    });

The problem is list_append expects the attribute var01_list to already be present, but I wouldn't know at the first insert. Is there a technique where it'll let me create an insert a List attribute if one doesn't exist and append to it in later calls?

Joseph
  • 2,155
  • 6
  • 20
  • 32

1 Answers1

1

Got the answer from a similar post here.

UpdateExpression: "set var01_list= list_append(if_not_exists(var01_list, :empty_list), :h)",
      ExpressionAttributeValues: {
        ":h": {"L": [{"N":var01}]},
        ":empty_list": {"L": []}
      },

The key was using if_not_exists with list_append. Didn't know that could be done in this matter

Community
  • 1
  • 1
Joseph
  • 2,155
  • 6
  • 20
  • 32