-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#319 - created the insert partiql query
- Loading branch information
1 parent
3539b42
commit 800b63c
Showing
7 changed files
with
75 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
File renamed without changes.
25 changes: 25 additions & 0 deletions
25
src/components/dynamodb/utils/stringify-data-before-save.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const config = { | ||
regionTableName: process.env.regionTableName, | ||
}; | ||
|
||
export { config }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |