skip to Main Content

I currently have an Express.js server hosted on a Lambda. I am already able to read/GET data from the table, with

app.get("/profile/general", async function (req, res) {
  
  const params = {
    TableName: "users",
    Key: {
      id: `${req.user.sub}`,
    },
  };

  try {
    let userObj = await docClient.get(params).promise();
    res.json({ "data": userObj });
  } catch (err) {
    console.log("error: ", err); 
    res.json({ "error": err });
  }
});

However, I am stuck researching the best/easiest way to do partial updates for items in DynamoDB (like objects in my "users" table).

I do not want to use a PUT method, because I don’t want to replace the whole existing user object. and what would the syntax look like? This is what I have so far —

app.patch("/profile/general", async function (req, res) {
  
  const params = {
    TableName: "users",
    Key: {
      id: `${req.user.sub}`,
    },
    UpdateExpression: "set age = req.body.age",
    UpdateExpression: "set country = req.body.country",
    UpdateExpression: "set skills = req.body.skills",
    ReturnValues: "ALL_NEW",
  };

  try {
    
    let newUserObj = await docClient.UpdateItem(params).promise(); 
    res.json({ "newData": newUserObj });
  } catch (err){
    console.log("error: ", err); 
    res.json({ "error": err });
  }
});

Is this the correct way to do a partial update with UpdateItem, passing in the necessary items to multiple "UpdateExpression" key-value pairs? The docs are a bit confusing on this.

2

Answers


  1. Here is how you should update attributes for your DynamoDB record.

    const params = {
        TableName: "users",
        Key: {
          id: `${req.user.sub}`,
        },
        UpdateExpression : "SET age = :age, country = :country, skills = :skills"
        ExpressionAttributeValues: {
            ':age': req.body.age,
            ':country': req.body.country,
            ':skills': req.body.skills
        },
        ReturnValues: "ALL_NEW",
      };
    

    In the above code, you define the values as :<expression-attribute>, and later in ExpressionAttributeValues, you provide the actual values for those attributes.

    To learn more, refer AWS documentation at Update expressions.

    Login or Signup to reply.
  2. It’s one UpdateExpression per update, where you specify all of the updates.

    app.patch("/profile/general", async function (req, res) {
      
      const params = {
        TableName: "users",
        Key: {
          id: `${req.user.sub}`,
        },
        UpdateExpression: `set age = ${req.body.age}, country = ${req.body.country}, skills = ${req.body.skills}`,
        ReturnValues: "ALL_NEW",
      };
    
      try {
        
        let newUserObj = await docClient.UpdateItem(params).promise(); 
        res.json({ "newData": newUserObj });
      } catch (err){
        console.log("error: ", err); 
        res.json({ "error": err });
      }
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search