I have a DynamoDB table which contains a list of players and their score. I need to create an AWS Lambda function that updates a player's score and returns their ranking. Currently my lambda writes the new score in the db, after which, it saves all the contents of the db in a variable "result", sorts it and executes a loop to find its place in the ranking in this way:
dynamoDb.scan(params, (error, result) => {
// handle potential errors
if (error) {
console.error(error);
callback(null, {
statusCode: error.statusCode || 501,
headers: { 'Content-Type': 'text/plain' },
body: 'Couldn\'t fetch the todos.',
});
return;
}
var userPosition = 1;
result.Items.sort(sortFunction);
for (let val of result.Items){
if (val.id === userId){
ranking = userPosition;
break
};
userPosition++;
}
Is there any smarter solution?