Skip to content

Commit

Permalink
fix local dynamodb tables access
Browse files Browse the repository at this point in the history
  • Loading branch information
Mirco Cipriani committed Jul 5, 2021
1 parent f769fdf commit f736071
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 11 deletions.
19 changes: 19 additions & 0 deletions backend/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,24 @@
"internalConsoleOptions": "neverOpen",
"port": 9229
},
{
"type": "node",
"request": "attach",
"name": "Attach to Docker",
// "preLaunchTask": "tsc-watch",
"protocol": "auto",
"port": 9232,
"restart": true,
"localRoot": "${workspaceFolder}",
"remoteRoot": "/home/node/app",
"outFiles": [
"${workspaceFolder}/build/**/*.js"
],
"smartStep": true,
"skipFiles": [
"<node_internals>/**",
"node_modules/**"
]
},
]
}
3 changes: 2 additions & 1 deletion backend/docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ services:
- ../.env
environment:
- DYNAMODB_ENDPOINT=http://dynamodb:8000
- DYNAMODB_REGION=eu-central-1
- AWS_ACCESS_KEY_ID=local
- AWS_SECRET_ACCESS_KEY=local
- AWS_CONFIG_FILE=
- AWS_DEFAULT_REGION=local
- AWS_REGION=eu-central-1
build:
context: ..
dockerfile: $PWD/Dockerfile
Expand Down
2 changes: 1 addition & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"test": "jest",
"open:doc": "node -r dotenv/config -e \"const o = require('open'); o(['http://', process.env.HTTP_HOST, ':', process.env.HTTP_PORT, '/api/v1/doc'].join(''))\"",
"seed": "ts-node test/seed.ts",
"open:dynamo:server": "AWS_REGION=local AWS_ACCESS_KEY_ID=local AWS_SECRET_ACCESS_KEY=local DYNAMO_ENDPOINT=http://127.0.0.1:8000 dynamodb-admin",
"open:dynamo:server": "AWS_REGION=eu-central-1 AWS_ACCESS_KEY_ID=local AWS_SECRET_ACCESS_KEY=local DYNAMO_ENDPOINT=http://127.0.0.1:8000 dynamodb-admin",
"open:dynamo:link": "node -e \"const o = require('open'); o('http://0.0.0.0:8001/')\"",
"open:dynamo": "concurrently npm:open:dynamo:*",
"gen:jsonschema": "typescript-json-schema --id app ./tsconfig.json --include './src/typings/model/*.ts' --required --out src/schema.json '*'"
Expand Down
13 changes: 8 additions & 5 deletions backend/src/services/dynamodb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@ export class DynamoDB {
private documentClient: ddb.DocumentClient;

constructor(private logger: Logger, private options: DynamoDBOptions) {
// AWS.config.update({
// region: options.region,
// });
AWS.config.update({
region: options.region,
accessKeyId: "local", // FIXME: get from conf
secretAccessKey: "local", // FIXME: get from conf
});
// instantiate DocumentClient
this.documentClient = new ddb.DocumentClient({
region: options.region,
endpoint: options.endpoint,
});
this.table = this.getTable(options.tableName);
this.logger.info({ options }, `Init done ${JSON.stringify(options)}`);
}

private getCommonAttributes(): EntityAttributes {
Expand All @@ -38,14 +41,14 @@ export class DynamoDB {

private handleAlreadyExistsError(err: any, duplicateItem: string): never {
if (err.code === "ConditionalCheckFailedException") {
this.logger.info(`Item already exists: ${duplicateItem}`, { err, duplicateItem });
this.logger.info({ err, duplicateItem }, `Item already exists: ${duplicateItem}`);
throw new AlreadyExistsError({ err, duplicateItem });
}
throw err;
}
private handleNotFoundError(err: any, itemKey: string): never {
if (err.code === "ConditionalCheckFailedException") {
this.logger.info(`Item not found: ${itemKey}`, { err, itemKey });
this.logger.info( { err, itemKey }, `Item not found: ${itemKey}`);
throw new NotFoundError(
{ cause: NotFoundErrorCause.OBJECT_NOT_FOUND },
{ err, itemKey },
Expand Down
1 change: 1 addition & 0 deletions backend/src/utils/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const pinoLogger = pino({
// }
},
enabled: !isInTestRunner(FeatureDisableInTest.LOGGING),
level: "debug",
});

export function getLogger(module: string): pino.Logger {
Expand Down
2 changes: 1 addition & 1 deletion backend/test/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

export const testTableName = `test_dentist`;
export const dynamoEndpoint = "http://localhost:8000";
export const dynamoRegion = "local";
export const dynamoRegion = "eu-central-1";
7 changes: 6 additions & 1 deletion backend/test/table.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { DynamoDB } from "aws-sdk";
import AWS, { DynamoDB } from "aws-sdk";

export async function createTable(tableName: string, region: string, endpoint: string) {
AWS.config.update({
region,
accessKeyId: "local",
secretAccessKey: "local",
});
const dyn = new DynamoDB({ region, endpoint });
try {
await dyn
Expand Down
3 changes: 1 addition & 2 deletions frontend/src/app/components/LoginForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,11 @@ export function LoginForm() {

export const loginErrorText = (error: LoginErrorType) => {
switch (error) {
case LoginErrorType.RESPONSE_ERROR:
return 'Server error 😞';
case LoginErrorType.USERNAME_EMPTY:
return 'Type your username';
case LoginErrorType.PASSWORD_EMPTY:
return 'Type your password';
case LoginErrorType.RESPONSE_ERROR:
case LoginErrorType.INVALID_CREDENTIAL:
return 'Invalid credentials 🥺';
default:
Expand Down

0 comments on commit f736071

Please sign in to comment.