最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to call stored procedure in strongloop - Stack Overflow

programmeradmin0浏览0评论

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.

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.

Share Improve this question edited Oct 11, 2016 at 5:28 marc_s 755k184 gold badges1.4k silver badges1.5k bronze badges asked Feb 18, 2016 at 6:38 NG51214NG51214 1111 silver badge7 bronze badges 1
  • Checking this URL, hopefully it can help you – Willie Cheng Commented Feb 18, 2016 at 6:42
Add a ment  | 

4 Answers 4

Reset to default 5
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'}
    }
  );
};

Refer this link. dataSource.connector.execute(sql, params, cb); or dataSource.connector.query(sql, params, cb);

https://docs.strongloop./display/public/LB/Database+connectors https://docs.strongloop./display/public/LB/Executing+native+SQL

 module.exports = function(DemoModel) {
    var server = require('../../server/server');
      var ds = server.dataSources.MySQL;

    DemoModel.list = function(optionalparam, cb) {  


      var sql = 'select * from DemoModel';
      ds.connector.execute(sql, function(err, data)
      {
      if(err) return err;
      console.log(err);
      console.log("data",data);
        cb(null, data);
      });

    }

    DemoModel.remoteMethod(
        'list', 
        {
          accepts: {arg: 'param', type: 'string'},
          returns: {arg: 'result', type: 'object'},
          http: {path: '/list', verb: 'get'}
        }
    );


    };

I finally called stored procedure successfully..

First, we need to define app like Riaz as kather var app = require('../../server/server');

But both app.dataSources.MySQL and app.dataSources.dsMySQL are not working for me. My loopback version is [email protected] and the correct property name for me is app.dataSources.mysql;

And then make sure your database user account has the privilege for calling stored procedure:

Granting Privileges on Functions/Procedures: While using functions and procedures, the Grant statement can be used to grant users the ability to execute the functions and procedures in MySQL. Granting Execute Privilege: Execute privilege gives the ability to execute a function or procedure.

Syntax:

GRANT EXECUTE ON [ PROCEDURE | FUNCTION ] object TO user;

Quote from: https://www.geeksforgeeks/mysql-grant-revoke-privileges/

Call PL/SQL stored procedure using node.js

Suppose the procedure's name is getCountryName and it takes one input parameter and one output parameter.

var executeProcedure = function () {
        var procedureName = "CALL GETCOUNTRYNAME(:inputVal,:outVal)";
        var oracledb = require('oracledb');
        oracledb.maxRows = 1;
        // oracledb.fetchAsString = [oracledb.CLOB];
        oracledb.getConnection(
            {
                user: 'user',
                password: 'password',
                connectString: "connectString"
            },
            function (err, connection) {
                if (err) {
                    console.error(err);

                    return;
                }
                connection.execute(
                    procedureName,
                    {
                        outVal: {
                            type: oracledb.STRING,
                            dir: oracledb.BIND_OUT
                        },
                        inputVal: {
                            type: oracledb.STRING,
                            dir: oracledb.BIND_IN,
                            val: 'IN'
                        }
                    },

                    function (err, result) {
                        if (err) {
                            console.error(err);

                            return;
                        }
                       console(result)
                    });
            });
};

For more details

https://github./oracle/node-oracledb/blob/master/doc/api.md#queryinglobs https://dzone./articles/plsql-record-types-and-the-nodejs-driver

https://github./oracle/node-oracledb/blob/master/examples/example.js

发布评论

评论列表(0)

  1. 暂无评论