/*```````````dbHelper.js`````````````*/
var AWS = require("aws-sdk");
AWS.config.update({
region: "us-east-1"
});
const tableName = "FORGETABLE_THINGS";
var dbHelper = function() {};
var docClient = new AWS.DynamoDB.DocumentClient();
dbHelper.prototype.addItem = (item, location, userID) => {
return new Promise((resolve, reject) => {
const params = {
TableName: tableName,
Item: {
'itemName': item,
'Location': location,
'userId': userID,
}
};
docClient.put(params, (err, data) => {
if (err) {
console.log("Unable to insert =>", JSON.stringify(err))
return reject("Unable to insert");
}
console.log("Saved Data, ", JSON.stringify(data));
resolve(data);
});
});
}
dbHelper.prototype.getItem = (userID) => {
return new Promise((resolve, reject) => {
const params = {
TableName: tableName,
KeyConditionExpression: "#userID = :user_id",
ExpressionAttributeNames: {
"#userID": "userId"
"itemName": "itemName"
},
ExpressionAttributeValues: {
":user_id": userID
}
}
docClient.query(params, (err, data) => {
if (err) {
console.error("Unable to read item. Error JSON:", JSON.stringify(err, null, 2));
return reject(JSON.stringify(err, null, 2))
}
console.log("GetItem succeeded:", JSON.stringify(data, null, 2));
resolve(data.Items)
})
});
}
dbHelper.prototype.removeItem = (item, userID) => {
return new Promise((resolve, reject) => {
const params = {
TableName: tableName,
Key: {
"userId": userID,
"itemName": item
},
ConditionExpression: "attribute_exists(itemName)"
}
docClient.delete(params, function(err, data) {
if (err) {
console.error("Unable to delete item. Error JSON:", JSON.stringify(err, null, 2));
return reject(JSON.stringify(err, null, 2));
}
console.log(JSON.stringify(err));
console.log("DeleteItem succeeded:", JSON.stringify(data, null, 2));
resolve();
})
});
};
module.exports = new dbHelper();