Skip to content

Commit 66889e2

Browse files
committed
perf: reduce microtasks
1 parent abb5764 commit 66889e2

File tree

4 files changed

+34
-45
lines changed

4 files changed

+34
-45
lines changed

.changeset/tidy-geese-sin.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"slonik": patch
3+
---
4+
5+
reduce microtasks

packages/slonik/src/connectionMethods/query.ts

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import type {
77
QueryResultRow,
88
} from '../types.js';
99
import type { DriverNotice, DriverQueryResult } from '@slonik/driver';
10-
import { SlonikError } from '@slonik/errors';
1110

1211
const executionRoutine: ExecutionRoutine = async (
1312
finalConnection,
@@ -42,30 +41,22 @@ const executionRoutine: ExecutionRoutine = async (
4241
};
4342
};
4443

45-
export const query: InternalQueryMethod = async (
44+
export const query: InternalQueryMethod = (
4645
connectionLogger,
4746
connection,
4847
clientConfiguration,
4948
slonikSql,
5049
inheritedQueryId,
5150
integrityValidation,
5251
) => {
53-
try {
54-
return await executeQuery(
55-
connectionLogger,
56-
connection,
57-
clientConfiguration,
58-
slonikSql,
59-
inheritedQueryId,
60-
executionRoutine,
61-
false,
62-
integrityValidation,
63-
);
64-
} catch (error) {
65-
if (error instanceof SlonikError) {
66-
connection.events.emit('error', error);
67-
}
68-
69-
throw error;
70-
}
52+
return executeQuery(
53+
connectionLogger,
54+
connection,
55+
clientConfiguration,
56+
slonikSql,
57+
inheritedQueryId,
58+
executionRoutine,
59+
false,
60+
integrityValidation,
61+
);
7162
};

packages/slonik/src/connectionMethods/stream.ts

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import type {
1010
StreamHandler,
1111
} from '../types.js';
1212
import type { DriverStreamResult } from '@slonik/driver';
13-
import { SlonikError } from '@slonik/errors';
1413
import { Transform } from 'node:stream';
1514
import { pipeline } from 'node:stream/promises';
1615

@@ -148,29 +147,19 @@ export const stream: InternalStreamFunction = async (
148147
onStream,
149148
uid,
150149
) => {
151-
try {
152-
const result = await executeQuery(
153-
connectionLogger,
154-
connection,
155-
clientConfiguration,
156-
slonikSql,
157-
uid,
158-
createExecutionRoutine(clientConfiguration, onStream),
159-
true,
160-
);
161-
162-
if (result.type === 'QueryResult') {
163-
throw new Error(
164-
'Query result cannot be returned in a streaming context.',
165-
);
166-
}
167-
168-
return result;
169-
} catch (error) {
170-
if (error instanceof SlonikError) {
171-
connection.events.emit('error', error);
172-
}
173-
174-
throw error;
150+
const result = await executeQuery(
151+
connectionLogger,
152+
connection,
153+
clientConfiguration,
154+
slonikSql,
155+
uid,
156+
createExecutionRoutine(clientConfiguration, onStream),
157+
true,
158+
);
159+
160+
if (result.type === 'QueryResult') {
161+
throw new Error('Query result cannot be returned in a streaming context.');
175162
}
163+
164+
return result;
176165
};

packages/slonik/src/routines/executeQuery.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,10 @@ export const executeQuery = async (
711711
stream,
712712
);
713713
} catch (error) {
714+
if (error instanceof SlonikError) {
715+
connection.events.emit('error', error);
716+
}
717+
714718
span.recordException(error);
715719
span.setStatus({
716720
code: SpanStatusCode.ERROR,

0 commit comments

Comments
 (0)