【问题标题】:node js Calling stored procedure in mysql using connection pool and multiple parametersnode js使用连接池和多个参数在mysql中调用存储过程
【发布时间】:2019-12-29 17:59:47
【问题描述】:

db.js

const mysql = require('mysql');

const connection = mysql.createPool({
  host: 'localhost',
  user : 'xxxxx',
  password : 'xxxxx',
  database : 'xxxx'
})
module.exports = connection;

customer.js

const db = require("../../db");

在 customer.js 中,我需要调用 mysql 的 SP,它将采用 10 个输入参数,因为它将在表中插入一条记录。 使用 db.js 函数从 customer.js 调用 SP 的最佳方法是什么

【问题讨论】:

    标签: mysql node.js stored-procedures


    【解决方案1】:

    我会像这样创建数据库类

    let pool;
    
    async function setConnectionPool() {
      if (!pool) {
        pool = await sql.connect("connectionstring here");
      }
    
      sql.on("error", err => {
        logger.error("An error has occured while setting connection pool", err);
        throw error;
      });
    }
    
    async function save(entity) {
      try {
        await pool.request()
          .input("SomeId1", sql.VarChar(50), entity.field1)
          .execute("SomeProcedureName");
      } catch (error) {
         console.log("error", error);
      }
    }
    
    module.exports = save;
    
    

    从您的 customer.js 中,您可以简单地调用方法 save

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-30
      • 1970-01-01
      • 1970-01-01
      • 2021-07-15
      • 2021-05-18
      • 1970-01-01
      • 2019-07-07
      • 1970-01-01
      相关资源
      最近更新 更多