clarkie / dynogels

DynamoDB data mapper for node.js. Originally forked from https://github.com/ryanfitz/vogels
Other
490 stars 110 forks source link

How to delete tables for all defined models? #150

Closed rasovica closed 6 years ago

rasovica commented 6 years ago

I am writing tests, In beforeAll I call createTables where all the tables are created prefixed by Test in their name, How can I delete all tables after I am done with tests? Is there deleteTables or something similar? I know i can delete each one calling Model.deleteTable but, that just results in 15+ lines of code inside afterAll

cdhowie commented 6 years ago

Shouldn't be too hard to aggregate a promise for this in your code?

function deleteAllTables() {
  return Promise.all(
    Object.values(dynogels.models).map(m =>
      new Promise((r, j) => m.deleteTable(err => err ? j(err) : r()))
    )
  );
}

Or using async:

function deleteAllTables(callback) {
  async.parallel(
    Object.values(dynogels.models).map(m => m.deleteTable.bind(m)),
    callback
  );
}
rasovica commented 6 years ago

Yes, this is simple and sexy, thank you, it would be nice if you implemented it in the library however.