我是node.js和TypeScript的新手,负责使用TypeScript和MySql中的存储过程从MySql中获取数据。我已经搜索了一个关于如何从TypeScript中执行存储过程的例子,但是没有发现一个关于它的例子。我发现的最接近的东西是node.js中的数据访问示例。此外,我对ORM包不感兴趣,我想使用一个低级的驱动程序,只需从类型记录中执行存储过程,那么我就可以处理其他的事情了。
我有调用存储过程的示例node.js代码,如下所示:
connection.query("CALL spMIJobFilter_lst('4261ebf7-614c-4b6c-9dff-39044a048b26');", function (error, results, fields) {
if (error)
throw error;
results.forEach(result => {
console.log(result);
});
});但不知道如何将其转换为TypeScript。
最后,这段代码返回json,我希望有一个选项返回一个简单的对象数组,并跳过数据库之间的某个进程,在这里将数据转换为json。
谢谢。
发布于 2022-07-16 15:03:00
找到了答案,但也许有人能解释为什么这方面的信息似乎很少。
//We need mysql2 and NOT mysql
const mysql = require('mysql2');
let connection = mysql.createConnection({
host: '127.0.0.1',
user: 'root',
password: 'myPW',
database: 'myDB'
});
connection.connect(function(err: any) {
if (err) {
return console.error('error: ' + err.message);
}
console.log('Connected to the MySQL server.');
});
console.log('step 3');
//Execute the stored procedure the same way we would in an SQL script.
//The only examples I found were in node.js, so here we need to add types to the prameters
//Note:The resultset in Array<any>, however, some procedures return a scalar value instead
// so this type would have to change. I have not test that yet.
connection.query('CALL spix_Trace_lstForReview(\"2022-1-1\", \"2022-7-7\" )', function (error: any, results: Array<any>, fields: any) {
if (error)
throw error;
console.log(results);
results.forEach(result => {
console.log(result.Name);
});
});https://stackoverflow.com/questions/72974272
复制相似问题