Skip to content

Instantly share code, notes, and snippets.

@bibhuticoder
Created August 17, 2020 03:48
Show Gist options
  • Save bibhuticoder/8f37c06482b550279c27d35e55f3df06 to your computer and use it in GitHub Desktop.
Save bibhuticoder/8f37c06482b550279c27d35e55f3df06 to your computer and use it in GitHub Desktop.
// 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