Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
An example of working with dynamodb from nodejs.
/**
* Helper class that interacts with DynamoDB to read and write data.
*/
class DynamoDbManager {
constructor() {
const AWS = require("aws-sdk")
AWS.config.update({
region: "us-east-2",
endpoint: "dynamodb.us-east-2.amazonaws.com"
})
this.docClient = new AWS.DynamoDB.DocumentClient()
}
/**
* Pulls data from DynamoDB for previous levels' stats for the current session.
*
* @param {string} sessionId
* @returns {Promise<any>}
*/
getDynamoScoresForSession(sessionId) {
return new Promise((resolve, reject) => {
const params = {
TableName: process.env.DynamoScoresTable,
KeyConditionExpression: 'sessionId= :sessionId',
ExpressionAttributeValues: {
':sessionId': sessionId
}
}
this.docClient.query(params, function (err, data) {
if (err) reject(err)
else resolve(data)
})
})
}
/**
* Pulls data from DynamoDB for the number of levels in a specified session.
*
* @param {string} sessionId
* @returns {Promise<any>}
*/
getLevelsForSession(sessionId) {
return new Promise((resolve, reject) => {
const params = {
TableName: process.env.DynamoLevelsTable,
KeyConditionExpression: 'sessionId= :sessionId',
ExpressionAttributeValues: {
':sessionId': sessionId
}
}
this.docClient.query(params, function (err, data) {
if (err) reject(err)
else resolve(data)
})
})
}
/**
* Takes a DynamoDB table name and a item. Uses the DynamoDB "put" operation to save the item to the table.
*
* @param {string} table
* @param {} item
* @returns {Promise<any>}
*/
writeData(table, item) {
return new Promise((resolve, reject) => {
const params = {
ReturnValues: 'NONE',
TableName: table,
Item: item
}
this.docClient.put(params, (err, data) => {
if (err) reject(err)
else resolve(data)
})
})
}
saveLevelScore() {
return new Promise((resolve, reject) => {
resolve('chyeah')
})
}
/**
* Deletes all data in the DynamoLevelsTable and SessionLevels table for a given sessionId.
*
* @param previousLevelsSessionData
* @param lastLevelData
* @returns {Promise<any>}
*/
deleteAllDynamoSessionData(previousLevelsSessionData, lastLevelData) {
return new Promise((resolve, reject) => {
const scoresRankingsToDelete = []
previousLevelsSessionData.forEach(previousLevelData => {
scoresRankingsToDelete.push({
DeleteRequest: {
Key: {
'sessionId': previousLevelData.sessionId,
'levelId': previousLevelData.levelId
}
}
})
})
scoresRankingsToDelete.push({
DeleteRequest: {
Key: {
'sessionId': lastLevelData.session,
'levelId': lastLevelData.levelId
}
}
})
const sessionLevelsToDelete = [{
DeleteRequest: {
Key: {
'sessionId': lastLevelData.session
}
}
}]
const params = {
RequestItems: {
[process.env.DynamoScoresTable]: scoresRankingsToDelete,
[process.env.DynamoLevelsTable]: sessionLevelsToDelete
}
}
this.docClient.batchWrite(params, function (err, data) {
if (err) reject(err)
else resolve(data)
})
})
}
}
module.exports = new DynamoDbManager()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.