-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamodb.js
47 lines (40 loc) · 992 Bytes
/
dynamodb.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
const AWS = require('aws-sdk');
const TableName = 'Hackday.Gamification';
const docClient = new AWS.DynamoDB.DocumentClient({ apiVersion: '2012-08-10' });
const getUser = (github) => docClient.get({
TableName,
Key: { github },
}).promise();
const createUser = (github) => docClient.put({
TableName,
Item: {
github,
pushes: 0,
pullRequests: 0,
},
}).promise();
/**
* Check if user exists, if not will create a new user with the github
* @param {String} github github of the user
*/
const userHandler = async (github) => {
const user = await getUser(github);
if (!user.Item) { // not exists
await createUser(github);
}
};
const increaseAttributeByOne = (github, attribute) => docClient.update({
TableName,
Key: { github },
UpdateExpression: 'set #k= #k + :val',
ExpressionAttributeNames: {
'#k': attribute,
},
ExpressionAttributeValues: {
':val': 1,
},
}).promise();
module.exports = {
userHandler,
increaseAttributeByOne,
};