Created
August 16, 2019 19:38
-
-
Save ahmetkucukoglu/abf8571f6f71805e5f94a40e5df03e91 to your computer and use it in GitHub Desktop.
Update 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.update = async event => { | |
const json = JSON.parse(event.body); | |
const params = { | |
TableName: process.env.TABLE_NAME, | |
Key: { | |
'id': event.pathParameters.id | |
}, | |
UpdateExpression: 'set #namek = :namev, description = :description, price = :price', | |
ConditionExpression: 'id = :id', | |
ExpressionAttributeNames: { | |
'#namek': 'name' | |
}, | |
ExpressionAttributeValues: { | |
':id': event.pathParameters.id, | |
':namev': json.name, | |
':description': json.description, | |
':price': json.price | |
} | |
}; | |
return new Promise((resolve, reject) => { | |
dynamoDbClient.update(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