Created
August 16, 2019 19:29
-
-
Save ahmetkucukoglu/677ea5553afb8e8ded91d7b97206143c to your computer and use it in GitHub Desktop.
Delete v2
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'); | |
const dynamoDbClient = new aws.DynamoDB.DocumentClient(); | |
module.exports.delete = async event => { | |
const params = { | |
TableName: process.env.TABLE_NAME, | |
Key: { | |
'id': event.pathParameters.id | |
}, | |
ConditionExpression: 'id = :id', | |
ExpressionAttributeValues: { | |
':id': event.pathParameters.id | |
} | |
}; | |
return new Promise((resolve, reject) => { | |
dynamoDbClient.delete(params, function (err, data) { | |
if (err) { | |
console.log(err); | |
if (err.code == 'ConditionalCheckFailedException') { | |
const response = { | |
statusCode: 404 | |
}; | |
resolve(response); | |
} | |
else { | |
reject(err); | |
} | |
} | |
else { | |
const response = { | |
statusCode: 200, | |
body: null | |
}; | |
resolve(response); | |
} | |
}); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment