Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MySQL]: optimize result set streaming from iterator() #4126

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 12 additions & 30 deletions drizzle-orm/src/mysql2/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import type {
ResultSetHeader,
RowDataPacket,
} from 'mysql2/promise';
import { once } from 'node:events';
import { Column } from '~/column.ts';
import { entityKind, is } from '~/entity.ts';
import type { Logger } from '~/logger.ts';
Expand Down Expand Up @@ -137,43 +136,26 @@ export class MySql2PreparedQuery<T extends MySqlPreparedQueryConfig> extends MyS
}).connection;

const { fields, query, rawQuery, joinsNotNullableMap, client, customResultMapper } = this;
const hasRowsMapper = Boolean(fields || customResultMapper);
const driverQuery = hasRowsMapper ? conn.query(query, params) : conn.query(rawQuery, params);

const stream = driverQuery.stream();

function dataListener() {
stream.pause();
}

stream.on('data', dataListener);

try {
const onEnd = once(stream, 'end');
const onError = once(stream, 'error');
const hasRowsMapper = Boolean(fields || customResultMapper);
const driverQuery = hasRowsMapper ? conn.query(query, params) : conn.query(rawQuery, params);

while (true) {
stream.resume();
const row = await Promise.race([onEnd, onError, new Promise((resolve) => stream.once('data', resolve))]);
if (row === undefined || (Array.isArray(row) && row.length === 0)) {
break;
} else if (row instanceof Error) { // eslint-disable-line no-instanceof/no-instanceof
throw row;
} else {
if (hasRowsMapper) {
if (customResultMapper) {
const mappedRow = customResultMapper([row as unknown[]]);
yield (Array.isArray(mappedRow) ? mappedRow[0] : mappedRow);
} else {
yield mapResultRow(fields!, row as unknown[], joinsNotNullableMap);
}
const stream = driverQuery.stream();

for await (const row of stream) {
if (hasRowsMapper) {
if (customResultMapper) {
const mappedRow = customResultMapper([row as unknown[]]);
yield (Array.isArray(mappedRow) ? mappedRow[0] : mappedRow);
} else {
yield row as T['execute'];
yield mapResultRow(fields!, row as unknown[], joinsNotNullableMap);
}
} else {
yield row as T['execute'];
}
}
} finally {
stream.off('data', dataListener);
if (isPool(client)) {
conn.end();
}
Expand Down