Skip to content

Instantly share code, notes, and snippets.

@HerbertLim
Created September 13, 2019 04:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HerbertLim/0fc695ab231f15896861e7b4a632d6d8 to your computer and use it in GitHub Desktop.
Save HerbertLim/0fc695ab231f15896861e7b4a632d6d8 to your computer and use it in GitHub Desktop.
Get max available ID by using DynamoDB: increase an attribute value at a DynamoDB table and returns increased value.
// MY_META DynamoDB table has a primary key "name" and a attribute "value"
// Added a record with "lastUsedSid" as name, and a Number as value.
export function getNextValidSid (callback) {
const params = {
TableName: MY_META,
Key: {
'name': 'lastUsedSid',
},
UpdateExpression: 'set #v = #v + :val',
ExpressionAttributeValues: {
':val': 1
},
ExpressionAttributeNames: {
'#v': 'value',
},
ReturnValues: 'UPDATED_NEW',
}
docClient.update(params, (err, data) => {
if (err) {
console.error(`getNextValidSid: Unable to increase lastUsedSid's value at ${MISEBIG_META}. Error JSON:`, JSON.stringify(err, null, 2))
return callback(err)
}
console.log(`getNextValidSid: result: `, data)
callback(null, data.Attributes.value)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment