3
3
const AWS = require ( 'aws-sdk' ) ; // eslint-disable-line import/no-extraneous-dependencies
4
4
5
5
const dynamoDb = new AWS . DynamoDB . DocumentClient ( ) ;
6
- const params = {
6
+ var params = {
7
7
TableName : process . env . POST_TABLE ,
8
+ Limit : 10
8
9
} ;
9
10
10
11
module . exports . list = ( event , context , callback ) => {
11
12
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
+ } ) ;
32
45
} ;
0 commit comments