stevejay / dynamodb-doc-client-wrapper

For building complete result sets from the AWS DynamoDB API DocumentClient class

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

update part of attributes in Dynamodb

ozbillwang opened this issue · comments

I am looking for the feature to update part of attributes in DynamoDb. Does this wrapper work for this feature?

Use below codes, I successfully update an item

https://github.com/serverless/examples/blob/master/aws-node-rest-api-with-dynamodb/todos/update.js#L22-L37

Then I add more attributes:

  const params = {
    TableName: process.env.DYNAMODB_TABLE,
    Key: {
      id: event.pathParameters.id,
    },
    ExpressionAttributeNames: {
      '#user_name': 'name',
    },
    ExpressionAttributeValues: {
      ':name': data.name,
      ':email': data.email,
      ':username': data.username,
      ':password': data.password,
      ':checked': data.checked,
      ':updatedAt': timestamp,
    },
    UpdateExpression: 'SET #user_name = :name, email = :email, username = :username, password = :password, checked = :checked, updatedAt = :updatedAt',
    ReturnValues: 'ALL_NEW',
  };

Above codes work fine if I feed all attributes.

$ cat test/user-1.json
{
  "name": "Bob",
  "email": "bob@example.com",
  "username": "bob",
  "password": "adfdsfdsf",
  "checked": false
}

But if I only want to update part of them, since I needn't update email and password every time, I got error Couldn't fetch the user item.

$ cat test/user-1.json
{
  "name": "Bob",
  "username": "bob-1",
  "checked": false
}

$ curl -X PUT ${url}/${id} --data '@test/user-1.json'
Couldn't fetch the user item.

So how to change the code that I don't have to update all attributes.

Closing as not relevant to this library.