You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I fetch n millions records from SqlServer (streaming) and process over every record. I want to call nested functions into request.on('row', row => { }) and pause streaming until end of latest nested functions.
#NodeJs
#SqlServer
I want this:
const sql = require('mssql')
sql.connect(config, err => {
// ... error checks
const request = new sql.Request()
request.stream = true // You can set streaming differently for each request
request.query('select * from verylargetable') // or request.execute(procedure)
request.on('recordset', columns => {
// Emitted once for each recordset in a query
})
request.on('row', row => {
// Emitted for each row in a recordset
//call process_1(row)
})
request.on('error', err => {
// May be emitted multiple times
})
request.on('done', result => {
// Always emitted as the last one
})
})
sql.on('error', err => {
// ... error handler
})
function process_1(data){
//process done after n minutes
//if end process, then call process_2(newData)
}
function process_2(newData){
//process done after n minutes
//if end process, then resume streaming request
}
Notice: I road tediousjs/tedious#181 and other issue about pause/pesume stream request but I didn't get any idea!
thanks
typeDeferred<T>={promise: Promise<T>,resolve: (result: T)=>void,reject: (reason: any)=>void,};functioncreateDeferred<T>(): Deferred<T>{constresult={}asDeferred<T>;result.promise=newPromise<T>((resolve,reject)=>{result.resolve=resolve;result.reject=reject;});returnresult;}functionstreamQuery<T>(query: TemplateStringsArray, ...interpolations: any[]): AsyncIterable<T>{return{[Symbol.asyncIterator]: asyncIterator};functionasyncIterator(): AsyncIterator<any>{constrequest=pool.request();request.stream=true;// Reproduce pool.query but for request.// This is equivalent to request._template('query', query, interpolations);constcommand=[query[0]];for(leti=0;i!==interpolations.length;i++){request.input(`p${i}`,interpolations[i]);command.push(`@p${i}`,query[i+1]);}request.query(command.join(''));// ignoring query result.request.on('row',row);request.on('error',error);request.on('done',done);// Note this will buffer without bound, mssql doesn't let us apply backpressure (suspend streaming).// Results received, but not requested, in the order receivedconstbuffer: Array<Promise<IteratorResult<T>>>=[];letbufferDone=false;// Results requested, but not received, in the order requestedconstpending: Array<Deferred<IteratorResult<T>>>=[];// At least one of the above should always be empty.return{ next };functionnext(): Promise<IteratorResult<T>>{// if (buffer.length === LOW) request.resume();if(buffer.length){returnbuffer.shift()!;}if(bufferDone){returnPromise.resolve({value: undefinedasanyasT,done: true});}constdeferred=createDeferred<IteratorResult<T>>();pending.push(deferred);returndeferred.promise;}functionrow(row: T){constresult={value: row,done: false};if(!pending.length){buffer.push(Promise.resolve(result));// if (buffer.length === FULL) request.pause();}else{pending.shift()!.resolve(result);}}functionerror(reason: any){if(!pending.length){buffer.push(Promise.reject(reason));// if (buffer.length === FULL) request.pause();}else{pending.shift()!.reject(reason);}}functiondone(){bufferDone=true;while(pending.length){pending.shift()!.resolve({value: undefinedasanyasT,done: true});}}}}
(note that this code is not terribly well tested, sorry!)
Hello!
I have the same problem and solve this with tedious pause/pesume stream request.
I've create small repo with example: tediousStreaming
There are 2 examples:
Yep - This was added in #775 and released in v5.0.0 - however, due to a bug in the tedious driver, it didn't actually work properly and is now in v6.0.0 without any bugs (see #838 and #832) 🎉
Activity
simonbuchan commentedon Feb 2, 2018
Use-case: backpressure from async-iterable usage:
(note that this code is not terribly well tested, sorry!)
mariotacke commentedon Feb 9, 2018
@simonbuchan, do you have an example which does not use TypeScript?
simonbuchan commentedon Feb 9, 2018
If you can't be bothered to strip the types yourself, you can paste it in the typescript playground
ilusharulkov commentedon Jun 18, 2018
Hello!
I have the same problem and solve this with tedious pause/pesume stream request.
I've create small repo with example: tediousStreaming
There are 2 examples:
When use node-mssql make sure this commit is in node_modules
I think data flow (process_1, process_2) is easy to emplement using librabry such as https://highlandjs.org/
dhensby commentedon May 7, 2019
pause/resume of streams is now added
hosseinGanjyar commentedon May 8, 2019
OH MY GOD...After about 2 years solved this issue.
dhensby commentedon May 8, 2019
Yep - This was added in #775 and released in v5.0.0 - however, due to a bug in the tedious driver, it didn't actually work properly and is now in v6.0.0 without any bugs (see #838 and #832) 🎉
mudouasenha commentedon Nov 17, 2020
Thanks!!!!!!