Skip to content

Instantly share code, notes, and snippets.

@ahmetkucukoglu
Created August 16, 2019 19:38
Show Gist options
  • Save ahmetkucukoglu/abf8571f6f71805e5f94a40e5df03e91 to your computer and use it in GitHub Desktop.
Save ahmetkucukoglu/abf8571f6f71805e5f94a40e5df03e91 to your computer and use it in GitHub Desktop.
Update v2
'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