Skip to content

Commit

Permalink
fix purge table when table has many keys
Browse files Browse the repository at this point in the history
  • Loading branch information
jcdesignweb authored and rchl committed Mar 1, 2019
1 parent 8660dd9 commit d7f27b0
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions lib/actions/purgeTable.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
const { doSearch } = require('../util')

const findPrimaryKey = context => {
const findPrimaryKeys = context => {
const params = {
TableName: context.tableName
}

return context.dynamodb.describeTable(params).promise()
.then(tableDescription => {
context.primaryKey = tableDescription.Table.KeySchema.find(
element => element.KeyType === 'HASH').AttributeName
const tableSchema = tableDescription.Table.KeySchema

context.primaryKeys = ['HASH', 'RANGE']
.map(keyType => tableSchema.find(element => element.KeyType === keyType))
.filter(attribute => attribute)
.map(attribute => attribute.AttributeName)


return context
})
}
Expand All @@ -26,9 +32,7 @@ const deleteAllElements = context => {
for (const item of context.items) {
params.RequestItems[context.tableName].push({
DeleteRequest: {
Key: {
[context.primaryKey]: item[context.primaryKey]
}
Key: item
}
})

Expand All @@ -50,7 +54,7 @@ const deleteAllElements = context => {

const findAllElements = context => {
const scanParams = {
ProjectionExpression: context.primaryKey,
ProjectionExpression: context.primaryKeys.join(', '),
}

return doSearch(context.dynamodb, context.tableName, scanParams)
Expand Down Expand Up @@ -81,9 +85,9 @@ const purgeTable = (tableName, dynamodb) => {
dynamodb
}

return findPrimaryKey(context)
return findPrimaryKeys(context)
.then(findAllElements)
.then(deleteAllElements)
}

module.exports = {purgeTable}
module.exports = { purgeTable }

0 comments on commit d7f27b0

Please sign in to comment.