Skip to content

Commit

Permalink
feat: add list all to lambda crud template (#13116)
Browse files Browse the repository at this point in the history
  • Loading branch information
sobolk authored Aug 15, 2023
1 parent 5de4098 commit 0fde28a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
11 changes: 11 additions & 0 deletions packages/amplify-e2e-tests/src/__tests__/function_2d.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,17 @@ describe('nodejs', () => {
expect(JSON.parse(response.Payload.toString()).statusCode).toEqual(200);
expect(JSON.parse(JSON.parse(response.Payload.toString()).body)).toEqual(item2);

response = await functionCloudInvoke(projRoot, {
funcName,
payload: JSON.stringify({
path: '/item',
httpMethod: 'GET',
}),
});
expect(JSON.parse(response.Payload.toString()).statusCode).toEqual(200);
expect(JSON.parse(JSON.parse(response.Payload.toString()).body)).toContainEqual(item1);
expect(JSON.parse(JSON.parse(response.Payload.toString()).body)).toContainEqual(item2);

response = await functionCloudInvoke(projRoot, {
funcName,
payload: JSON.stringify({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ See the License for the specific language governing permissions and limitations
<%= props.topLevelComment %>

const { DynamoDBClient } = require('@aws-sdk/client-dynamodb');
const { DeleteCommand, DynamoDBDocumentClient, GetCommand, PutCommand, QueryCommand, } = require('@aws-sdk/lib-dynamodb');
const { DeleteCommand, DynamoDBDocumentClient, GetCommand, PutCommand, QueryCommand, ScanCommand } = require('@aws-sdk/lib-dynamodb');
const awsServerlessExpressMiddleware = require('aws-serverless-express/middleware')
const bodyParser = require('body-parser')
const express = require('express')
Expand Down Expand Up @@ -55,9 +55,28 @@ const convertUrlType = (param, type) => {
}
}

/********************************
* HTTP Get method for list objects *
********************************/
/************************************
* HTTP Get method to list objects *
************************************/

app.get(path, async function(req, res) {
var params = {
TableName: tableName,
Select: 'ALL_ATTRIBUTES',
};

try {
const data = await ddbDocClient.send(new ScanCommand(params));
res.json(data.Items);
} catch (err) {
res.statusCode = 500;
res.json({error: 'Could not load items: ' + err.message});
}
});

/************************************
* HTTP Get method to query objects *
************************************/

app.get(path + hashKeyPath, async function(req, res) {
const condition = {}
Expand Down

0 comments on commit 0fde28a

Please sign in to comment.