DYNAMODB_UPDATE - Atualizar Item
O que é este Node?
O DYNAMODB_UPDATE é o node responsável por atualizar campos específicos de um item existente no DynamoDB sem substituir o item inteiro, de forma eficiente e atômica.
Por que este Node existe?
Atualizar apenas campos necessários é mais eficiente que substituir o item completo. O DYNAMODB_UPDATE existe para:
- Eficiência: Atualiza apenas os campos especificados, economiza largura de banda
- Atomicidade: Operação atômica - completa ou não acontece
- Operações Matemáticas: Incrementar/decrementar valores (contadores, estoque)
- Listas e Sets: Adicionar/remover itens de listas sem reescrever tudo
Como funciona internamente?
Quando o DYNAMODB_UPDATE é executado, o sistema:
- Autentica: Valida as credenciais AWS
- Localiza Item: Busca o item pela chave primária
- Aplica UpdateExpression: Modifica apenas os campos especificados
- Se item não existe: Pode criar ou retornar erro (depende da expressão)
- Se item existe: Atualiza campos e mantém os demais intactos
- Retorna: Item atualizado (opcionalmente)
Código interno (aws-executors.service.ts:349-359):
case 'updateItem':
const updateResult = await dynamodb.update({
TableName: data.tableName,
Key: data.key,
UpdateExpression: data.updateExpression,
ExpressionAttributeValues: data.expressionAttributeValues,
}).promise();
return {
success: true,
attributes: updateResult.Attributes,
};
Quando você DEVE usar este Node?
Use DYNAMODB_UPDATE sempre que precisar de modificar dados existentes:
Casos de uso
- Incrementar contador: "Adicionar +1 ao número de visualizações"
- Atualizar status: "Mudar status do pedido de 'pendente' para 'enviado'"
- Modificar estoque: "Decrementar 5 unidades do estoque após venda"
- Adicionar item à lista: "Adicionar novo telefone à lista de contatos"
Quando NÃO usar DYNAMODB_UPDATE
- Substituir item completo: Use DYNAMODB_PUT ao invés (mais simples)
- Item não existe: Use DYNAMODB_PUT para criar (Update pode falhar)
- Deletar item: Use DYNAMODB_DELETE ao invés
- Atualizar múltiplos itens: Faça múltiplas chamadas ou use BatchWriteItem
Parâmetros Detalhados
accessKeyId (string, obrigatório)
O que é: Chave de acesso AWS.
Padrão: Nenhum (sempre obrigatório)
Flow completo para testar:
{
"name": "Teste DynamoDB Update - Access Key",
"nodes": [
{
"id": "start_1",
"type": "start",
"position": { "x": 100, "y": 100 },
"data": { "label": "Início" }
},
{
"id": "input_1",
"type": "input",
"position": { "x": 250, "y": 100 },
"data": {
"label": "Product ID",
"parameters": {
"message": "Digite o ID do produto para atualizar:",
"variableName": "product_id"
}
}
},
{
"id": "dynamodb_1",
"type": "dynamodb_update",
"position": { "x": 400, "y": 100 },
"data": {
"label": "Incrementar Views",
"parameters": {
"accessKeyId": "AKIAIOSFODNN7EXAMPLE",
"secretAccessKey": "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
"region": "us-east-1",
"operation": "updateItem",
"tableName": "Produtos",
"key": {
"productId": "{{product_id}}"
},
"updateExpression": "SET visualizacoes = visualizacoes + :inc",
"expressionAttributeValues": {
":inc": 1
}
}
}
},
{
"id": "message_1",
"type": "message",
"position": { "x": 550, "y": 100 },
"data": {
"label": "Confirmação",
"parameters": {
"message": "✅ Visualização registrada!"
}
}
},
{
"id": "end_1",
"type": "end",
"position": { "x": 700, "y": 100 },
"data": { "label": "Fim" }
}
],
"edges": [
{ "source": "start_1", "target": "input_1" },
{ "source": "input_1", "target": "dynamodb_1" },
{ "source": "dynamodb_1", "target": "message_1" },
{ "source": "message_1", "target": "end_1" }
]
}
Teste: Digite um ID. O campo visualizacoes será incrementado em 1.
secretAccessKey (string, obrigatório)
O que é: Chave secreta AWS.
Padrão: Nenhum (sempre obrigatório)
Flow completo para testar:
{
"name": "Teste DynamoDB Update - Secret Key",
"nodes": [
{
"id": "start_1",
"type": "start",
"position": { "x": 100, "y": 100 },
"data": { "label": "Início" }
},
{
"id": "email_1",
"type": "email",
"position": { "x": 250, "y": 100 },
"data": {
"label": "Email",
"parameters": {
"message": "Digite seu email:",
"variableName": "user_email"
}
}
},
{
"id": "dynamodb_1",
"type": "dynamodb_update",
"position": { "x": 400, "y": 100 },
"data": {
"label": "Atualizar Último Acesso",
"parameters": {
"accessKeyId": "AKIAIOSFODNN7EXAMPLE",
"secretAccessKey": "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
"region": "sa-east-1",
"operation": "updateItem",
"tableName": "Usuarios",
"key": {
"userId": "{{user_email}}"
},
"updateExpression": "SET ultimoAcesso = :now",
"expressionAttributeValues": {
":now": "2025-10-12T10:30:00Z"
}
}
}
},
{
"id": "message_1",
"type": "message",
"position": { "x": 550, "y": 100 },
"data": {
"label": "Confirmação",
"parameters": {
"message": "Último acesso atualizado!"
}
}
},
{
"id": "end_1",
"type": "end",
"position": { "x": 700, "y": 100 },
"data": { "label": "Fim" }
}
],
"edges": [
{ "source": "start_1", "target": "email_1" },
{ "source": "email_1", "target": "dynamodb_1" },
{ "source": "dynamodb_1", "target": "message_1" },
{ "source": "message_1", "target": "end_1" }
]
}
Teste: Digite email. O campo ultimoAcesso será atualizado.
region (string, obrigatório)
O que é: Região AWS da tabela.
Padrão: Nenhum (sempre obrigatório)
Flow completo para testar:
{
"name": "Teste DynamoDB Update - Region",
"nodes": [
{
"id": "start_1",
"type": "start",
"position": { "x": 100, "y": 150 },
"data": { "label": "Início" }
},
{
"id": "input_1",
"type": "input",
"position": { "x": 250, "y": 150 },
"data": {
"label": "Order ID",
"parameters": {
"message": "Digite o ID do pedido:",
"variableName": "order_id"
}
}
},
{
"id": "switch_1",
"type": "switch",
"position": { "x": 400, "y": 150 },
"data": {
"label": "Região",
"parameters": {
"message": "Região do pedido?\n1️⃣ US\n2️⃣ Brasil",
"cases": [
{ "value": "1", "label": "US" },
{ "value": "2", "label": "BR" }
]
}
}
},
{
"id": "dynamodb_1",
"type": "dynamodb_update",
"position": { "x": 550, "y": 100 },
"data": {
"label": "Update US",
"parameters": {
"accessKeyId": "AKIAIOSFODNN7EXAMPLE",
"secretAccessKey": "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
"region": "us-east-1",
"operation": "updateItem",
"tableName": "Pedidos",
"key": { "orderId": "{{order_id}}" },
"updateExpression": "SET #status = :s",
"expressionAttributeValues": { ":s": "enviado" }
}
}
},
{
"id": "dynamodb_2",
"type": "dynamodb_update",
"position": { "x": 550, "y": 200 },
"data": {
"label": "Update BR",
"parameters": {
"accessKeyId": "AKIAIOSFODNN7EXAMPLE",
"secretAccessKey": "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
"region": "sa-east-1",
"operation": "updateItem",
"tableName": "Pedidos",
"key": { "orderId": "{{order_id}}" },
"updateExpression": "SET #status = :s",
"expressionAttributeValues": { ":s": "enviado" }
}
}
},
{
"id": "message_1",
"type": "message",
"position": { "x": 700, "y": 150 },
"data": {
"label": "Confirmação",
"parameters": {
"message": "Status atualizado na região selecionada"
}
}
},
{
"id": "end_1",
"type": "end",
"position": { "x": 850, "y": 150 },
"data": { "label": "Fim" }
}
],
"edges": [
{ "source": "start_1", "target": "input_1" },
{ "source": "input_1", "target": "switch_1" },
{ "source": "switch_1", "target": "dynamodb_1", "label": "1" },
{ "source": "switch_1", "target": "dynamodb_2", "label": "2" },
{ "source": "dynamodb_1", "target": "message_1" },
{ "source": "dynamodb_2", "target": "message_1" },
{ "source": "message_1", "target": "end_1" }
]
}
Teste: Escolha região. Update executa na região correta.
tableName (string, obrigatório)
O que é: Nome da tabela DynamoDB.
Padrão: Nenhum (sempre obrigatório)
Flow completo para testar:
{
"name": "Teste DynamoDB Update - Table Name",
"nodes": [
{
"id": "start_1",
"type": "start",
"position": { "x": 100, "y": 100 },
"data": { "label": "Início" }
},
{
"id": "input_1",
"type": "input",
"position": { "x": 250, "y": 100 },
"data": {
"label": "Tabela",
"parameters": {
"message": "Nome da tabela:",
"variableName": "table_name"
}
}
},
{
"id": "input_2",
"type": "input",
"position": { "x": 400, "y": 100 },
"data": {
"label": "ID",
"parameters": {
"message": "ID do item:",
"variableName": "item_id"
}
}
},
{
"id": "dynamodb_1",
"type": "dynamodb_update",
"position": { "x": 550, "y": 100 },
"data": {
"label": "Update Genérico",
"parameters": {
"accessKeyId": "AKIAIOSFODNN7EXAMPLE",
"secretAccessKey": "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
"region": "us-east-1",
"operation": "updateItem",
"tableName": "{{table_name}}",
"key": { "id": "{{item_id}}" },
"updateExpression": "SET lastModified = :now",
"expressionAttributeValues": { ":now": "2025-10-12" }
}
}
},
{
"id": "message_1",
"type": "message",
"position": { "x": 700, "y": 100 },
"data": {
"label": "Confirmação",
"parameters": {
"message": "Tabela {{table_name}} atualizada"
}
}
},
{
"id": "end_1",
"type": "end",
"position": { "x": 850, "y": 100 },
"data": { "label": "Fim" }
}
],
"edges": [
{ "source": "start_1", "target": "input_1" },
{ "source": "input_1", "target": "input_2" },
{ "source": "input_2", "target": "dynamodb_1" },
{ "source": "dynamodb_1", "target": "message_1" },
{ "source": "message_1", "target": "end_1" }
]
}
Teste: Digite tabela e ID. Campo lastModified será atualizado.
key (object, obrigatório)
O que é: Chave primária do item a atualizar (Partition Key + Sort Key se aplicável).
Padrão: Nenhum (sempre obrigatório)
Flow completo para testar:
{
"name": "Teste DynamoDB Update - Key",
"nodes": [
{
"id": "start_1",
"type": "start",
"position": { "x": 100, "y": 100 },
"data": { "label": "Início" }
},
{
"id": "email_1",
"type": "email",
"position": { "x": 250, "y": 100 },
"data": {
"label": "Email",
"parameters": {
"message": "Digite seu email:",
"variableName": "user_email"
}
}
},
{
"id": "input_1",
"type": "input",
"position": { "x": 400, "y": 100 },
"data": {
"label": "Novo Nome",
"parameters": {
"message": "Digite seu novo nome:",
"variableName": "new_name"
}
}
},
{
"id": "dynamodb_1",
"type": "dynamodb_update",
"position": { "x": 550, "y": 100 },
"data": {
"label": "Atualizar Nome",
"parameters": {
"accessKeyId": "AKIAIOSFODNN7EXAMPLE",
"secretAccessKey": "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
"region": "us-east-1",
"operation": "updateItem",
"tableName": "Usuarios",
"key": {
"userId": "{{user_email}}"
},
"updateExpression": "SET nome = :n",
"expressionAttributeValues": {
":n": "{{new_name}}"
}
}
}
},
{
"id": "message_1",
"type": "message",
"position": { "x": 700, "y": 100 },
"data": {
"label": "Confirmação",
"parameters": {
"message": "✅ Nome atualizado para: {{new_name}}"
}
}
},
{
"id": "end_1",
"type": "end",
"position": { "x": 850, "y": 100 },
"data": { "label": "Fim" }
}
],
"edges": [
{ "source": "start_1", "target": "email_1" },
{ "source": "email_1", "target": "input_1" },
{ "source": "input_1", "target": "dynamodb_1" },
{ "source": "dynamodb_1", "target": "message_1" },
{ "source": "message_1", "target": "end_1" }
]
}
Teste: Digite email e novo nome. Item com aquela chave será atualizado.
updateExpression (string, obrigatório)
O que é: Expressão que define quais campos atualizar e como.
Padrão: Nenhum (sempre obrigatório)
Sintaxe:
- SET campo = :valor - Define valor
- SET campo = campo + :inc - Incrementa
- SET campo = campo - :dec - Decrementa
- REMOVE campo - Remove atributo
- ADD campo :valor - Adiciona a número ou set
- DELETE campo :valor - Remove de set
Flow completo para testar:
{
"name": "Teste DynamoDB Update - Update Expression",
"nodes": [
{
"id": "start_1",
"type": "start",
"position": { "x": 100, "y": 200 },
"data": { "label": "Início" }
},
{
"id": "input_1",
"type": "input",
"position": { "x": 250, "y": 200 },
"data": {
"label": "Product ID",
"parameters": {
"message": "Digite o ID do produto:",
"variableName": "product_id"
}
}
},
{
"id": "switch_1",
"type": "switch",
"position": { "x": 400, "y": 200 },
"data": {
"label": "Operação",
"parameters": {
"message": "O que fazer?\n1️⃣ Vender (-1 estoque)\n2️⃣ Repor (+10 estoque)\n3️⃣ Arquivar",
"cases": [
{ "value": "1", "label": "Vender" },
{ "value": "2", "label": "Repor" },
{ "value": "3", "label": "Arquivar" }
]
}
}
},
{
"id": "dynamodb_1",
"type": "dynamodb_update",
"position": { "x": 550, "y": 150 },
"data": {
"label": "Decrementar",
"parameters": {
"accessKeyId": "AKIAIOSFODNN7EXAMPLE",
"secretAccessKey": "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
"region": "us-east-1",
"operation": "updateItem",
"tableName": "Produtos",
"key": { "productId": "{{product_id}}" },
"updateExpression": "SET estoque = estoque - :dec",
"expressionAttributeValues": { ":dec": 1 }
}
}
},
{
"id": "dynamodb_2",
"type": "dynamodb_update",
"position": { "x": 550, "y": 250 },
"data": {
"label": "Incrementar",
"parameters": {
"accessKeyId": "AKIAIOSFODNN7EXAMPLE",
"secretAccessKey": "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
"region": "us-east-1",
"operation": "updateItem",
"tableName": "Produtos",
"key": { "productId": "{{product_id}}" },
"updateExpression": "SET estoque = estoque + :inc",
"expressionAttributeValues": { ":inc": 10 }
}
}
},
{
"id": "dynamodb_3",
"type": "dynamodb_update",
"position": { "x": 550, "y": 350 },
"data": {
"label": "Arquivar",
"parameters": {
"accessKeyId": "AKIAIOSFODNN7EXAMPLE",
"secretAccessKey": "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
"region": "us-east-1",
"operation": "updateItem",
"tableName": "Produtos",
"key": { "productId": "{{product_id}}" },
"updateExpression": "SET #status = :s REMOVE preco",
"expressionAttributeValues": { ":s": "arquivado" }
}
}
},
{
"id": "message_1",
"type": "message",
"position": { "x": 700, "y": 250 },
"data": {
"label": "Confirmação",
"parameters": {
"message": "Produto atualizado com sucesso!"
}
}
},
{
"id": "end_1",
"type": "end",
"position": { "x": 850, "y": 250 },
"data": { "label": "Fim" }
}
],
"edges": [
{ "source": "start_1", "target": "input_1" },
{ "source": "input_1", "target": "switch_1" },
{ "source": "switch_1", "target": "dynamodb_1", "label": "1" },
{ "source": "switch_1", "target": "dynamodb_2", "label": "2" },
{ "source": "switch_1", "target": "dynamodb_3", "label": "3" },
{ "source": "dynamodb_1", "target": "message_1" },
{ "source": "dynamodb_2", "target": "message_1" },
{ "source": "dynamodb_3", "target": "message_1" },
{ "source": "message_1", "target": "end_1" }
]
}
Teste: Escolha operação. Cada UpdateExpression faz algo diferente no item.
expressionAttributeValues (object, obrigatório se houver placeholders)
O que é: Valores das variáveis usadas no updateExpression.
Padrão: Nenhum
Flow completo para testar:
{
"name": "Teste DynamoDB Update - Expression Values",
"nodes": [
{
"id": "start_1",
"type": "start",
"position": { "x": 100, "y": 150 },
"data": { "label": "Início" }
},
{
"id": "input_1",
"type": "input",
"position": { "x": 250, "y": 150 },
"data": {
"label": "Order ID",
"parameters": {
"message": "Digite o ID do pedido:",
"variableName": "order_id"
}
}
},
{
"id": "switch_1",
"type": "switch",
"position": { "x": 400, "y": 150 },
"data": {
"label": "Novo Status",
"parameters": {
"message": "Atualizar status para:\n1️⃣ Processando\n2️⃣ Enviado\n3️⃣ Entregue",
"cases": [
{ "value": "1", "label": "Processando" },
{ "value": "2", "label": "Enviado" },
{ "value": "3", "label": "Entregue" }
]
}
}
},
{
"id": "dynamodb_1",
"type": "dynamodb_update",
"position": { "x": 550, "y": 100 },
"data": {
"label": "Status Processando",
"parameters": {
"accessKeyId": "AKIAIOSFODNN7EXAMPLE",
"secretAccessKey": "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
"region": "us-east-1",
"operation": "updateItem",
"tableName": "Pedidos",
"key": { "orderId": "{{order_id}}" },
"updateExpression": "SET #status = :s, lastUpdate = :now",
"expressionAttributeValues": {
":s": "processando",
":now": "2025-10-12T10:00:00Z"
}
}
}
},
{
"id": "dynamodb_2",
"type": "dynamodb_update",
"position": { "x": 550, "y": 200 },
"data": {
"label": "Status Enviado",
"parameters": {
"accessKeyId": "AKIAIOSFODNN7EXAMPLE",
"secretAccessKey": "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
"region": "us-east-1",
"operation": "updateItem",
"tableName": "Pedidos",
"key": { "orderId": "{{order_id}}" },
"updateExpression": "SET #status = :s, lastUpdate = :now",
"expressionAttributeValues": {
":s": "enviado",
":now": "2025-10-12T10:00:00Z"
}
}
}
},
{
"id": "dynamodb_3",
"type": "dynamodb_update",
"position": { "x": 550, "y": 300 },
"data": {
"label": "Status Entregue",
"parameters": {
"accessKeyId": "AKIAIOSFODNN7EXAMPLE",
"secretAccessKey": "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
"region": "us-east-1",
"operation": "updateItem",
"tableName": "Pedidos",
"key": { "orderId": "{{order_id}}" },
"updateExpression": "SET #status = :s, lastUpdate = :now",
"expressionAttributeValues": {
":s": "entregue",
":now": "2025-10-12T10:00:00Z"
}
}
}
},
{
"id": "message_1",
"type": "message",
"position": { "x": 700, "y": 200 },
"data": {
"label": "Confirmação",
"parameters": {
"message": "Status do pedido atualizado!"
}
}
},
{
"id": "end_1",
"type": "end",
"position": { "x": 850, "y": 200 },
"data": { "label": "Fim" }
}
],
"edges": [
{ "source": "start_1", "target": "input_1" },
{ "source": "input_1", "target": "switch_1" },
{ "source": "switch_1", "target": "dynamodb_1", "label": "1" },
{ "source": "switch_1", "target": "dynamodb_2", "label": "2" },
{ "source": "switch_1", "target": "dynamodb_3", "label": "3" },
{ "source": "dynamodb_1", "target": "message_1" },
{ "source": "dynamodb_2", "target": "message_1" },
{ "source": "dynamodb_3", "target": "message_1" },
{ "source": "message_1", "target": "end_1" }
]
}
Teste: Escolha status. Os valores dinâmicos são injetados na expressão.
Parâmetros
| Campo | Tipo | Obrigatório | Descrição |
|---|---|---|---|
| accessKeyId | string | Sim | Chave de acesso AWS |
| secretAccessKey | string | Sim | Chave secreta AWS |
| region | string | Sim | Região AWS |
| operation | string | Sim | Tipo de operação (sempre "updateItem") |
| tableName | string | Sim | Nome da tabela |
| key | object | Sim | Chave primária do item |
| updateExpression | string | Sim | Expressão de atualização |
| expressionAttributeValues | object | Condicional | Valores dos placeholders (obrigatório se houver) |
Exemplo 1: Sistema de Estoque - Venda de Produto
Objetivo: Demonstrar decremento atômico de estoque ao processar venda.
JSON para Importar
{
"name": "Venda de Produto - Update Estoque",
"nodes": [
{
"id": "start_1",
"type": "start",
"position": { "x": 100, "y": 200 },
"data": { "label": "Início" }
},
{
"id": "message_1",
"type": "message",
"position": { "x": 250, "y": 200 },
"data": {
"label": "Boas-vindas",
"parameters": {
"message": "🛒 Sistema de Vendas\n\nVamos processar sua compra."
}
}
},
{
"id": "input_1",
"type": "input",
"position": { "x": 400, "y": 200 },
"data": {
"label": "Código Produto",
"parameters": {
"message": "Digite o código do produto:",
"variableName": "product_code"
}
}
},
{
"id": "number_1",
"type": "number",
"position": { "x": 550, "y": 200 },
"data": {
"label": "Quantidade",
"parameters": {
"message": "Quantas unidades?",
"variableName": "quantity"
}
}
},
{
"id": "dynamodb_1",
"type": "dynamodb_update",
"position": { "x": 700, "y": 200 },
"data": {
"label": "Atualizar Estoque",
"parameters": {
"accessKeyId": "AKIAIOSFODNN7EXAMPLE",
"secretAccessKey": "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
"region": "sa-east-1",
"operation": "updateItem",
"tableName": "Produtos",
"key": {
"productId": "{{product_code}}"
},
"updateExpression": "SET estoque = estoque - :qty, ultimaVenda = :now ADD totalVendas :qty",
"expressionAttributeValues": {
":qty": "{{quantity}}",
":now": "2025-10-12T10:30:00Z"
}
}
}
},
{
"id": "message_2",
"type": "message",
"position": { "x": 850, "y": 200 },
"data": {
"label": "Sucesso",
"parameters": {
"message": "✅ Venda processada!\n\n📦 Produto: {{product_code}}\n🔢 Quantidade: {{quantity}}\n📊 Estoque atualizado\n💰 Total de vendas incrementado\n\nObrigado pela compra!"
}
}
},
{
"id": "end_1",
"type": "end",
"position": { "x": 1000, "y": 200 },
"data": { "label": "Fim" }
}
],
"edges": [
{ "source": "start_1", "target": "message_1" },
{ "source": "message_1", "target": "input_1" },
{ "source": "input_1", "target": "number_1" },
{ "source": "number_1", "target": "dynamodb_1" },
{ "source": "dynamodb_1", "target": "message_2" },
{ "source": "message_2", "target": "end_1" }
]
}
Saída esperada:
Sistema: 🛒 Sistema de Vendas
Vamos processar sua compra.
Sistema: Digite o código do produto:
Usuário: PROD123
Sistema: Quantas unidades?
Usuário: 3
Sistema: ✅ Venda processada!
📦 Produto: PROD123
🔢 Quantidade: 3
📊 Estoque atualizado
💰 Total de vendas incrementado
Obrigado pela compra!
Exemplo 2: Sistema de Pedidos - Atualizar Status
Objetivo: Demonstrar atualização de status e timestamp de modificação.
JSON para Importar
{
"name": "Atualizar Status de Pedido - Update",
"nodes": [
{
"id": "start_1",
"type": "start",
"position": { "x": 100, "y": 200 },
"data": { "label": "Início" }
},
{
"id": "message_1",
"type": "message",
"position": { "x": 250, "y": 200 },
"data": {
"label": "Iniciar",
"parameters": {
"message": "📦 Atualização de Pedido\n\nVamos atualizar o status."
}
}
},
{
"id": "input_1",
"type": "input",
"position": { "x": 400, "y": 200 },
"data": {
"label": "Order ID",
"parameters": {
"message": "Digite o ID do pedido:",
"variableName": "order_id"
}
}
},
{
"id": "switch_1",
"type": "switch",
"position": { "x": 550, "y": 200 },
"data": {
"label": "Novo Status",
"parameters": {
"message": "Atualizar para qual status?\n1️⃣ Em Processamento\n2️⃣ Enviado\n3️⃣ Entregue\n4️⃣ Cancelado",
"cases": [
{ "value": "1", "label": "Processando" },
{ "value": "2", "label": "Enviado" },
{ "value": "3", "label": "Entregue" },
{ "value": "4", "label": "Cancelado" }
]
}
}
},
{
"id": "dynamodb_1",
"type": "dynamodb_update",
"position": { "x": 700, "y": 200 },
"data": {
"label": "Atualizar Status",
"parameters": {
"accessKeyId": "AKIAIOSFODNN7EXAMPLE",
"secretAccessKey": "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
"region": "us-east-1",
"operation": "updateItem",
"tableName": "Pedidos",
"key": {
"orderId": "{{order_id}}"
},
"updateExpression": "SET #status = :newStatus, lastUpdate = :now, updatedBy = :user",
"expressionAttributeValues": {
":newStatus": "{{switch_1.selectedValue}}",
":now": "2025-10-12T10:45:00Z",
":user": "sistema"
}
}
}
},
{
"id": "message_2",
"type": "message",
"position": { "x": 850, "y": 200 },
"data": {
"label": "Confirmação",
"parameters": {
"message": "✅ Status Atualizado!\n\n🆔 Pedido: {{order_id}}\n📊 Novo Status: {{switch_1.selectedLabel}}\n⏰ Atualizado: 12/10/2025 10:45\n👤 Por: Sistema\n\nCliente será notificado."
}
}
},
{
"id": "end_1",
"type": "end",
"position": { "x": 1000, "y": 200 },
"data": { "label": "Fim" }
}
],
"edges": [
{ "source": "start_1", "target": "message_1" },
{ "source": "message_1", "target": "input_1" },
{ "source": "input_1", "target": "switch_1" },
{ "source": "switch_1", "target": "dynamodb_1" },
{ "source": "dynamodb_1", "target": "message_2" },
{ "source": "message_2", "target": "end_1" }
]
}
Saída esperada:
Sistema: 📦 Atualização de Pedido
Vamos atualizar o status.
Sistema: Digite o ID do pedido:
Usuário: PED-12345
Sistema: Atualizar para qual status?
1️⃣ Em Processamento
2️⃣ Enviado
3️⃣ Entregue
4️⃣ Cancelado
Usuário: 2
Sistema: ✅ Status Atualizado!
🆔 Pedido: PED-12345
📊 Novo Status: Enviado
⏰ Atualizado: 12/10/2025 10:45
👤 Por: Sistema
Cliente será notificado.
Resposta do Node
{
"success": true,
"attributes": {
"productId": "PROD123",
"estoque": 47,
"ultimaVenda": "2025-10-12T10:30:00Z",
"totalVendas": 153
}
}
Boas Práticas
✅ SIM:
- Use Update ao invés de Put quando modificar apenas alguns campos
- Faça operações atômicas (incremento/decremento) com
SET campo = campo + :val - Atualize timestamp de modificação em todas as updates
- Use ConditionExpression para evitar race conditions (ex: só decrementar se estoque > 0)
- Combine múltiplas operações em uma UpdateExpression
- Retorne novos valores com
ReturnValues: 'ALL_NEW'(no código) para confirmar
❌ NÃO:
- Usar Put quando só precisa atualizar 1-2 campos (desperdício de dados)
- Fazer read-modify-write manual (use operações atômicas)
- Esquecer de validar se item existe antes de atualizar
- Usar UpdateExpression complexa demais (divida em múltiplas operações)
- Atualizar chaves primárias (impossível - delete e reinsira ao invés)
Dicas
💡 Dica 1: Update é atômico! SET estoque = estoque - 1 é seguro mesmo com múltiplas execuções simultâneas - DynamoDB garante consistência.
💡 Dica 2: Use REMOVE campo para deletar atributo sem deletar o item inteiro. Útil para limpar dados temporários.
💡 Dica 3: ADD só funciona com números e sets. Para incrementar: ADD contador :inc. Para adicionar a set: ADD tags :newTag.
💡 Dica 4: Combine SET, REMOVE, ADD e DELETE em uma UpdateExpression: SET nome = :n REMOVE tempData ADD pontos :p.
💡 Dica 5: Use ConditionExpression para validações: SET estoque = estoque - :qty com condição estoque >= :qty previne estoque negativo.
Próximo Node
→ DYNAMODB_PUT - Inserir ou sobrescrever item completo → DYNAMODB_DELETE - Deletar item → DYNAMODB_GET - Buscar item para conferir atualização