-
Notifications
You must be signed in to change notification settings - Fork 235
/
Copy pathindex.js
63 lines (56 loc) · 1.48 KB
/
index.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*global require, module*/
var ApiBuilder = require('claudia-api-builder'),
AWS = require('aws-sdk'),
api = new ApiBuilder(),
dynamoDb = new AWS.DynamoDB.DocumentClient();
module.exports = api;
// Create new user
api.post('/user', function (request) {
'use strict';
var params = {
TableName: request.env.tableName,
Item: {
userid: request.body.userId,
name: request.body.name,
age: request.body.age
}
};
// return dynamo result directly
return dynamoDb.put(params).promise();
}, { success: 201 }); // Return HTTP status 201 - Created when successful
// get user for {id}
api.get('/user/{id}', function (request) {
'use strict';
var id, params;
// Get the id from the pathParams
id = request.pathParams.id;
params = {
TableName: request.env.tableName,
Key: {
userid: id
}
};
// post-process dynamo result before returning
return dynamoDb.get(params).promise().then(function (response) {
return response.Item;
});
});
// delete user with {id}
api.delete('/user/{id}', function (request) {
'use strict';
var id, params;
// Get the id from the pathParams
id = request.pathParams.id;
params = {
TableName: request.env.tableName,
Key: {
userid: id
}
};
// return a completely different result when dynamo completes
return dynamoDb.delete(params).promise()
.then(function () {
return 'Deleted user with id "' + id + '"';
});
}, {success: { contentType: 'text/plain'}});
api.addPostDeployConfig('tableName', 'DynamoDB Table Name:', 'configure-db');