I implemeted a code that deletes multiple records in a table and when that deletion is completed then goes to another table to delete it's record. But the thing is I am not deleting all records of a table just making query to some of the records that have range key (id = '1423') and all records related to this key I get and using foreach loop I then delete the records. Same I am doing in other table as well.
KeyPoints about my code : In both table "id" is not the primary key and also in both Table A and Table B I have this same "id" = 1423.
Code snippet :
data: function(tableName, id){
return new Promise(function(resolve, reject){
var scanParams = {
TableName: tableName,
KeyConditionExpression: "#getId = :p",
ExpressionAttributeNames:{
"#getId": "id"
},
FilterExpression: '#getId = :v1' ,
ExpressionAttributeValues:{
":v1" : id
}
};
AWS.docClient.scan(scanParams, function(err, data) {
if (!_.isNil(err)) {
console.error("Error :", JSON.stringify(err, null, 2));
reject(err)
} else {
if(_.size(data.Items) > 0){
data.Items.forEach(function(obj, i){
var params = {
TableName: scanParams.TableName,
Key: {"hashKey":obj[hashKey]},
ReturnValues: 'NONE', // optional (NONE | ALL_OLD)
ReturnConsumedCapacity: 'NONE', // optional (NONE | TOTAL | INDEXES)
ReturnItemCollectionMetrics: 'NONE', // optional (NONE | SIZE)
}
AWS.docClient.delete(params, function(err, data) {
if (err) {
console.error("Unable to delete items :", JSON.stringify(err, null, 2));
reject(err)
} else {
console.log('Deleted the records in the table')
resolve('success')
}// successful response
});
})
} else {
console.log('No record in table')
resolve('success')
}
}
})
})
}
I am calling this data function in other two files just passing different table name in the parameters, i.e., here : function(tableName, id) and id is same.
via learner
No comments:
Post a Comment