Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. 'use strict';
  2. var aws = require('aws-sdk');
  3.  
  4. console.log('Loading function');
  5.  
  6. exports.handler = (event, context, callback) => {
  7. var cloudSearchClient = new aws.CloudSearchDomain({
  8. endpoint: 'doc-shop-domain-ft32cd2vtsf4r42cqim4oopvwe.us-east-1.cloudsearch.amazonaws.com'
  9. });
  10.  
  11. var cloudSearchDocuments = event.Records.map(function(record) {
  12. console.log('Processing record: ' + JSON.stringify(record));
  13. var id = record.dynamodb.Keys.id.S
  14. if (record.eventName == 'REMOVE') {
  15. return {
  16. id: id,
  17. type: 'delete'
  18. };
  19. }
  20.  
  21. var image = record.dynamodb.NewImage;
  22.  
  23. return {
  24. id: id,
  25. type: 'add',
  26. fields: {
  27. description: image.description.S,
  28. totalcomments: image.totalComments.N,
  29. version: image.version.N,
  30. totalrating: image.totalRating.N,
  31. id: image.id.S,
  32. name: image.name.S
  33. }
  34. };
  35. });
  36.  
  37. var uploadDocumentsParams = {
  38. contentType: 'application/json',
  39. documents : JSON.stringify(cloudSearchDocuments)
  40. };
  41.  
  42. cloudSearchClient.uploadDocuments(uploadDocumentsParams, function(err, data) {
  43. if(err) {
  44. console.log('Failed to process documents', err, err.stack);
  45. context.fail(err);
  46. } else {
  47. context.succeed("Synchronized " + event.Records.length + " records.");
  48. }
  49. });
  50. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement