I'm trying to use the AWS SDK for JS to set a DynamoDB item's attribute Password of type String to a string representation of an integer. This was my initial approach:
import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
import {
UpdateCommand,
DynamoDBDocumentClient,
} from '@aws-sdk/lib-dynamodb';
const dynamoClient = new DynamoDBClient({
credentials: {
accessKeyId: process.env.MY_AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.MY_AWS_SECRET_ACCESS_KEY,
},
region: 'us-east-2',
});
const dynamoDocClient = DynamoDBDocumentClient.from(dynamoClient);
const key = { Email: lorem.ipsum@email.com };
dynamoInput.Key = key;
const attr = 'Password';
const attrVal = 456789;
dynamoInput.UpdateExpression = `SET ${attr} = ${attrVal}`;
dynamoDocClient.send(new UpdateCommand(dynamoInput));
But this yields the error ValidationException: Invalid UpdateExpression: Syntax error; token: "456789", near: "= 456789"
. I thought the issue might be that I was setting an attribute of type String to an integer, rather than a string representation of that integer. So I tried enclosing the integer 456789 in quotes of various kinds within the update expression. But apparently quote tokens are also syntactically illegal to directly include in update expressions. This led me to resort to the use of an expression attribute value as a placeholder for my string representation.
const attr = 'Password';
const attrVal = 456789;
dynamoInput.ExpressionAttributeValues = { ':v': `${attrVal}` };
dynamoInput.UpdateExpression = `SET ${attr} = :v`;
But this just gave me another error: ValidationException: The provided key element does not match the schema
.
DocumentClient is not doing anything special, it's simply setting the values as it's Inferred from JavaScript variables.
const attr = 'Password';
const attrVal = "456789";
dynamoInput.ExpressionAttributeValues = { ':v': attrVal };
dynamoInput.ExpressionAttributeNames={"#p":attr}
dynamoInput.UpdateExpression = "SET #p = :v";