Deployable hello function behind api gateway

This commit is contained in:
Ben Ramey
2018-09-18 06:50:29 -05:00
parent 5437fe88b0
commit 435060a764
6 changed files with 176 additions and 0 deletions

61
services/db.js Normal file
View File

@@ -0,0 +1,61 @@
const dynamoClient = require('../dynamoClient');
const create = async (tableName, item) => {
const fullItem = {...item, created_at: Date.now()};
await dynamoClient.put({
TableName: tableName,
Item: fullItem
}).promise();
// put does not return saved item, just return our object
return fullItem;
};
const get = async (tableName, keyMap) => {
const res = await dynamoClient.get({
TableName: tableName,
Key: keyMap
}).promise();
return res && res.Item;
};
const queryByID = async (tableName, idKeyName, id) => {
// querying by id can be used when we are querying a table
// that has a sortKey, but we don't need to use the sortKey
const res = await dynamoClient.query({
TableName: "gt_answers",
KeyConditionExpression: "#id = :idValue",
ExpressionAttributeNames: {
"#id": idKeyName,
},
ExpressionAttributeValues: {
":idValue": id,
}
}).promise();
return res && res.Items;
};
const update = async (tableName, keyMap, ue, eav) => {
const res = await dynamoClient.update({
TableName: tableName,
Key: keyMap,
UpdateExpression: ue,
ExpressionAttributeValues: eav,
ReturnValues: 'UPDATED_NEW'
}).promise();
console.log('update res', res);
return res;
};
module.exports = {
client: dynamoClient,
create,
get,
queryByID,
update
};