tedious icon indicating copy to clipboard operation
tedious copied to clipboard

[FEATURE REQUEST] AsyncIterator for Request class (promise-based streaming)

Open chdh opened this issue 2 years ago • 4 comments

I suggest to implement an AsyncIterator for the Request class.

This would allow promise-based streaming of query results.

Example of how to use it:

const request = new Request(sql);
connection.execSql(request);
for await (const row of request) {
   console.log(row);
}

chdh avatar Jul 05 '23 13:07 chdh

This is a great idea, and I've thought about this a bit before. The main problem is that a single Request does not mean you get a single "type" of rows - there's multiple rowstreams which each will come with their own column definitions.

Do you have a suggestion how this could be modeled?

arthurschreiber avatar Jul 05 '23 14:07 arthurschreiber

there's multiple rowstreams which each will come with their own column definitions.

A simple solution would be to use the following structure for the items returned by the iterator:

interface RequestIteratorItem {
   row: ColumnValue[] | Record<string, ColumnValue>;
   resultSetNo: number; // 1..n
   columnMetaData: ColumnMetaData[] | Record<string, ColumnMetaData>;
}

The user would have to detect a change in resultSetNo to process the start of a new result set.

chdh avatar Jul 05 '23 17:07 chdh

@arthurschreiber I already have a working private implementation of an AsyncIterator for the Tedious Request class. I needed the functionality for a client project. I could use it as the basis for a PR.

Would you be interested in a PR from me, or have you already started development yourself?

chdh avatar Jul 06 '23 15:07 chdh

I'd be happy if you open a PR. 🥳

arthurschreiber avatar Jul 06 '23 15:07 arthurschreiber