0

I structured my NodeJS project as follow: in approutes.js i handled all client requests. And load the controllers in it.

approutes.js

var store_entry_controller = require('../controllers/storeEntryController');

app.route('/add_new_store_entry')
.post(store_entry_controller.add_new_store_entry)

In storeEntryController.js i imported the storeEntryModel.js to execute Model functions

storeEntryController.js

var StoreEntry=  require('../models/storeEntryModel.js');

exports.add_new_store_entry = function(req,res){

    var new_store_entry = new StoreEntry(req.body);
    new_store_entry.starting_amount=new_store_entry.remain_amount;

    StoreEntry.addNewStoreEntry(new_store_entry,function(err,storeEntry){
        if(err){
            res.send(err);
        }else{
            res.send(storeEntry);
        };
    })
};

storeEntryModel.js

in storeEntryModel.js where i need to apply MySQL transactions (sql.beginTransaction - sql.rollback and sql.commit) I imported another Model which is CashDetail.js. In order to make another sql transaction when first query is executed with no errors. I am doing it this way.

var sql = require('./db.js');

//CashDetailModel contains the second query that should be executed on success of the first queryy

 var CashDetailModel = require('./CashDetailModel.js');

StoreEntry.addNewStoreEntry = function (entry_details,result){

   // START TRANSACTIONS
    sql.beginTransaction(function(err){
        if (err) { throw err; }
        sql.query('INSERT INTO store_entry SET ?',entry_details, function(err,res){
            if(err){
                sql.rollback(function() {
                    throw err;
                  });
            }else{
                //IMOPRTING ANOTHER FUNCTION FORM ANOTHER MODAL.
                CashDetailModel.addCashDetails(entry_details,function(err,cashDetails){

                })
            }
        })
    })

}

CashDetailModel.js

This is another Modal where i need to import it and call the function in storeEntryModel.js.

I am confused if the sql.commit and sql.rollback should be in this modal or storeEntryModel

CashDetail.addCashDetails= function(cashDetails,result){



    sql.query('INSERT INTO cash_details SET ?',cashDetails, function(err,res){
        if(err){
            sql.rollback(function() {
                throw err;
              });
        }else{
            sql.commit(function(err) {
                if (err) { 
                    sql.rollback(function() {
                    throw err;
                  });
                }
                sql.end();
              });
        }
    })
    console.log('********************** cashDetails ************************');
    console.log(cashDetails);
}

The problem

I am confused how to apply MySQL transactions when second queries is imported from another modal. shall i set the code of sql.rollback and sql.commit in the imported modal that contain the second query or in the first modal inside sql.beginTransaction ?

Ali
  • 1,633
  • 7
  • 35
  • 58
  • Does this help? https://stackoverflow.com/questions/15760067/node-js-mysql-transaction – O. Jones Apr 24 '20 at 11:28
  • You need to pass the transaction as a string to sql.query() – El. Apr 24 '20 at 11:28
  • @O.Jones no, this is different question – Ali Apr 24 '20 at 11:35
  • @El. please can you show us how, I wrote a very clear example so everyone can understand it. you can write how the `storeEntryModel.js` should look like. – Ali Apr 24 '20 at 11:37
  • @El. actully i am pasing the query as a string, but the problem is not here, but when calling another function from another modal to execute the query – Ali Apr 24 '20 at 11:43

0 Answers0