Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node.js AWS dynamodb updateItem

Is there a way to achieve the following few points with updateItem:

  1. Add attributes if the attributes not exist in DynamoDB
  2. Update attributes if the attributes exist in DynamoDB
  3. Leave those attributes as what they are if the attributes are not contained in the params.

Here is an example: This is the object in DynamoDB:

{
    id: "1234",
    variable1: "hello",
    variable2: "world"
}

Here is the input that I wish to update:

{
    id: "1234",
    variable1: "hello2",
    variable23: "dog"  // the variable name "variable23" could be anything
}

Here is the updated item in the DynamoDB that I want to achieve:

{
    id: "1234",
    variable1: "hello2",
    variable2: "world",
    variable23: "dog"
}

The "variable23" could be any variable name as input.

I use node.js

like image 736
Pano Avatar asked Jan 28 '17 22:01

Pano


3 Answers

This is exactly what AWS.DynamoDB.DocumentClient's update method does.

There is already a sample code on how to use the update method here for AWS SDK for JavaScript in Node.js.

For example:

'use strict';

const aws = require('aws-sdk');

// It is recommended that we instantiate AWS clients outside the scope of the handler 
// to take advantage of connection re-use.
const docClient = new aws.DynamoDB.DocumentClient();

exports.handler = (event, context, callback) => {
    const params = {
        TableName: "MYTABLE",
        Key: {
            "id": "1"
        },
        UpdateExpression: "set variable1 = :x, #MyVariable = :y",
        ExpressionAttributeNames: {
            "#MyVariable": "variable23"
        },
        ExpressionAttributeValues: {
            ":x": "hello2",
            ":y": "dog"
        }
    };

    docClient.update(params, function(err, data) {
        if (err) console.log(err);
        else console.log(data);
    });
};
like image 193
Khalid T. Avatar answered Nov 16 '22 22:11

Khalid T.


You can update attributes dynamically. see below code.

export const update = (item) => {
  console.log(item)
  const Item = {
    note: "dynamic",
    totalChild: "totalChild",
    totalGuests: "totalGuests"
  };
  let updateExpression='set';
  let ExpressionAttributeNames={};
  let ExpressionAttributeValues = {};
  for (const property in Item) {
    updateExpression += ` #${property} = :${property} ,`;
    ExpressionAttributeNames['#'+property] = property ;
    ExpressionAttributeValues[':'+property]=Item[property];
  }

  
  console.log(ExpressionAttributeNames);


  updateExpression= updateExpression.slice(0, -1);
  
  
   const params = {
     TableName: TABLE_NAME,
     Key: {
      booking_attempt_id: item.booking_attempt_id,
     },
     UpdateExpression: updateExpression,
     ExpressionAttributeNames: ExpressionAttributeNames,
     ExpressionAttributeValues: ExpressionAttributeValues
   };

   return dynamo.update(params).promise().then(result => {
       return result;
   })
   
}
like image 25
Cemil Birinci Avatar answered Nov 16 '22 21:11

Cemil Birinci


I think some of the examples are a bit confusing. If I have the following table columns

ID  | Name | Age

And I want to update the Name attribute and leave the Age attribute unchanged.

const updateName = async () => {
  const aws = require('aws-sdk');
  const docClient = new aws.DynamoDB.DocumentClient();

  const newName = 'Bob';

  const params = {
    TableName: 'myTable',
    Key: {
      ID: 'myId',
    },
    UpdateExpression: 'set Name = :r',
    ExpressionAttributeValues: {
      ':r': newName,
    },
  };

  await docClient.update(params).promise();
}

updateName();

This seemed a bit more simple.

like image 23
thedanotto Avatar answered Nov 16 '22 21:11

thedanotto