How do I overwrite a list of strings in dynamodb using document client

Viewed 36

I have a dynamoDB table that has a list of strings as an attribute of a User item (AssignedPractices):

    {
      "UserID": "2291b7bf-90a4-439e-872e-1bf6e8112a59",
      "Address": "?",
      "AssignedPractices": [
         "50575950-d932-48bc-8f27-eb476408ef91",
         "f4495bed-faf7-44e6-9f76-08fc0818b8f3"
       ],
      "Name": "John Doe",
      "Phone": "?",
     }

In dynamo it looks like this:

"AssignedPractices": {
  "L": [
   {
    "S": "50575950-d932-48bc-8f27-eb476408ef91"
   },
   {
    "S": "f4495bed-faf7-44e6-9f76-08fc0818b8f3"
   }
  ]
 },

I'm trying to update this list using a graphQL mutation. Since this is going to be a short list of strings, in order to not deal with insertions/removals, I just want to overwrite it.

This is what I've attempted, but documentClient just bails on me .. no error or exception.

      const params = {
            TableName: tableName,
            Key: { [primaryKey]: primaryKeyValue },
            UpdateExpression: `set ${updateKey} = :updateValue`,
            ExpressionAttributeValues: {
                ':updateValue': updateValue,
            },
        };

        console.log('[update] params =', params);

        try {
            const result = await documentClient.update(params).promise();
            console.log('[update] result =', result);
            return 1;
        } catch (err) {
            console.log('[update] : error', err);
            return 0;
        }

I see in CloudWatch that it did get the list of strings:

INFO    [update] params = {
  TableName: 'users',
  Key: { UserID: '2291b7bf-90a4-439e-872e-1bf6e8112a59' },
  UpdateExpression: 'set AssignedPractices = :updateValue',
  ExpressionAttributeValues: {
    ':updateValue': [
      '50575950-d932-48bc-8f27-eb476408ef91',
      'f4495bed-faf7-44e6-9f76-08fc0818b8f3',
      'da9e34b1-8bad-4f2d-b0eb-637dbc6c8ef0'
    ]
  }
}

But nothing after that, and the list obviously didn't update. Feels to me my UpdateExpression is not correct, but not sure how to make it work.

1 Answers

I tested this out locally, without GraphQL and it works as expected.

const AWS = require('aws-sdk')
const ddb = new AWS.DynamoDB.DocumentClient({ region: 'eu-west-1' });


const email = "myemail@gmail.com"
const location = "sales"

const params = {
    TableName: 'testTable',
    Key: {
        email: email,
        sortKey: `${email}#${location}`
    },
    UpdateExpression: 'set AssignedPractices = :updateValue',
    ExpressionAttributeValues: {
        ':updateValue': [
            '50575950-d932-48bc-8f27-eb476408ef91',
            'f4495bed-faf7-44e6-9f76-08fc0818b8f3',
            'da9e34b1-8bad-4f2d-b0eb-637dbc6c8ef0'
        ]
    },
    ReturnValues: 'ALL_NEW'
}

ddb.update(params)
    .promise()
    .then(res => console.log(res))
    .catch(err => {
        console.log("ERROR")
        console.log(err.code, err.message);
    })

You can confirm the update succeeded by using the ReturnValues parameter, the output below:

{
  Attributes: {
    location: 'sales',
    AssignedPractices: [
      '50575950-d932-48bc-8f27-eb476408ef91',
      'f4495bed-faf7-44e6-9f76-08fc0818b8f3',
      'da9e34b1-8bad-4f2d-b0eb-637dbc6c8ef0'
    ],
    email: 'myemail@gmail.com',
    sortKey: 'myemail@gmail.com#sales'
  }
}
Related