aws lambda - Confused on creating an update request for DynamoDB using API Gateway -
i'm writing lambda function resources posting phone number. below code.
exports.handle = function (e, ctx, cb) { var body = json.parse(e.body); var params = { tablename: 'useraddresses', filterexpression: '#phone = :phone', expressionattributenames: { "#phone": "phone" }, expressionattributevalues: { ":phone": body.phone } } dynamodb.scan(params).promise().then(function (data) { var uw = data.items[0]; var res = { "statuscode": 200, "headers": {}, "body": json.stringify(uw) }; ctx.succeed(res); }); }
this working fine. want same put , patch. can 1 please point me in right direction.
for patch, should like, phone
should passed queryparameter , body updated in json body
thanks
is phone number hash key? if so, use dynamodb.get() or dynamodb.query() instead...
here's quick , dirty example, may started dynamodb updates:
const aws = require('aws-sdk); const bluebird = require('bluebird'); aws.config.setpromisesdependency(bluebird); const dynamodb = new aws.dynamodb.documentclient(); let update = (id, attributes) => { var params = { tablename: 'mytablename', key: { myhashkeyattr: id }, attributeupdates: attributes }; return dynamodb.update(params).promise(); }; exports.handler = (event, context, callback) => { console.log(json.stringify(event, null, 2)); let body = json.parse(event.body); let attributes = { firstname: { action: 'put', value: body.firstname }, lastname: { action: 'put', value: body.lastname }, updatedat: { action: 'put', value: new date() } }; // if phone number hash key... update(event.querystringparameters.phone, attributes) .then( (result) => { console.log(result); callback({ statuscode: 200, headers: {}, body: json.stringify({message: 'updated!'}) }); } ).catch( (error) => { console.error(error); callback({ statuscode: 500, headers: {}, body: json.stringify({message: 'ops!'}) }); } ); }
wiki
Comments
Post a Comment