1

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?

jarmod
  • 71,565
  • 16
  • 115
  • 122
G_Chillà
  • 11
  • 1
  • How many items are in this table for a given player ID? What is the key schema of this table? – jarmod Mar 25 '21 at 12:59

0 Answers0