0

I tried to create the code to clean all tables respecting their relations (the child tables must be cleaned before their parent) and after this I need to add some basic data to run my tests.

I'm having some issues related to Promises chaining, because if I use Promise.all it doesn't respect the correct order, and if the create the chained blocks using then(() => {}) (as in the sample code below) it respects the order but the code becomes a mess.

How can I achieve the same result but without the mess?

before((done) => {
        modelDef.TaskListItemModel.TaskListItem.query().del()
            .then(() => {
                modelDef.TaskListModel.TaskList.query().del()
                    .then(() => {
                        modelDef.ProductAnomalyModel.ProductAnomaly.query().del()
                            .then(() => {
                                modelDef.ProductModel.Product.query().del()
                                    .then(() => {
                                        modelDef.UserModel.User.query().del()
                                            .then(() => {
                                                modelDef.UserModel.User.forge(test_User)
                                                    .save()
                                                    .then((result) => {
                                                        test_User.id = result.get('id');
                                                        test_ProductAnomaly.createdBy = test_User.id;
                                                        test_TaskList.createdBy = test_User.id;

                                                        return modelDef.ProductModel.Product.forge(test_Product)
                                                            .save();
                                                    })
                                                    .then((result) => {
                                                        test_Product.id = result.get('id');
                                                        test_ProductAnomaly.ProductId = test_Product.id;

                                                        return modelDef.ProductAnomalyModel.ProductAnomaly.forge(test_ProductAnomaly)
                                                            .save();
                                                    })
                                                    .then((result) => {
                                                        test_ProductAnomaly.id = result.get('id');
                                                        test_TaskListItem.ProductAnomalyId = test_ProductAnomaly.id;

                                                        return modelDef.TaskListModel.TaskList.forge(test_TaskList)
                                                            .save();
                                                    })
                                                    .then((result) => {
                                                        test_TaskListItem.tasklistId = result.get('id');

                                                        return modelDef.TaskListItemModel.TaskListItem.forge(test_TaskListItem)
                                                            .save();
                                                    })
                                                    .then(done);
                                            })
                                    })
                            })
                    })
            });

    });
juliano.net
  • 7,982
  • 13
  • 70
  • 164
  • which database? – Mikael Lepistö Sep 28 '17 at 20:37
  • The dominant issue with the code in the question is that promises are used as callbacks rather than as promises. The question against which I closed deals with that issue. – Louis Sep 28 '17 at 20:40
  • @MikaelLepistö I'm using MySQL – juliano.net Sep 29 '17 at 00:42
  • @JulianoNunesSilvaOliveira you could start transaction, set foreign key checks off and then truncate all the tables in any order you like. something like this: https://github.com/Vincit/knex-db-manager/blob/master/lib/MySqlDatabaseManager.js#L86 – Mikael Lepistö Sep 29 '17 at 18:19
  • Also with knex inside transaction you can do multiple queries, without need to wait that earlier is ready, since driver buffers those queries before sending them out. – Mikael Lepistö Sep 29 '17 at 18:21

0 Answers0