-
Notifications
You must be signed in to change notification settings - Fork 108
/
Copy pathdeleteTable.js
51 lines (39 loc) · 1.52 KB
/
deleteTable.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
var async = require('async')
module.exports = function deleteTable (store, data, cb) {
var key = data.TableName, tableDb = store.tableDb
store.getTable(key, false, function (err, table) {
if (err) return cb(err)
// Check if table is ACTIVE or not?
if (table.TableStatus == 'CREATING') {
err = new Error
err.statusCode = 400
err.body = {
__type: 'com.amazonaws.dynamodb.v20120810#ResourceInUseException',
message: 'Attempt to change a resource which is still in use: Table is being created: ' + key,
}
return cb(err)
}
table.TableStatus = 'DELETING'
var deletes = [ store.deleteItemDb.bind(store, key), store.deleteTagDb.bind(store, key) ]
;[ 'Local', 'Global' ].forEach(function (indexType) {
var indexes = table[indexType + 'SecondaryIndexes'] || []
deletes = deletes.concat(indexes.map(function (index) {
return store.deleteIndexDb.bind(store, indexType, table.TableName, index.IndexName)
}))
})
delete table.GlobalSecondaryIndexes
tableDb.put(key, table, function (err) {
if (err) return cb(err)
async.parallel(deletes, function (err) {
if (err) return cb(err)
setTimeout(function () {
tableDb.del(key, function (err) {
// eslint-disable-next-line no-console
if (err && !/Database is not open/.test(err)) console.error(err.stack || err)
})
}, store.options.deleteTableMs)
cb(null, { TableDescription: table })
})
})
})
}