Skip to content

Commit ff0e65b

Browse files
committedOct 12, 2017
limit parameter
1 parent c86df53 commit ff0e65b

File tree

1 file changed

+34
-21
lines changed

1 file changed

+34
-21
lines changed
 

‎api/list.js

+34-21
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,43 @@
33
const AWS = require('aws-sdk'); // eslint-disable-line import/no-extraneous-dependencies
44

55
const dynamoDb = new AWS.DynamoDB.DocumentClient();
6-
const params = {
6+
var params = {
77
TableName: process.env.POST_TABLE,
8+
Limit: 10
89
};
910

1011
module.exports.list = (event, context, callback) => {
1112

12-
// fetch all posts from the database
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 posts.',
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-
});
13+
if( event.queryStringParameters !== null && typeof event.queryStringParameters === 'object' ) {
14+
15+
if( event.queryStringParameters.hasOwnProperty('limit') ) {
16+
17+
params.Limit = event.queryStringParameters.limit;
18+
}
19+
}
20+
21+
// fetch all posts from the database
22+
dynamoDb.scan(params, (error, result) => {
23+
24+
// handle potential errors
25+
if (error) {
26+
27+
console.error(error);
28+
callback(null, {
29+
statusCode: error.statusCode || 501,
30+
headers: { 'Content-Type': 'text/plain' },
31+
body: 'Couldn\'t fetch the posts.',
32+
});
33+
34+
return;
35+
}
36+
37+
// create a response
38+
const response = {
39+
statusCode: 200,
40+
body: JSON.stringify(result.Items),
41+
};
42+
43+
callback(null, response);
44+
});
3245
};

0 commit comments

Comments
 (0)