Jason
Jason

Reputation: 87

Deleting a row if the value is a duplicate DynamoDB

I'm trying to compare if dynambodb has duplicate phoneNumbers and if it does I want to delete the older one. There doesn't seem to be a whole lot, on doing this through lambda. Some help would be appreciated. Thanks

const dynamoDb = new AWS.DynamoDB.DocumentClient();
const params = {
  TableName: process.env.DYNAMODB_TABLE,
  ProjectionExpression: "phoneNumber, createdAt",
};

module.exports.list = (event, context, callback) => {
  // fetch all LakeSubscriptions from the database
  dynamoDb.scan(params, (error, result) => {
    // handle potential errors
    if (error) {
      console.error(error);
      callback(null, {
        statusCode: error.statusCode || 501,
        headers: { 'Content-Type': 'text/plain' },
        body: 'Couldn\'t fetch the item.',
      });
      return;
    }
    /*
    for(item in result.phoneNumber){   //not really sure how I can pull these individual values I need to use to compare.

    module.exports.delete = (event, context, callback) => {
  const params = {
    TableName: process.env.DYNAMODB_TABLE,
    Key: {
      id: event.pathParameters.id,
    },
  };

  // delete the lakeSubscription from the database
  dynamoDb.delete(params, (error) => {
    // handle potential errors
    if (error) {
      //error handling
      });
      return;
    }
  });
};
}
*/


    // create a response
    const response = {
      statusCode: 200,
      body: JSON.stringify(result.Items),
    };
    callback(null, response);
  });
};

Upvotes: 0

Views: 158

Answers (1)

Sabarish Sathasivan
Sabarish Sathasivan

Reputation: 1276

Not a expert in Node.JS , but some suggestions

Scan operation results are returned in pages of 1 MB each, So the above code will not return all the rows in the dynamodb . Refer to the Step 4.3: Scan of this documentation

https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GettingStarted.NodeJs.04.html

This would give you some idea on how to paginate and get all records from dynamodb . you can add all the results

Also refer the following post

How to fetch/scan all items from `AWS dynamodb` using node.js

This should give u a better idea

Upvotes: 1

Related Questions