Skip to content

Commit

Permalink
add stream as iterator part (#454)
Browse files Browse the repository at this point in the history
  • Loading branch information
bertho-zero authored Jul 18, 2023
1 parent 39cad61 commit 7702d42
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/guide/interfaces.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ Streams are a powerful way of piping data through as it comes in, rather than al

**.stream([options], [callback])**

If called with a callback, the callback is passed the stream and a promise is returned. Otherwise, the readable stream is returned.
If called with a callback, the callback is passed the stream and a promise is returned. Otherwise, the readable stream is returned.
When the stream is consumed as an [iterator](https://nodejs.org/api/stream.html#readablesymbolasynciterator), if the loop terminates with a `break`, `return`, or a `throw`, the stream will be destroyed. In other terms, iterating over a stream will consume the stream fully.

```js
// Retrieve the stream:
Expand All @@ -127,6 +128,17 @@ const stream = knex.select('*')
stream.pipe(writableStream);
```

```js
// Use as an iterator
const stream = knex.select('*')
.from('users')
.stream();

for await (const row of stream) {
/* ... */
}
```

```js
// Use as a promise:
const stream = knex.select('*')
Expand Down

0 comments on commit 7702d42

Please sign in to comment.