Created
June 13, 2020 12:56
-
-
Save AndrewBestbier/6714b3a1007e398f97959f0982fe474a to your computer and use it in GitHub Desktop.
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
const AWS = require("aws-sdk"); | |
const crypto = require("crypto"); | |
// Generate unique id with no external dependencies | |
const generateUUID = () => crypto.randomBytes(16).toString("hex"); | |
// Initialising the DynamoDB SDK | |
const documentClient = new AWS.DynamoDB.DocumentClient(); | |
exports.handler = async event => { | |
const { postCode } = JSON.parse(event.body); | |
const params = { | |
TableName: "post-codes", | |
Item: { | |
id: generateUUID(), | |
postCode: postCode | |
} | |
}; | |
try { | |
const data = await documentClient.put(params).promise(); | |
const response = { | |
statusCode: 200 | |
}; | |
return response; // Returning a 200 if the item has been inserted | |
} catch (e) { | |
return { | |
statusCode: 500, | |
body: JSON.stringify(e) | |
}; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment