[英]Use pipe to stream data from msssql to node
I am using node and node-mssql 6.0.1 and I use streams to get big amounts of data from the DB and send it to the front end.我正在使用 node 和node-mssql 6.0.1,我使用流从数据库中获取大量数据并将其发送到前端。
I am trying to use pipe and stream as suggested by the docs , but I cannot get it to work.我正在尝试按照文档的建议使用管道和流,但我无法让它工作。
My code is我的代码是
const pool = new sql.ConnectionPool(config);
const poolConnect = pool.connect();
const testStream = (ws , id) => {
poolConnect.then((pool) => {
const request = new sql.Request(pool);
request.pipe(stream);
request
.input('id_param', sql.Int, parseInt(id))
.query('SELECT * FROM dataTable WHERE id = @id_param ')
})
I get stream is not defined
.我得到
stream is not defined
。
My goal is to combine stream and pipe like the example here and then use websockets to send data to the client.我的目标是像这里的示例一样组合流和管道,然后使用 websockets 将数据发送到客户端。 I dont even know if it makes sense to combine stream and websockets.
我什至不知道将流和 websockets 结合是否有意义。 I cannot think of any way to combine or test
我想不出任何组合或测试的方法
My current working code that uses just stream, looks like this我当前仅使用流的工作代码如下所示
const sql = require('mssql');
const pool = new sql.ConnectionPool(config);
const poolConnect = pool.connect();
const testStream = (ws , id) => {
poolConnect.then((pool) => {
const request = new sql.Request(pool);
request.stream = true;
request
.input('id_param', sql.Int, parseInt(id))
.query('SELECT * FROM table WHERE id = @id_param ')
let rowsToProcess = [];
let data = [];
request.on('row', row => {
rowsToProcess.push(row);
if (rowsToProcess.length >= 20) {
request.pause();
processRows(false);
}
});
request.on('done', () => {
processRows(true);
sql.close();
});
const processRows = () => {
rowsToProcess.forEach((item)=>{
data.push(item.name);
data.push(item.surname);
data.push(item.age);
});
ws.send(JSON.stringify({ success:true, message: data }));
rowsToProcess = [];
data = [];
}//processRows
}) //poolConnect.then
What am I missing here?我在这里缺少什么? Please help me get started with pipe and streams, I cannot find pipe/stream examples in Google
请帮助我开始使用管道和流,我在 Google 中找不到管道/流示例
Thanks谢谢
Simple Writeable stream简单的可写流
const { Writable } = require('stream');
const stream = new Writable({
write(chunk, encoding, callback) {
console.log(chunk.toString());
callback();
}
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.