Deployable hello function behind api gateway
This commit is contained in:
61
services/db.js
Normal file
61
services/db.js
Normal 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
|
||||
};
|
||||
Reference in New Issue
Block a user