title: How to dynamically update an attribute in a dynamodb item?
body: <p>I created an item in dynamodb using Node js, the item has multiple attributes such as brand, category, discount, validity, etc. I am using uuid to generate ids for each item. Now let's say I want to update the validity attribute of the item, in which case I am currently sending the entire json object with the value of validity modified to the new value.</p>
  
  <p>This is definitely not optimal, please help me find an optimal solution.</p>
  
  <pre><code>const params = {
      TableName: process.env.PRODUCT_TABLE,
      Key: {
        id: event.pathParameters.id,
      },
      ExpressionAttributeNames: {
        '#discount': 'discount',
      },
      ExpressionAttributeValues: {
        ':brand': data.brand,
        ':category': data.category,
        ':discount': data.discount,
        ':denominations': data.denominations,
        ":validity": data.validity,
        ":redemption": data.redemption    
      },
      UpdateExpression: 'SET #discount = :discount, denominations = :denominations, brand = :brand, category = :category, validity = :validity, redemption = :redemption',
      ReturnValues: 'ALL_NEW',
    };
  </code></pre>
  
  <p>I want to send just the attribute I want to update with the new value, if I want to change the validity from 6 months to 8 months, I should just send something like:
  {
      "validity": "8 months"
  }
  And it should update the validity attribute of the item.
  Same should apply to any other attribute of the item.</p>
  
  <pre><code>'use strict';
  
  const AWS = require('aws-sdk');
  
  const dynamoDb = new AWS.DynamoDB.DocumentClient();
  
  module.exports.update = (event, context, callback) =&gt; {
    const data = JSON.parse(event.body);
  
    let attr = {};
    let nameobj = {};
    let exp = 'SET #';
    let arr = Object.keys(data);
    let attrname = {};
  
    arr.map((key) =&gt; {attr[`:${key}`]=data[key]});
  
    arr.map((key) =&gt; {
      exp += `${key} = :${key}, `
    });
  
    arr.map((key) =&gt; {nameobj[`#${key}`]=data[key]});
  
    attrname = {
      [Object.keys(nameobj)[0]] : nameobj[Object.keys(nameobj)[0]]
    }
  
    const params = {
      TableName: process.env.PRODUCT_TABLE,
      Key: {
        id: event.pathParameters.id,
      },
      ExpressionAttributeNames: attrname,
      ExpressionAttributeValues: attr,
      UpdateExpression: exp,
      ReturnValues: 'ALL_NEW',
    };
  
    // update the todo in the database
    dynamoDb.update(params, (error, result) =&gt; {
      // handle potential errors
      if (error) {
        console.error(error);
        callback(null, {
          statusCode: error.statusCode || 501,
          headers: { 'Content-Type': 'text/plain' },
          body: 'Couldn\'t update the card',
        });
        return;
      }
  
      // create a response
      const response = {
        statusCode: 200,
        body: JSON.stringify(result.Attributes),
      };
      callback(null, response);
    });
  };
  </code></pre>