How the sequelize deal with session when temporary table used in mysql stored procedure?

Viewed 1833

We are building an application using nodejs, mysql using sequelize. Sequelize makes development more easy when it comes to deal with database. Nice concept. I need your help in Sequelize. We have gone far with software development using Sequelize using MySQL database. We have built many stored procedures in MySQL which takes care on complex business calculation/ logic.

We faced below two issues while deal with Stored procedure which contains temporary table data and sequelize.

Case 1:

I have created one stored procedure which returns the data from temporary table. The stored procedure is creating the temporary table if not exist.

CREATE PROCEDURE `Sproc_GetCountryList` ()
BEGIN
    Drop temporary table if exists tempCountry;

    Create temporary table if not exists tempCountry

    select countryID, countryName, countrySortCode
    from countrymst;
END

Now when I call it using sequelize and transaction, (see minimal example code below), it returns duplicate data on subsequent call from UI immediately. but when I keep few seconds difference in call (e.g. around 10 seconds) then it returns the proper result.

getCountryList: (req, res) => {
    const {
        sequelize
    } = req.app.locals.models;
        return sequelize.transaction().the(t) => {
            return sequelize.query('CALL Sproc_GetCountryList ()', {
                type: sequelize.QueryTypes.SELECT,
                transaction: t
            }).then((response) => {
                t.commit();
                return resHandler.successRes(res, 200, STATE.SUCCESS, _.values(_.first(response)));
            }).catch(function (err) {
                t.rollback();
            });
        }).catch(function (err) {
            t.rollback();
        });
},

Each stored procedure contains one or two temporary table and as per MySQL, temporary table scope is limited to per session. So user having different session will have multiple temporary table but with its own set of temporary data. (Ref. https://www.oreilly.com/library/view/mysql-cookbook-2nd/059652708X/ch04s04.html) Do you have any suggestion how I can deal with session with sequelize so that it always return the unique data on subsequent call.

Case 2:

I have created one stored procedure which returns the data from temporary table. The stored procedure is creating the temporary table if not exist and also drops the temporary table at the end of stored procedure.

CREATE PROCEDURE `Sproc_GetCountryList` ()
BEGIN

    Create temporary table if not exists tempCountry
    select countryID, countryName, countrySortCode
    from countrymst;

    Drop temporary table tempCountry;

END

Now when I call it using sequelize, (see minimal example code below), subsequent call from UI immediately, and returns the error like, column 'countryName' is undefined.

getCountryList: (req, res) => {
    const {
        sequelize
    } = req.app.locals.models;
        sequelize.query('CALL Sproc_GetCountryList ()', {
            type: sequelize.QueryTypes.SELECT
        }).then((response) => {
            return resHandler.successRes(res, 200, STATE.SUCCESS, _.values(_.first(response)));
        }).catch(function (err) {
            console.trace();
        });
},

Can you please share you thought to prevent this kind of situation?

0 Answers
Related