MCPcopy
hub / github.com/drizzle-team/drizzle-orm / iterator

Method iterator

drizzle-orm/src/mysql2/session.ts:145–195  ·  view source on GitHub ↗
(
		placeholderValues: Record<string, unknown> = {},
	)

Source from the content-addressed store, hash-verified

143 }
144
145 async *iterator(
146 placeholderValues: Record<string, unknown> = {},
147 ): AsyncGenerator<T['execute'] extends any[] ? T['execute'][number] : T['execute']> {
148 const params = fillPlaceholders(this.params, placeholderValues);
149 const conn = ((isPool(this.client) ? await this.client.getConnection() : this.client) as {} as {
150 connection: CallbackConnection;
151 }).connection;
152
153 const { fields, query, rawQuery, joinsNotNullableMap, client, customResultMapper } = this;
154 const hasRowsMapper = Boolean(fields || customResultMapper);
155 const driverQuery = hasRowsMapper ? conn.query(query, params) : conn.query(rawQuery, params);
156
157 const stream = driverQuery.stream();
158
159 function dataListener() {
160 stream.pause();
161 }
162
163 stream.on('data', dataListener);
164
165 try {
166 const onEnd = once(stream, 'end');
167 const onError = once(stream, 'error');
168
169 while (true) {
170 stream.resume();
171 const row = await Promise.race([onEnd, onError, new Promise((resolve) => stream.once('data', resolve))]);
172 if (row === undefined || (Array.isArray(row) && row.length === 0)) {
173 break;
174 } else if (row instanceof Error) { // eslint-disable-line no-instanceof/no-instanceof
175 throw row;
176 } else {
177 if (hasRowsMapper) {
178 if (customResultMapper) {
179 const mappedRow = customResultMapper([row as unknown[]]);
180 yield (Array.isArray(mappedRow) ? mappedRow[0] : mappedRow);
181 } else {
182 yield mapResultRow(fields!, row as unknown[], joinsNotNullableMap);
183 }
184 } else {
185 yield row as T['execute'];
186 }
187 }
188 }
189 } finally {
190 stream.off('data', dataListener);
191 if (isPool(client)) {
192 conn.end();
193 }
194 }
195 }
196}
197
198export interface MySql2SessionOptions {

Callers

nothing calls this directly

Calls 5

fillPlaceholdersFunction · 0.90
mapResultRowFunction · 0.90
isPoolFunction · 0.70
queryMethod · 0.45
onMethod · 0.45

Tested by

no test coverage detected