DynamoDB UpdateItem in Node.js (AWS SDK v3)

UpdateItem changes specific attributes of one item (and creates the item if it doesn't exist), leaving everything else untouched. In AWS SDK v3 you send an UpdateItemCommand with an UpdateExpression.

Code

import {DynamoDBClient, UpdateItemCommand} from '@aws-sdk/client-dynamodb';

const client = new DynamoDBClient({region: 'us-east-1'});

async function updateSong() {
  const command = new UpdateItemCommand({
    TableName: 'Music',
    Key: {
      Artist: {S: 'Arturo Sandoval'},
      SongTitle: {S: 'Cubano Chant'}
    },
    UpdateExpression: 'SET Genre = :genre, #yr = :year ADD Awards :inc',
    ExpressionAttributeNames: {'#yr': 'Year'},
    ExpressionAttributeValues: {
      ':genre': {S: 'Latin Jazz'},
      ':year': {N: '1994'},
      ':inc': {N: '1'}
    },
    ReturnValues: 'ALL_NEW'
  });

  const response = await client.send(command);
  console.log(response.Attributes); // the item after the update
  return response.Attributes;
}

updateSong();

Explanation

  • Key — the full primary key of the item to update.
  • UpdateExpression — one or more clauses:
    • SET assigns attributes (Genre = :genre). Year is reserved, so it's aliased to #yr.
    • ADD on a number does an atomic increment (ADD Awards :inc) — safe under concurrency, no read-modify-write race. REMOVE deletes an attribute; DELETE removes elements from a set.
  • ExpressionAttributeValues — the :placeholder → typed-value map.
  • ReturnValues"ALL_NEW" returns the full item after the update (also UPDATED_NEW, ALL_OLD, UPDATED_OLD, or NONE).
  • Upsert semanticsUpdateItem creates the item if the key doesn't exist. Add a ConditionExpression (e.g. attribute_exists(Artist)) to update-only.

Do it visually

The DynamoDB Expression Builder builds SET / ADD / REMOVE clauses with the name/value maps and copies runnable code.

To edit items in a GUI — change a field, review the generated UpdateExpression, copy the SDK v3 code — download DynoTable.

Travaille avec DynamoDB sans la Console

DynoTable est un client de bureau rapide pour DynamoDB — parcours les tables, exécute des requêtes de style SQL et édite les items en local.