Created
September 13, 2019 04:35
-
-
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.
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
// 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