Skip to content

Commit

Permalink
feat: add AsyncIterable support to stream() (fixes #426)
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed Sep 29, 2023
1 parent 35b9a98 commit 821851c
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 4 deletions.
21 changes: 21 additions & 0 deletions .README/QUERY_METHODS.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,27 @@ await connection.stream(sql.typeAlias('foo')`SELECT foo`, (stream) => {
});
```

You can also using the [AsyncIterable](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AsyncIterator) interface:

```ts
await connection.stream(sql.typeAlias('foo')`SELECT foo`, async (stream) => {
for await (const row of stream) {
row;
// {
// data: {
// foo: 'bar'
// },
// fields: [
// {
// name: 'foo',
// dataTypeId: 23,
// }
// ]
// }
}
});
```

### `transaction`

`transaction` method is used wrap execution of queries in `START TRANSACTION` and `COMMIT` or `ROLLBACK`. `COMMIT` is called if the transaction handler returns a promise that resolves; `ROLLBACK` is called otherwise.
Expand Down
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2486,6 +2486,27 @@ await connection.stream(sql.typeAlias('foo')`SELECT foo`, (stream) => {
});
```

You can also using the [AsyncIterable](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AsyncIterator) interface:

```ts
await connection.stream(sql.typeAlias('foo')`SELECT foo`, async (stream) => {
for await (const row of stream) {
row;
// {
// data: {
// foo: 'bar'
// },
// fields: [
// {
// name: 'foo',
// dataTypeId: 23,
// }
// ]
// }
}
});
```

<a name="user-content-slonik-query-methods-transaction"></a>
<a name="slonik-query-methods-transaction"></a>
### <code>transaction</code>
Expand Down
9 changes: 5 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,16 @@ export type QueryId = string;

export type MaybePromise<T> = Promise<T> | T;

type StreamDataEvent<T> = { data: T; fields: readonly Field[] };

// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
interface TypedReadable<T> extends Readable {
// eslint-disable-next-line @typescript-eslint/method-signature-style
on(
event: 'data',
listener: (chunk: { data: T; fields: readonly Field[] }) => void,
): this;
on(event: 'data', listener: (chunk: StreamDataEvent<T>) => void): this;
// eslint-disable-next-line @typescript-eslint/method-signature-style
on(event: string | symbol, listener: (...args: any[]) => void): this;

[Symbol.asyncIterator]: () => AsyncIterableIterator<StreamDataEvent<T>>;
}

export type StreamHandler<T> = (stream: TypedReadable<T>) => void;
Expand Down
31 changes: 31 additions & 0 deletions test/slonik/integration/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,37 @@ test('streams rows (check types)', async (t) => {
await pool.end();
});

test('streams rows using AsyncIterator', async (t) => {
const pool = await createPool(t.context.dsn);

await pool.query(sql.unsafe`
INSERT INTO person (name)
VALUES ('foo'), ('bar'), ('baz')
`);

const names: string[] = [];

await pool.stream(
sql.type(
z.object({
name: z.string(),
}),
)`
SELECT name
FROM person
`,
async (stream) => {
for await (const row of stream) {
names.push(row.data.name);
}
},
);

t.deepEqual(names, ['foo', 'bar', 'baz']);

await pool.end();
});

test('streams rows with different batchSize', async (t) => {
const pool = await createPool(t.context.dsn);

Expand Down

0 comments on commit 821851c

Please sign in to comment.