Skip to content

Commit

Permalink
#319 - created the insert partiql query
Browse files Browse the repository at this point in the history
  • Loading branch information
malaquiasdev committed Feb 28, 2021
1 parent 3539b42 commit 800b63c
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 15 deletions.
4 changes: 2 additions & 2 deletions serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ resources:
Properties:
TableName: ${self:custom.regionTableName}
AttributeDefinitions:
- AttributeName: id
- AttributeName: uf
AttributeType: S
KeySchema:
- AttributeName: id
- AttributeName: uf
KeyType: HASH
ProvisionedThroughput:
ReadCapacityUnits: ${self:custom.tableThroughput}
Expand Down
19 changes: 19 additions & 0 deletions src/components/dynamodb/querys/save-new-region.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import AWS from 'aws-sdk';
import { stringify } from '../utils/stringify-data-before-save';

const dynamoDB = new AWS.DynamoDB();

async function saveNewRegion(tableName, data) {
const insertQuery = `INSERT INTO "${tableName}" VALUE ${stringify(data)}`;
try {
return dynamoDB
.executeStatement({
Statement: insertQuery,
})
.promise();
} catch (error) {
throw error;
}
}

export { saveNewRegion };
25 changes: 25 additions & 0 deletions src/components/dynamodb/utils/stringify-data-before-save.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* eslint-disable max-depth */
/* eslint-disable complexity */
/* eslint-disable max-statements */
function stringify(value) {
const lastKey = Object.keys(value).pop();
let objString = '';
if (typeof value === 'object') {
objString += '{';
for (const key in value) {
objString += `'${key}':${stringify(value[key])}`;
if (key !== lastKey) {
objString += ',';
}
}
objString += '}';
} else if (typeof value === 'string') {
objString += `'${value.replace(/'/g, "''")}'`;
} else if (typeof value === 'number') {
objString += `${value}`;
} else if (typeof value === 'boolean') {
objString += `${value}`;
}
return objString;
}
export { stringify };
5 changes: 5 additions & 0 deletions src/functions/create-new-region/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const config = {
regionTableName: process.env.regionTableName,
};

export { config };
32 changes: 19 additions & 13 deletions src/functions/create-new-region/handler.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
import { config } from './config';
import { saveNewRegion } from '../../components/dynamodb/querys/save-new-region';
import { handlerSuccess } from '../../libs/http/response/handler-success';
import { handlerResponseError } from '../../libs/http/response/handler-error';
import { createNewRegion } from './use-case';

async function handlerCreateNewRegion(event) {
console.log('regionTableName', process.env.regionTableName);
console.log('event.body', event.body);
return {
statusCode: 200,
body: JSON.stringify(
{
message: 'Go Serverless v1.0! Your function executed successfully!',
input: event.body,
},
null,
2,
),
};
const {
body,
requestContext: { requestId },
} = event;
try {
const region = createNewRegion(JSON.parse(body), {
saveNewRegion,
tableName: config.regionTableName,
});
return handlerSuccess({ body: region }, requestId);
} catch (error) {
return handlerResponseError({ error }, requestId);
}
}

export { handlerCreateNewRegion };
5 changes: 5 additions & 0 deletions src/functions/create-new-region/use-case/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
async function createNewRegion({ uf, label, draft = true }, { saveNewRegion, tableName }) {
return saveNewRegion(tableName, { uf: uf, label, draft });
}

export { createNewRegion };

0 comments on commit 800b63c

Please sign in to comment.