11'use strict' ;
22
33const AWS = require ( 'aws-sdk' ) ; // eslint-disable-line import/no-extraneous-dependencies
4-
54const dynamoDb = new AWS . DynamoDB . DocumentClient ( ) ;
5+
6+ /*
7+ var params = {
8+ TableName: process.env.DYNAMODB_TABLE,
9+ Limit: 10
10+ };
11+
612var params = {
7- TableName : process . env . POST_TABLE ,
8- Limit : 10
13+
14+ TableName: process.env.DYNAMODB_TABLE,
15+ IndexName: "UpdatedAtIndex",
16+ ScanIndexForward: true
917};
18+ */
19+
20+ const params = {
21+ TableName : process . env . DYNAMODB_TABLE ,
22+ Limit : 5 ,
23+ IndexName : 'UpdatedAtIndex' ,
24+ KeyConditionExpression : 'dummyHashKey = :x' ,
25+ ExpressionAttributeValues : {
26+ ':x' : 'OK'
27+ } ,
28+ ProjectionExpression : "id, userId, parentId, correctAnswer, title, body, createdAt, updatedAt"
29+ }
30+
1031
1132module . exports . list = ( event , context , callback ) => {
1233
34+ // Do we have any parameters?
35+ /*
1336 if( event.queryStringParameters !== null && typeof event.queryStringParameters === 'object' ) {
1437
15- if ( event . queryStringParameters . hasOwnProperty ( 'limit' ) ) {
38+ // Do we have a limit clause?
39+ if( event.queryStringParameters.hasOwnProperty('limit') ) params.Limit = event.queryStringParameters.limit;
1640
17- params . Limit = event . queryStringParameters . limit ;
18- }
41+ // Do we have a page number?
1942 }
43+ */
44+ dynamoDb . query ( params , function ( error , data ) {
45+
46+ // Handle potential errors
47+ if ( error ) {
48+
49+ console . error ( error ) ;
50+ callback ( null , {
51+ statusCode : error . statusCode || 501 ,
52+ headers : { 'Content-Type' : 'text/plain' } ,
53+ body : 'Couldn\'t fetch the posts.' ,
54+ } ) ;
55+
56+ return ;
57+ }
58+
59+ // Create a response
60+ const response = {
61+ statusCode : 200 ,
62+ body : JSON . stringify ( data ) ,
63+ } ;
64+
65+ callback ( null , response ) ;
66+
67+ } ) ;
2068
21- // fetch all posts from the database
69+ // Fetch all posts from the database
70+ /*
2271 dynamoDb.scan(params, (error, result) => {
2372
24- // handle potential errors
73+ // Handle potential errors
2574 if (error) {
2675
2776 console.error(error);
@@ -34,12 +83,13 @@ module.exports.list = (event, context, callback) => {
3483 return;
3584 }
3685
37- // create a response
86+ // Create a response
3887 const response = {
3988 statusCode: 200,
4089 body: JSON.stringify(result.Items),
4190 };
4291
4392 callback(null, response);
4493 });
94+ */
4595} ;
0 commit comments