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的最佳方式是什么
发布于 2019-12-30 02:16:43
我将像这样创建db类。
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
希望这能有所帮助。
https://stackoverflow.com/questions/59521937
复制相似问题