With your data loaded, you can use the Data API to do something more complex than the Select 1 query you did in the last module.
A common access pattern is to fetch the highest scores for a user. You can try that here.
Look at the code in scripts/fetchHighScoresForUser.js. This code contains the internal method your application calls to fetch the highest scores for a particular user. The code looks as follows:
const AWS = require('aws-sdk')
const rdsdataservice = new AWS.RDSDataService();
const fetchHighScoresForUser= async (username, count) => {
const params = {
resourceArn: process.env.DATABASE_ARN,
secretArn: process.env.SECRET_ARN,
database: 'leaderboard',
includeResultMetadata: true,
sql: 'SELECT game_id, username, gamedate, score, level FROM games WHERE username = :username ORDER BY score DESC LIMIT :count',
parameters: [
{
name: 'username',
value: { stringValue: username }
},
{
name: 'count',
value: { longValue: count }
}
]
}
const results = await rdsdataservice.executeStatement(params).promise()
return results
}
fetchHighScoresForUser(ubecker, 1).then((results) => console.log(JSON.stringify(results, null, 2)))
Your fetchHighScoresForUser function takes two arguments -- the username for whom you want to fetch high scores, and the number of records you want to fetch. It then makes a query using the Data API to fetch the top scores for that user.
At the bottom of the file is an example of using the fetchHighScoresForUser function by calling it with the username of ubecker and requesting only a single record.
Execute the script by running the following command in your terminal:
You should see the following output in your terminal:
{
"columnMetadata": [
{
"arrayBaseColumnType": 0,
"isAutoIncrement": true,
"isCaseSensitive": false,
"isCurrency": false,
"isSigned": true,
"label": "game_id",
"name": "game_id",
"nullable": 0,
"precision": 11,
"scale": 0,
"schemaName": "",
"tableName": "games",
"type": 4,
"typeName": "INT"
},
{
"arrayBaseColumnType": 0,
"isAutoIncrement": false,
"isCaseSensitive": false,
"isCurrency": false,
"isSigned": false,
"label": "username",
"name": "username",
"nullable": 0,
"precision": 50,
"scale": 0,
"schemaName": "",
"tableName": "games",
"type": 12,
"typeName": "VARCHAR"
},
{
"arrayBaseColumnType": 0,
"isAutoIncrement": false,
"isCaseSensitive": false,
"isCurrency": false,
"isSigned": false,
"label": "gamedate",
"name": "gamedate",
"nullable": 0,
"precision": 19,
"scale": 0,
"schemaName": "",
"tableName": "games",
"type": 93,
"typeName": "TIMESTAMP"
},
{
"arrayBaseColumnType": 0,
"isAutoIncrement": false,
"isCaseSensitive": false,
"isCurrency": false,
"isSigned": true,
"label": "score",
"name": "score",
"nullable": 0,
"precision": 11,
"scale": 0,
"schemaName": "",
"tableName": "games",
"type": 4,
"typeName": "INT"
},
{
"arrayBaseColumnType": 0,
"isAutoIncrement": false,
"isCaseSensitive": false,
"isCurrency": false,
"isSigned": true,
"label": "level",
"name": "level",
"nullable": 0,
"precision": 11,
"scale": 0,
"schemaName": "",
"tableName": "games",
"type": 4,
"typeName": "INT"
}
],
"numberOfRecordsUpdated": 0,
"records": [
[
{
"longValue": 101
},
{
"stringValue": "ubecker"
},
{
"stringValue": "2019-11-06 09:00:37"
},
{
"longValue": 9090
},
{
"longValue": 84
}
]
]
}
This output is pretty verbose. The Data API includes a lot of information about the result, including detailed column metadata for each column returned.
This information can be hard to parse in each data access method. In the next step, you use a utility method to wrap the Data API.