Skip to content

Commit c2fba3f

Browse files
adress CR
1 parent f3b02b4 commit c2fba3f

File tree

7 files changed

+158
-356
lines changed

7 files changed

+158
-356
lines changed

README.md

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -617,37 +617,38 @@ await database.delete();
617617
<a id="streaming-results"></a>
618618
### Streaming results
619619

620-
The recommended way to consume query results is by using streams.
620+
Streaming can only be used with a v2 connection and only using the execute stream method.
621+
The streamResult method from normal the normal execute method returns an in-memory stream of result, rather than dynamically fetching them from the response.
621622

622-
For convenience, `statement.streamResult` also returns `meta: Promise<Meta[]>` and `statistics: Promise<Statistics>`, which are wrappers over `data.on('metadata')` and `data.on('statistics')`.
623+
The recommended way to consume query results is by using streams with standard events:
624+
* `data.on('meta')`
625+
* `data.on('data')`
626+
* `data.on('end')`
627+
* `data.on('error')`
623628

624629
```typescript
625630
const firebolt = Firebolt();
626631

627632
const connection = await firebolt.connect(connectionParams);
628633

629-
const statement = await connection.execute("SELECT 1");
634+
const statement = await connection.executeStream("SELECT 1");
630635

631-
const {
632-
data,
633-
meta: metaPromise,
634-
statistics: statisticsPromise
635-
} = await statement.streamResult();
636+
const { data } = await statement.streamResult();
636637

637638
const rows: unknown[] = [];
638639

639-
const meta = await metaPromise;
640-
641-
for await (const row of data) {
642-
rows.push(row);
643-
}
640+
const meta = await stream.once(data, "meta");
644641

645-
const statistics = await statisticsPromise
642+
data.on("data", data => {;
643+
rows.push(data);
644+
});
646645

647646
console.log(meta);
648-
console.log(statistics);
649647
console.log(rows)
650648
```
649+
In case an errors occurs before streaming, or during the first packet, the error will be thrown by the executeStream method. If the error occurs during streaming, it will be emitted by the stream.
650+
651+
```typescript
651652

652653
<a id="custom-stream-transformers"></a>
653654
### Custom stream transformers
@@ -679,22 +680,45 @@ const serializedStream = new SerializeRowStream()
679680
680681
const firebolt = Firebolt();
681682
const connection = await firebolt.connect(connectionParams);
682-
const statement = await connection.execute("select 1 union all select 2");
683+
const statement = await connection.executeStream("select * from generate_series(1, 1000)");
683684
684685
const { data } = await statement.streamResult();
685686
686687
687688
data.pipe(serializedStream).pipe(process.stdout);
688689
```
689690

690-
Or use `rowParser` that returns strings or Buffer:
691+
<a id="in-memory-stream"></a>
692+
### In-memory stream
693+
694+
When using the streamResult method on the object returned from a simple execute method, the driver will return an in-memory stream of the result. This is useful for small result sets, but not recommended for large result sets.
691695

692696
```typescript
693-
const { data } = await statement.streamResult({
694-
rowParser: (row: string) => `${row}\n`
695-
});
697+
const firebolt = Firebolt();
698+
699+
const connection = await firebolt.connect(connectionParams);
696700
697-
data.pipe(process.stdout);
701+
const statement = await connection.execute("SELECT 1");
702+
703+
const {
704+
data,
705+
meta: metaPromise,
706+
statistics: statisticsPromise
707+
} = await statement.streamResult();
708+
709+
const rows: unknown[] = [];
710+
711+
const meta = await metaPromise;
712+
713+
for await (const row of data) {
714+
rows.push(row);
715+
}
716+
717+
const statistics = await statisticsPromise
718+
719+
console.log(meta);
720+
console.log(statistics);
721+
console.log(rows)
698722
```
699723

700724
## Development process

src/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,5 @@ export {
3838
QueryFormatter
3939
} from "./formatter/base";
4040

41-
export { JSONParser } from "./statement/stream/parser";
42-
4341
export type { Connection } from "./connection";
4442
export type { Meta } from "./meta";

src/statement/stream/parser.test.ts

Lines changed: 0 additions & 95 deletions
This file was deleted.

0 commit comments

Comments
 (0)