Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: gajus/slonik
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v36.0.0
Choose a base ref
...
head repository: gajus/slonik
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v37.0.0
Choose a head ref
  • 8 commits
  • 7 files changed
  • 1 contributor

Commits on Sep 29, 2023

  1. Copy the full SHA
    418b0fd View commit details
  2. style: drop Type suffix

    gajus committed Sep 29, 2023
    Copy the full SHA
    3050278 View commit details
  3. fix: re-use executionRoutine

    gajus committed Sep 29, 2023
    Copy the full SHA
    789b090 View commit details
  4. fix: abstract getFields

    gajus committed Sep 29, 2023
    Copy the full SHA
    86c08a3 View commit details
  5. Copy the full SHA
    6ccdfa4 View commit details
  6. fix: correct stream types

    gajus committed Sep 29, 2023
    Copy the full SHA
    0f29250 View commit details
  7. feat: rename stream event property from row to data

    BREAKING CHANGE: even types were wrong to being with, the property change would break implementation for untyped consumers
    gajus committed Sep 29, 2023
    Copy the full SHA
    35b9a98 View commit details
  8. Copy the full SHA
    821851c View commit details
Showing with 341 additions and 196 deletions.
  1. +27 −6 .README/QUERY_METHODS.md
  2. +27 −6 README.md
  3. +34 −27 src/connectionMethods/query.ts
  4. +108 −108 src/connectionMethods/stream.ts
  5. +3 −3 src/routines/executeQuery.ts
  6. +7 −3 src/types.ts
  7. +135 −43 test/slonik/integration/stream.ts
33 changes: 27 additions & 6 deletions .README/QUERY_METHODS.md
Original file line number Diff line number Diff line change
@@ -172,23 +172,44 @@ Example:

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

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.
33 changes: 27 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -2469,23 +2469,44 @@ Example:

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

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>
61 changes: 34 additions & 27 deletions src/connectionMethods/query.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { type ExecutionRoutine } from '../routines/executeQuery';
import { executeQuery } from '../routines/executeQuery';
import {
type Field,
@@ -7,6 +8,38 @@ import {
} from '../types';
import { type QueryResult as PgQueryResult } from 'pg';

const executionRoutine: ExecutionRoutine = async (
finalConnection,
finalSql,
finalValues,
) => {
const result: PgQueryResult & { notices?: Notice[] } =
await finalConnection.query(
finalSql,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
finalValues as any[],
);

const fields: Field[] = [];

if (result.fields) {
for (const field of result.fields) {
fields.push({
dataTypeId: field.dataTypeID,
name: field.name,
});
}
}

return {
command: result.command as QueryResult<unknown>['command'],
fields,
notices: result.notices ?? [],
rowCount: result.rowCount || 0,
rows: result.rows || [],
};
};

export const query: InternalQueryMethod = async (
connectionLogger,
connection,
@@ -20,32 +53,6 @@ export const query: InternalQueryMethod = async (
clientConfiguration,
slonikSql,
inheritedQueryId,
async (finalConnection, finalSql, finalValues) => {
const result: PgQueryResult & { notices?: Notice[] } =
await finalConnection.query(
finalSql,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
finalValues as any[],
);

const fields: Field[] = [];

if (result.fields) {
for (const field of result.fields) {
fields.push({
dataTypeId: field.dataTypeID,
name: field.name,
});
}
}

return {
command: result.command as QueryResult<unknown>['command'],
fields,
notices: result.notices ?? [],
rowCount: result.rowCount || 0,
rows: result.rows || [],
};
},
executionRoutine,
);
};
Loading