Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to SET and DELETE using a single UpdateExpression with Dynamo

I'm trying to update an item using updateExpression, and i'd like to edit an attribute and delete another attribute on the same object, but i get the following error:

Invalid UpdateExpression: Syntax error; token: EOF near: "attributeToDelete"

I have to admit i wrote the update expression guessing the proper way to use it (failing). Here's the code snippet:

dynamodb.updateAsync({
    TableName: `myTable`,
    Key: { id: req.params.id },
    UpdateExpression: 'SET attributeToEdit = :newValue DELETE attributeToDelete',
    ExpressionAttributeValues: { ':newValue': 'valueToSet' },
  })

How am i supposed to write an UpdateExpression when I want to include more than a action? (where actions are -> SET, DELETE, REMOVE, ADD)

like image 833
Koop4 Avatar asked May 17 '17 10:05

Koop4


People also ask

How do I delete items in DynamoDB?

With the DynamoDB API, you use the DeleteItem action to delete data from a table, one item at a time. You must specify the item's primary key values. In addition to DeleteItem , Amazon DynamoDB supports a BatchWriteItem action for deleting multiple items at the same time.

How do I delete multiple items in DynamoDB?

To delete a single item, use DeleteItem. To delete multiple items, use BatchWriteItem. Despite the naming of BatchWriteItem , it can be used to put multiple items or to delete multiple items, and you can target one or more DynamoDB tables in the same API call.

How do I change the value of a Dynamo database?

To update an existing item in an Amazon DynamoDB table, you use the UpdateItem operation. You must provide the key of the item that you want to update. You must also provide an update expression, indicating the attributes that you want to modify and the values that you want to assign to them.

What is condition expression in DynamoDB?

Conditional put The PutItem operation overwrites an item with the same key (if it exists). If you want to avoid this, use a condition expression. This allows the write to proceed only if the item in question does not already have the same key.


2 Answers

Use REMOVE rather than DELETE. DELETE is used to delete element from SET.

 UpdateExpression: 'SET attributeToEdit = :newValue REMOVE attributeToDelete',

REMOVE - Removes one or more attributes from an item.

like image 179
notionquest Avatar answered Oct 18 '22 10:10

notionquest


I struggled with this for a long while, so I figured a second example may be helpful for future searchers. This example is using CakePHP, Marshal JSON, SET and ADD, and includes variables to show a couple of different ways to get what you want. You'd obviously also pass in all the variables in the overall function calling this.

    $key = $this->marshaler->marshalJson('
        {
            "field_one": ' . $variableOne . '
        }
    ');

    if ($criteriaOne === null) {
        $add = $this->marshaler->marshalJson('
            {"0":{
                "field_two": ' . $variableTwo . ',
                "field_three": "' . $variableThree . '"
            }}
        ');
    } else {
        $add = $this->marshaler->marshalJson('
            {"0":{
                "field_two": ' . $variableTwo . ',
                "field_three": "' . $variableThree . '"
                "field_four": "null"
            }}
        ');
    }

    $eav = [
        ":vals" => [
            "L" => [$add[0]]
        ],
        ":vals1" => [
            "NS" => ["$variableTwo"]
        ]
    ];

    $ean = [
        "#co" => "column_one",
        "#ct" => "column_two"
    ];

    return [
        'TableName' => 'Dynamo_Table_Name',
        'Key' => $key,
        'UpdateExpression' =>
            'SET #co = list_append(:vals, #co) ADD #ct :vals1',
        'ExpressionAttributeNames' => $ean,
        'ExpressionAttributeValues' => $eav,
        'ReturnValues' => 'ALL_NEW'
    ];
like image 27
tjchecketts Avatar answered Oct 18 '22 08:10

tjchecketts