Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// automatically appends updatedAt timestamp
const update = async (updateParams) => {
try {
const timestamp = new Date().getTime();
params.UpdateExpression = params.UpdateExpression + ', #updatedAt = :updatedAt';
params.ExpressionAttributeNames['#updatedAt'] = 'updatedAt';
params.ExpressionAttributeValues[':updatedAt'] = timestamp;
return await db.update(updateParams).promise();
}
catch (error) { throw error; }
};
var params = {
TableName: 'PRODUCTS_TABLE',
Key: { "id": someProductId },
ConditionExpression: 'id = :id AND createdBy = :createdBy',
UpdateExpression: 'set #data.#price = :updatedPrice',
ExpressionAttributeNames: { '#data': 'data', '#price': 'price' },
ExpressionAttributeValues: { ':id': someProductId, ':updatedPrice': 3456.78, ':createdBy': creatorId }
};
try {
await dynamoDB.update(params);
} catch (error) {
console.log(error);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment