Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling Input/Output type Store Procedure in Sequelize

I've created a store procedure in MySQL which expect some inputs and return some output. To call store procedure in MySQL i am running

CALL createCoupon(1236,321, @message);
SELECT @message AS message

and getting the output in message object.

now here comes the situation i need to call this SP in sequelize. I'm working on sailsjs project and using sequelize module for queering.

I've created the database connection in config/db_config , my connection string is:

var sequelize = new Sequelize(db.name, db.user, db.pass, {
    host: db.host,
    dialect: "mysql", // or 'sqlite', 'postgres', 'mariadb'
    port:    3306, // or 5432 (for postgres)
    maxConcurrentQueries: 100,
    pool: {
    maxConnections: 50,
    maxIdleTime: 2000
    },
    queue: true
})

and i'm calling it in controller like:

var Sequelize = require('sequelize');
var sequelize = require('../../config/db_config').dbase;

function setCoupon(couponCode, userId, setCouponResponse) {
    var createCouponSQL = "some raw query";


    sequelize.query(createCouponSQL, null, {
        raw: true
    }).success(function(createCoupon) {
            sails.log.info(createCoupon);
            setCouponResponse(null, createCoupon);
    }).error(function(err) {
        sails.log.error(err);
            setCouponResponse(err, null);
    });
}


module.exports = {
    'createCoupon': function(req, callback) {
        setCoupon(req.param('coupon_code'), req.session.userSession, function(err, setCouponResponse){

        })

    }
}

now i need to call SP in sequelize so i simply try:

 var createCouponSQL = "CALL createCoupon(1236,321, @message);";
          createCouponSQL += "SELECT @message AS message";

    sequelize.query(createCouponSQL, null, {
        raw: true
    }).success(function(createCoupon) {
            sails.log.info(createCoupon);
            setCouponResponse(null, createCoupon);
    }).error(function(err) {
        sails.log.error(err);
            setCouponResponse(err, null);
    });

but the sequlize trigger the error:

Executing (default): CALL createCoupon(1236,321, @message);SELECT @message AS m
essage
error: Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the m
anual that corresponds to your MySQL server version for the right syntax to use
 near 'SELECT @message AS message' at line 1
    at Query.Sequence._packetToError (C:\Users\asd\Desktop\CardCash P2\Website\
node_modules\mysql\lib\protocol\sequences\Sequence.js:30:14)

I've done some R&D but not able to find any thing proper to call a store procedure which is sending me back the response in @message and i need to execute another select statement to get the result.

Please guide me how can i do this properly using the way I've connected the database.

Thanks.

like image 643
Ahsan Hussain Avatar asked Nov 01 '22 02:11

Ahsan Hussain


1 Answers

Though Pretty late, But we are doing something similar in a project. The entire journey looks like,

We have created all the SP's in individual SQL files and there is a deployment step, which inserts all the SP's into the database, fetching them from that particular location.

All the SP's basically return by running a SELECT statement in the end of the SP. And not having a separate select outside the SP.

So a basic structure like

DELIMITER $$
DROP PROCEDURE IF EXISTS sp_copy_cleansheet;
$$
CREATE PROCEDURE `take_over_the_world` (#Some Input Args)

BEGIN
  DECLARE method_to_be_used  VARCHAR(1000);
  # Define how to take over the world, 
  # SET method_to_be_used = "Approach A";
  ...
  ...
  SELECT method_to_be_used as Result;
END

Now we call the SP's using sequelize raw queries,

const query = 'CALL take_over_the_world(:someArg)';
return Model.sequelize.query(query, 
 { 
   replacements : { someArg},
   type : Model.sequelize.QueryTypes.SELECT 
})
.then((response) => {

//Access response here. Its of the form [{Result: /*How to take over*/}]
});

It all seems to be working Perfectly so far :)

like image 142
Shivam Avatar answered Nov 09 '22 06:11

Shivam