Last active
June 23, 2019 18:14
-
-
Save sabha/4509778870faea590147be04fd6d4f1f to your computer and use it in GitHub Desktop.
ServerLess using Lambda - DynamoDB
This file contains hidden or 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
'use strict' | |
const AWS = require('aws-sdk'); | |
exports.handler = async (event, context) => { | |
const documentClient = new AWS.DynamoDB.DocumentClient(); | |
let responseBody = ""; | |
let statusCode = 0; | |
const { id } = event.pathParameters; | |
const params = { | |
TableName: "CallDetails", | |
Key: { | |
id: id, | |
} | |
} | |
try { | |
const data = await documentClient.delete(params).promise(); | |
responseBody = JSON.stringify(data); | |
statusCode = 204; | |
} catch(err) { | |
responseBody = `Unable to delete call details: ${err}`; | |
statusCode = 403; | |
} | |
const response = { | |
statusCode: statusCode, | |
headers: { | |
"Content-Type": "application/json", | |
"access-control-allow-origin": "*" | |
}, | |
body: responseBody | |
} | |
return response; | |
} |
This file contains hidden or 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
'use strict' | |
const AWS = require('aws-sdk'); | |
exports.handler = async (event, context) => { | |
const documentClient = new AWS.DynamoDB.DocumentClient(); | |
let responseBody = ""; | |
let statusCode = 0; | |
const params = { | |
TableName: "CallDetails" | |
} | |
try { | |
const data = await documentClient.scan(params).promise(); | |
responseBody = JSON.stringify(data.Items); | |
statusCode = 200; | |
} catch(err) { | |
responseBody = `Unable to get all call details: ${err}`; | |
statusCode = 403; | |
} | |
const response = { | |
statusCode: statusCode, | |
headers: { | |
"Content-Type": "application/json", | |
"access-control-allow-origin": "*" | |
}, | |
body: responseBody | |
} | |
return response; | |
} |
This file contains hidden or 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
'use strict' | |
const AWS = require('aws-sdk'); | |
exports.handler = async (event, context) => { | |
const documentClient = new AWS.DynamoDB.DocumentClient(); | |
let responseBody = ""; | |
let statusCode = 0; | |
const {id, memberid} = JSON.parse(event.body); | |
const params = { | |
TableName: "CallDetails", | |
Item: { | |
id: id, | |
memberid: memberid | |
} | |
} | |
try { | |
const data = await documentClient.put(params).promise(); | |
responseBody = JSON.stringify(data); | |
statusCode = 201; | |
} catch(err) { | |
responseBody = `Unable to put call details: ${err}`; | |
statusCode = 403; | |
} | |
const response = { | |
statusCode: statusCode, | |
headers: { | |
"Content-Type": "application/json", | |
"access-control-allow-origin": "*" | |
}, | |
body: responseBody | |
} | |
return response; | |
} |
This file contains hidden or 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
'use strict' | |
const AWS = require('aws-sdk'); | |
exports.handler = async (event, context) => { | |
const documentClient = new AWS.DynamoDB.DocumentClient(); | |
let responseBody = ""; | |
let statusCode = 0; | |
const {id, memberid} = JSON.parse(event.body); | |
const params = { | |
TableName: "CallDetails", | |
Key: { | |
id: id | |
}, | |
UpdateExpression: "set memberid = :n", | |
ExpressionAttributeValues: { | |
":n": memberid | |
}, | |
ReturnValues: "CALLDETAIL_UPDATE" | |
} | |
try { | |
const data = await documentClient.update(params).promise(); | |
responseBody = JSON.stringify(data); | |
statusCode = 201; | |
} catch(err) { | |
responseBody = `Unable to update call details: ${err}`; | |
statusCode = 403; | |
} | |
const response = { | |
statusCode: statusCode, | |
headers: { | |
"Content-Type": "application/json", | |
"access-control-allow-origin": "*" | |
}, | |
body: responseBody | |
} | |
return response; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
put (create) :
domain.com/callDetails/{id}
delete :
domain.com/callDetails/{id}
getAll :
domain.com/callDetails
update :
domain.com/callDetails/{id}