Created
July 17, 2024 21:34
-
-
Save CharlieGreenman/19264e128fb7b41cf8e84f915f2da067 to your computer and use it in GitHub Desktop.
Atomic counter using AWS, Typescript/Node + AWS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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