Skip to content

Commit 9eef07b

Browse files
authored
Merge pull request #550 from pozeus/master
Adding GET, LIST, PUT operations to rest-api-typescript-dynamodb
2 parents d0a6a55 + 13f7920 commit 9eef07b

File tree

5 files changed

+202
-1
lines changed

5 files changed

+202
-1
lines changed

aws-node-typescript-rest-api-with-dynamodb/README.md

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,54 @@ then
2727
You can then run the compiler by running `tsc` in this directory. It will pull the settings from .tsconfig and extra @types
2828
from package.json. The output create.js file is what will be uploaded by serverless.
2929

30-
For brevity, I have just demonstrated this to match with the todos/create.js lambda function
30+
For brevity, I have just demonstrated this to match with the todos/create.js, todos/list.js, todos/get.js and todos/update.js lambda function
31+
32+
## Usage
33+
34+
You can create, retrieve, update, or delete todos with the following commands:
35+
36+
### Create a Todo
37+
38+
```bash
39+
curl -X POST https://XXXXXXX.execute-api.us-east-1.amazonaws.com/dev/todos --data '{ "text": "Learn Serverless" }'
40+
```
41+
42+
Example Result:
43+
```bash
44+
{"text":"Learn Serverless","id":"ee6490d0-aa11e6-9ede-afdfa051af86","createdAt":1479138570824,"checked":false,"updatedAt":1479138570824}%
45+
```
46+
47+
### List all Todos
48+
49+
```bash
50+
curl https://XXXXXXX.execute-api.us-east-1.amazonaws.com/dev/todos
51+
```
52+
53+
Example output:
54+
```bash
55+
[{"text":"Deploy my first service","id":"ac90feaa11e6-9ede-afdfa051af86","checked":true,"updatedAt":1479139961304},{"text":"Learn Serverless","id":"206793aa11e6-9ede-afdfa051af86","createdAt":1479139943241,"checked":false,"updatedAt":1479139943241}]%
56+
```
57+
58+
### Get one Todo
59+
60+
```bash
61+
# Replace the <id> part with a real id from your todos table
62+
curl https://XXXXXXX.execute-api.us-east-1.amazonaws.com/dev/todos/<id>
63+
```
64+
65+
Example Result:
66+
```bash
67+
{"text":"Learn Serverless","id":"ee6490d0-aa11e6-9ede-afdfa051af86","createdAt":1479138570824,"checked":false,"updatedAt":1479138570824}%
68+
```
69+
70+
### Update a Todo
71+
72+
```bash
73+
# Replace the <id> part with a real id from your todos table
74+
curl -X PUT https://XXXXXXX.execute-api.us-east-1.amazonaws.com/dev/todos/<id> --data '{ "text": "Learn Serverless", "checked": true }'
75+
```
76+
77+
Example Result:
78+
```bash
79+
{"text":"Learn Serverless","id":"ee6490d0-aa11e6-9ede-afdfa051af86","createdAt":1479138570824,"checked":true,"updatedAt":1479138570824}%
80+
```

aws-node-typescript-rest-api-with-dynamodb/serverless.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,30 @@ functions:
2828
method: post
2929
cors: true
3030

31+
list:
32+
handler: todos/list.list
33+
events:
34+
- http:
35+
path: todos
36+
method: get
37+
cors: true
38+
39+
get:
40+
handler: todos/get.get
41+
events:
42+
- http:
43+
path: todos/{id}
44+
method: get
45+
cors: true
46+
47+
update:
48+
handler: todos/update.update
49+
events:
50+
- http:
51+
path: todos/{id}
52+
method: put
53+
cors: true
54+
3155
resources:
3256
Resources:
3357
TodosDynamoDbTable:
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict';
2+
3+
import { DynamoDB } from 'aws-sdk'
4+
5+
const dynamoDb = new DynamoDB.DocumentClient()
6+
7+
8+
module.exports.get = (event, context, callback) => {
9+
const params = {
10+
TableName: process.env.DYNAMODB_TABLE,
11+
Key: {
12+
id: event.pathParameters.id,
13+
},
14+
};
15+
16+
// fetch todo from the database
17+
dynamoDb.get(params, (error, result) => {
18+
// handle potential errors
19+
if (error) {
20+
console.error(error);
21+
callback(null, {
22+
statusCode: error.statusCode || 501,
23+
headers: { 'Content-Type': 'text/plain' },
24+
body: 'Couldn\'t fetch the todo item.',
25+
});
26+
return;
27+
}
28+
29+
// create a response
30+
const response = {
31+
statusCode: 200,
32+
body: JSON.stringify(result.Item),
33+
};
34+
callback(null, response);
35+
});
36+
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict';
2+
3+
import { DynamoDB } from 'aws-sdk'
4+
5+
const dynamoDb = new DynamoDB.DocumentClient()
6+
const params = {
7+
TableName: process.env.DYNAMODB_TABLE,
8+
};
9+
10+
module.exports.list = (event, context, callback) => {
11+
// fetch all todos from the database
12+
// For production workloads you should design your tables and indexes so that your applications can use Query instead of Scan.
13+
dynamoDb.scan(params, (error, result) => {
14+
// handle potential errors
15+
if (error) {
16+
console.error(error);
17+
callback(null, {
18+
statusCode: error.statusCode || 501,
19+
headers: { 'Content-Type': 'text/plain' },
20+
body: 'Couldn\'t fetch the todo items.',
21+
});
22+
return;
23+
}
24+
25+
// create a response
26+
const response = {
27+
statusCode: 200,
28+
body: JSON.stringify(result.Items),
29+
};
30+
callback(null, response);
31+
});
32+
};
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
'use strict';
2+
3+
const AWS = require('aws-sdk'); // eslint-disable-line import/no-extraneous-dependencies
4+
5+
const dynamoDb = new AWS.DynamoDB.DocumentClient();
6+
7+
module.exports.update = (event, context, callback) => {
8+
const timestamp = new Date().getTime();
9+
const data = JSON.parse(event.body);
10+
11+
// validation
12+
if (typeof data.text !== 'string' || typeof data.checked !== 'boolean') {
13+
console.error('Validation Failed');
14+
callback(null, {
15+
statusCode: 400,
16+
headers: { 'Content-Type': 'text/plain' },
17+
body: 'Couldn\'t update the todo item.',
18+
});
19+
return;
20+
}
21+
22+
const params = {
23+
TableName: process.env.DYNAMODB_TABLE,
24+
Key: {
25+
id: event.pathParameters.id,
26+
},
27+
ExpressionAttributeNames: {
28+
'#todo_text': 'text',
29+
},
30+
ExpressionAttributeValues: {
31+
':text': data.text,
32+
':checked': data.checked,
33+
':updatedAt': timestamp,
34+
},
35+
UpdateExpression: 'SET #todo_text = :text, checked = :checked, updatedAt = :updatedAt',
36+
ReturnValues: 'ALL_NEW',
37+
};
38+
39+
// update the todo in the database
40+
dynamoDb.update(params, (error, result) => {
41+
// handle potential errors
42+
if (error) {
43+
console.error(error);
44+
callback(null, {
45+
statusCode: error.statusCode || 501,
46+
headers: { 'Content-Type': 'text/plain' },
47+
body: 'Couldn\'t fetch the todo item.',
48+
});
49+
return;
50+
}
51+
52+
// create a response
53+
const response = {
54+
statusCode: 200,
55+
body: JSON.stringify(result.Attributes),
56+
};
57+
callback(null, response);
58+
});
59+
};

0 commit comments

Comments
 (0)