I have a table called JuridicalPerson in my DynamoDB
var params = {
AttributeDefinitions: [{
AttributeName: 'code',
AttributeType: 'S'
}],
KeySchema: [{
AttributeName: 'code',
KeyType: 'HASH'
}],
ProvisionedThroughput: {
ReadCapacityUnits: 5,
WriteCapacityUnits: 5
},
TableName: 'JuridicalPerson'
}
I can save items there, but I can't update those items.
Sample of item's in my JuridicalPerson table
{
"code": {
"S": "jp_rJaHvVrzf"
},
"status": {
"S": "pending"
}
}
Update Expression
function updateDynamoDB (payload) {
const adhesionUpdate = Object.assign({}, payload)
return new Promise((resolve, reject) => {
const params = {
TableName: 'JuridicalPerson',
Key: {
'code': {
'S': adhesionUpdate.code
}
},
UpdateExpression: 'SET #status = :val1',
ExpressionAttributeNames: {
'#status': 'status'
},
ExpressionAttributeValues: {
':val1': { 'S': adhesionUpdate.status }
},
ReturnValues: 'ALL_NEW'
}
return dynamoAdapter.getState().update(params, (err, items) => {
if (err) {
return reject(err)
}
return resolve(items)
})
})
}
If I put a console.log before the Update just to see the params, we have
params: { TableName: 'JuridicalPerson',
Key: { code: { S: 'jp_rJaHvVrzf' } },
UpdateExpression: 'set #status = :val1',
ExpressionAttributeNames: { '#status': 'status' },
ExpressionAttributeValues: { ':val1': { S: 'active' } },
ReturnValues: 'ALL_NEW' }
But I got the following error
err: { ValidationException: Invalid attribute value type
message: 'Invalid attribute value type',
code: 'ValidationException',
time: 2017-12-18T12:40:39.488Z,
requestId: 'bc23aab1-d9a5-426f-a1af-3ff558e7e0fa',
statusCode: 400,
retryable: false,
retryDelay: 41.054909592801195 }
statusis defined as a key in any secondary index, and if so of any other data type than string?{S: 'some value'}form, but simply as'some value'. The documentation does however clearly state thatExpressionAttributeValuesshould be in the more verbose form, but if I were you I'd give it a try without the{S: ...}. Personally I have only been using the DocumentClient API since its release, mostly to get rid of the incredibly verbose syntax with all theseS:andN:...