move files around

This commit is contained in:
Ben Ramey
2018-09-20 06:46:02 -05:00
parent 8eba063aef
commit 4e114f8de4
5 changed files with 8 additions and 10 deletions

View File

@@ -0,0 +1,13 @@
const AWS = require('aws-sdk');
// Set the region
AWS.config.update({region: 'us-east-2'});
// also set local endpoint while in development
if (process.env.NODE_ENV === 'development') {
AWS.config.update({endpoint: "http://localhost:8000"});
}
const client = new AWS.DynamoDB.DocumentClient({});
module.exports = client;

70
services/db/index.js Normal file
View File

@@ -0,0 +1,70 @@
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 scan = async (tableName) => {
const res = await dynamoClient.scan({
TableName: tableName
}).promise();
return res && res.Items;
};
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: tableName,
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,
scan,
queryByID,
update
};