Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save CharlieGreenman/19264e128fb7b41cf8e84f915f2da067 to your computer and use it in GitHub Desktop.
Save CharlieGreenman/19264e128fb7b41cf8e84f915f2da067 to your computer and use it in GitHub Desktop.
Atomic counter using AWS, Typescript/Node + AWS
// ATOMIC COUNTER
// no other operations can happen before or after state of the data being modified
const updateParams: DocumentClient.UpdateItemInput = {
TableName: GENERIC_TABLE,
Key: { pk: `ORG#${organizationId}`, sk: `USER#${userId}` },
ReturnValues: 'UPDATED_NEW',
UpdateExpression: 'SET #itemCounter = if_not_exists(#itemCounter, :start) + :inc, #activeItems = if_not_exists(#activeItems, :start) + :inc',
ExpressionAttributeNames: {
'#itemCounter': 'itemCounter',
'#activeItems': 'activeItems'
},
ExpressionAttributeValues: {
':inc': 1,
':start': 0
}
};
const updateResult = await dynamo.update(updateParams).promise();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment