简体   繁体   中英

Calling DynamoDB query and then writing result to s3 bucket: How to simplify this code with async await?

I have the following lambda function that reads from a DynamoDB table then writes the result to a spreadsheet on the S3 bucket.

It works fine but I'd like to improve it using async await. Is this possible?

'use strict';
const AWS = require('aws-sdk');
const S3 = new AWS.S3();
const excel = require('excel4node');
const workbook = new excel.Workbook();
const worksheet = workbook.addWorksheet('Sheet 1');
const db = new AWS.DynamoDB.DocumentClient({ apiVersion: '2012-08-10'});

const contactsTable = process.env.CONTACTS_TABLE;
const contactsBucket = process.env.CONTACTS_BUCKET;
const bucketRegion = process.env.REGION;

  module.exports.exportContactsByID = (event, _context, callback) => {
  const id = event.pathParameters.id;
  const params = {
    TableName: contactsTable,
    IndexName: "company_id-created_at-index",
    KeyConditionExpression: "company_id = :v_ID",
    ExpressionAttributeValues: {
      ":v_ID": id,
    },
    ProjectionExpression: "company_name, contact_name, age, phone, email, dsr_ratio, max_eligibility, created_at_local, selected_banks"
  };

return db.query(params).promise()
.then((res) => {
  const items = res.Items;
  if(res.Count === 0){
    return callback(null, response(200, `No records found for id ${id}`));
  }
  // Headers + Columns
  worksheet.cell(1, 1).string('Client').style({font: {bold: true}});
  worksheet.cell(1, 2).string('Name').style({font: {bold: true}});
  worksheet.cell(1, 3).string('Age').style({font: {bold: true}});
  worksheet.cell(1, 4).string('Email').style({font: {bold: true}});
  worksheet.cell(1, 5).string('Phone').style({font: {bold: true}});
  worksheet.cell(1, 6).string('DSR Ratio').style({font: {bold: true}});
  worksheet.cell(1, 7).string('Max Eligibility').style({font: {bold: true}});
  worksheet.cell(1, 8).string('Submission Date').style({font: {bold: true}});
  worksheet.cell(1, 9).string('Banks').style({font: {bold: true}});
  // Rows
  items.forEach((item, i) => {
    worksheet.cell(i + 2, 1).string(item.company_name);
    worksheet.cell(i + 2, 2).string(item.contact_name);
    worksheet.cell(i + 2, 3).string(item.age);
    worksheet.cell(i + 2, 4).string(item.email);
    worksheet.cell(i + 2, 5).string(item.phone);
    worksheet.cell(i + 2, 6).string(item.dsr_ratio);
    worksheet.cell(i + 2, 7).string(item.max_eligibility);
    worksheet.cell(i + 2, 8).string(item.created_at_local);
    worksheet.cell(i + 2, 9).string(item.selected_banks);
  });
  return workbook.writeToBuffer().then(buffer => {
    const params = {
      Bucket: contactsBucket,
      Key: `contacts/${id}.xlsx`,
      Body: buffer,
      ACL: 'public-read'
    }
    const fileURL = `https://${contactsBucket}.s3-${bucketRegion}.amazonaws.com/contacts/${id}.xlsx`;
    return S3.upload(params).promise().then(_data => {
      callback(null, response(200, fileURL));
});
  })
})
.catch((err) => callback(null, response(err.statusCode, err)));
};

The code above is the improved version from the original code I had earlier . Thank you for reading.

After reading this , I simplified the code as the following - by having multiple awaits

  module.exports.exportContactsByID = async (event, _context) => {
  const id = event.pathParameters.id;
  const params = {
    TableName: contactsTable,
    IndexName: "company_id-created_at-index",
    KeyConditionExpression: "company_id = :v_ID",
    ExpressionAttributeValues: {
      ":v_ID": id,
    },
    ProjectionExpression: "company_name, contact_name, age, phone, email, dsr_ratio, max_eligibility, created_at_local, selected_banks"
  };

try {
    const res = await db.query(params).promise();
    if(res.Count === 0){
      const response = {
        statusCode: 200,    
        headers: {    
          'Access-Control-Allow-Origin': '*',    
          'Access-Control-Allow-Credentials': true,    
        },
        body: JSON.stringify({
          status: `No records found for company id ${id}`,
        }),
      };  
      return response;
    }
    else {
    // Headers + Columns
    const items = res.Items;
    worksheet.cell(1, 1).string('Client').style({font: {bold: true}});
    worksheet.cell(1, 2).string('Name').style({font: {bold: true}});
    worksheet.cell(1, 3).string('Age').style({font: {bold: true}});
    worksheet.cell(1, 4).string('Email').style({font: {bold: true}});
    worksheet.cell(1, 5).string('Phone').style({font: {bold: true}});
    worksheet.cell(1, 6).string('DSR Ratio').style({font: {bold: true}});
    worksheet.cell(1, 7).string('Max Eligibility').style({font: {bold: true}});
    worksheet.cell(1, 8).string('Submission Date').style({font: {bold: true}});
    worksheet.cell(1, 9).string('Banks').style({font: {bold: true}});
    // Rows
    items.forEach((item, i) => {
      worksheet.cell(i + 2, 1).string(item.company_name);
      worksheet.cell(i + 2, 2).string(item.contact_name);
      worksheet.cell(i + 2, 3).string(item.age);
      worksheet.cell(i + 2, 4).string(item.email);
      worksheet.cell(i + 2, 5).string(item.phone);
      worksheet.cell(i + 2, 6).string(item.dsr_ratio);
      worksheet.cell(i + 2, 7).string(item.max_eligibility);
      worksheet.cell(i + 2, 8).string(item.created_at_local);
      worksheet.cell(i + 2, 9).string(item.selected_banks);
    });
    const buffer = await workbook.writeToBuffer();
    const params = {
            Bucket: contactsBucket,
            Key: `contacts/${id}.xlsx`,
            Body: buffer,
            ACL: 'public-read'
    }
    const fileURL = `https://${contactsBucket}.s3-${bucketRegion}.amazonaws.com/contacts/${id}.xlsx`;
    const s3 = await S3.upload(params).promise(); 
    const response = {
      statusCode: 200,    
      headers: {    
        'Access-Control-Allow-Origin': '*',    
        'Access-Control-Allow-Credentials': true,    
      },
      body: JSON.stringify({
        fileURL: fileURL,
      }),
    };  
    return response;
  } 
}
catch (err){
  console.log(err);
  return err;
}
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM