Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Session 2 : adding dynamo db #2

Open
wants to merge 3 commits into
base: session-2-v2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"devDependencies": {
"@types/aws-lambda": "8.10.17",
"@types/node": "10.12.18",
"@types/serverless": "^1.72.4",
"@types/serverless": "^1.78.36",
"@types/uuid": "3.4.5",
"@typescript-eslint/eslint-plugin": "2.0.0",
"@typescript-eslint/parser": "2.0.0",
Expand Down
23 changes: 12 additions & 11 deletions backend/serverless.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as AwsConfig from 'serverless/aws';

import ApiGatewayErrors from './resources/apiGatewayErrors';
import dynamodb from './resources/dynamodb';

const serverlessConfiguration: AwsConfig.Serverless = {
service: 'dojo-serverless-backend',
Expand All @@ -15,16 +16,16 @@ const serverlessConfiguration: AwsConfig.Serverless = {
stage: 'dev',
profile: 'dojo-serverless',
iamRoleStatements: [
// {
// Effect: 'Allow',
// Action: [
// 'dynamodb:Query',
// 'dynamodb:PutItem',
// 'dynamodb:DeleteItem',
// 'dynamodb:ListStreams',
// ],
// Resource: { 'Fn::GetAtt': ['DojoServerlessTable', 'Arn'] },
// },
{
Effect: 'Allow',
Action: [
'dynamodb:Query',
'dynamodb:PutItem',
'dynamodb:DeleteItem',
'dynamodb:ListStreams',
],
Resource: { 'Fn::GetAtt': ['DojoServerlessTable', 'Arn'] },
},
],
usagePlan: {
quota: {
Expand Down Expand Up @@ -66,7 +67,7 @@ const serverlessConfiguration: AwsConfig.Serverless = {
},
resources: {
Resources: {
...ApiGatewayErrors,
...ApiGatewayErrors, DojoServerlessTable: dynamodb,
},
},
};
Expand Down
23 changes: 13 additions & 10 deletions backend/src/handlers/virus/get.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { APIGatewayProxyHandler } from 'aws-lambda';
import uuid from 'uuid';
import { DynamoDB } from 'aws-sdk';

import { success } from '@libs/response';
import { failure, success } from '@libs/response';

export const main: APIGatewayProxyHandler = async () => {
const viruses = [
{ id: uuid() },
{ id: uuid() },
{ id: uuid() },
{ id: uuid() },
];
const documentClient = new DynamoDB.DocumentClient();

return success({ viruses });
export const main: APIGatewayProxyHandler = async () => {
return documentClient.query({TableName: "dojo-serverless-table"}, function(err, data) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tu peux faire sans la callback, avec un async/await et un try/catch

if (err) {
console.log("Error", err);
return failure({ err });
} else {
const viruses = data;
return success({ viruses });
}
});
};
8 changes: 4 additions & 4 deletions backend/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -493,10 +493,10 @@
"@types/tough-cookie" "*"
form-data "^2.5.0"

"@types/serverless@^1.72.4":
version "1.72.6"
resolved "https://registry.yarnpkg.com/@types/serverless/-/serverless-1.72.6.tgz#8fb8c8e313cde77f9722bb9c28b21a2c44f97bc2"
integrity sha512-ku1Ym85fHbTpN9zc8mJltH7lhyMxievrpgp+6jEcw9zFUXLpK8rsVCaZfcJBvIccDBAlvlBlO5Rb1NwciH/B/A==
"@types/serverless@^1.78.36":
version "1.78.36"
resolved "https://registry.yarnpkg.com/@types/serverless/-/serverless-1.78.36.tgz#a7cf3b22825caa815de845f215aa20ca9379ff1a"
integrity sha512-ciFBz5IPFuWKWlZH1ZKc+fYdE5Eo4MYrcavstuKWbQxpCKT9XIenoXQhmYtQ0PG166EjQ+OQz8qynMCJNykEsQ==

"@types/tough-cookie@*":
version "4.0.0"
Expand Down