NG51214 NG51214 - 1 month ago 28
Javascript Question

How to call stored procedure in strongloop

I am using

MySQL
and
strongloop
, I have a stored procedure to swap data

swap_XYZ('<old_abc>', '<new_new>')


I am not able to find any example in the documentation to call stored procedure. How to call this stored procedure? Thanks in advance.

Answer
module.exports = function (ABCModel) {
 var ds = app.dataSources.dsMySQL;

  ABCModel.swap = function (old_abc, new_abc, cb) {

    var sql = "CALL `swap_XYZ`('" + old_abc + "','" + new_abc + "');";

    ds.connector.query(sql, function (err, data) {
      if (err) {
        console.log("Error:", err);
      }
      cb(null, data);
      console.log("data:", data);
    });
  }

  ABCModel.remoteMethod(
    'swap',
    {
      accepts: [
        {arg: 'old_abc', type: 'string'},
        {arg: 'new_abc', type: 'string'}
      ],
      returns: {arg: 'result', type: 'object'},
      http: {path: '/swap', verb: 'post'}
    }
  );
};