Would it be possible to create a Delete Table method so that an entire table can be deleted from the database?
const dynamoose = require('dynamoose')
...
let Model = dynamoose.model('Models', Model)
Model.deleteTable().then(err=>console.log(err))
IMO, this project should be advocating for the use of CloudFormation to manage the existence, absence, and high level properties (eg: throughput, hash keys, etc) of tables in DynamoDB.
I think we're getting to a point where this concept should be decided one way or another, since a similar discussion has emerged in #172 as well.
I completely understand the request; however, deleting a table is initially not supported. I don't want Dynamoose to be accused of deleting someone's table.
As for the CloudFormation, see #151.
Deleting tables would be useful for us too - in our tests, we run a DynamoDB Local Docker container and create and drop tables to ensure no state remains between tests.
It's not as though this isn't a destructive library - you could delete all the data from within a table! IMO, the onus should be on the user to ensure permissions are set up correctly (e.g. the dynamodb:DeleteTable permission).
As workaround:
let dynamoDB = dynamoose.ddb();
dynamoDB.deleteTable({TableName: 'table-name-to-remove'}, (err, resp) => {
if (err) {
console.error(err);
}
console.log(resp);
})
I needed to delete the table before running every test. I ran into issues where table was not deleted but the function returned. So I added tableNotExists event and then resolve the promise
const dynamoDB = dynamoose.ddb();
deleteTable: tableName => new Promise((resolve, reject) => {
dynamoDB.deleteTable({ TableName: tableName }, (err, response) => {
if (err) {
if (err.code === 'ResourceNotFoundException') {
return resolve('table does not exists, so nothing to delete');
}
return reject(err);
}
dynamoDB.waitFor('tableNotExists', { TableName: tableName }, (error) => {
if (error) {
console.log(error);
return reject(error);
}
return resolve(response);
});
return null;
});
}),
Most helpful comment
Deleting tables would be useful for us too - in our tests, we run a DynamoDB Local Docker container and create and drop tables to ensure no state remains between tests.
It's not as though this isn't a destructive library - you could delete all the data from within a table! IMO, the onus should be on the user to ensure permissions are set up correctly (e.g. the
dynamodb:DeleteTablepermission).