-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
30 lines (23 loc) · 785 Bytes
/
app.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
'use strict';
const AWS = require('aws-sdk');
const dynamo = new AWS.DynamoDB.DocumentClient();
exports.handler = async (event, context, callback) => {
const route = event.pathParameters.proxy;
const tableName = process.env.DYNAMODB_TABLE;
// Get the existing count for the route.
let value = (await dynamo.get({
TableName: tableName,
Key: { id: route }
}).promise()).Item;
let count = (value && value.count) || 0;
// Increment the count and write it back to dynamo DB.
await (dynamo.put({
TableName: tableName,
Item: { id: route, count: ++count }
})).promise();
console.log(`Got count ${count} for '${route}'`);
return {
statusCode: 200,
body: JSON.stringify({ route, count })
}
}