MCPcopy Create free account
hub / github.com/mikro-orm/mikro-orm / stream

Function stream

packages/sql/src/AbstractSqlConnection.ts:311–353  ·  view source on GitHub ↗

Executes a SQL query and returns an async iterable that yields results row by row.

(
    query: string | NativeQueryBuilder | RawQueryFragment,
    params: readonly unknown[] = [],
    ctx?: Transaction<Kysely<any>>,
    loggerContext?: LoggingOptions,
    chunkSize?: number,
  )

Source from the content-addressed store, hash-verified

309
310 /** Executes a SQL query and returns an async iterable that yields results row by row. */
311 async *stream<T extends EntityData<AnyEntity>>(
312 query: string | NativeQueryBuilder | RawQueryFragment,
313 params: readonly unknown[] = [],
314 ctx?: Transaction<Kysely<any>>,
315 loggerContext?: LoggingOptions,
316 chunkSize?: number,
317 ): AsyncIterableIterator<T> {
318 await this.ensureConnection();
319 const q = this.prepareQuery(query, params);
320 const sql = this.getSql(q.query, q.formatted, loggerContext);
321 const { abort, loggerContext: cleanCtx } = extractAbortOptions(loggerContext);
322
323 // construct the compiled query manually with `kind: 'SelectQueryNode'` to avoid sqlite validation for select queries when streaming
324 const compiled = {
325 query: {
326 kind: 'SelectQueryNode',
327 },
328 sql: q.formatted,
329 parameters: [],
330 } as unknown as CompiledQuery;
331
332 try {
333 const res = (ctx ?? this.getClient())
334 .getExecutor()
335 .stream(compiled, chunkSize ?? 100, abort ? { signal: abort.signal } : undefined);
336
337 this.logQuery(sql, {
338 sql,
339 params,
340 ...cleanCtx,
341 affected: Utils.isPlainObject<QueryResult>(res) ? res.affectedRows : undefined,
342 });
343
344 for await (const items of res) {
345 for (const row of this.transformRawResult(items, 'all') as T[]) {
346 yield row;
347 }
348 }
349 } catch (e) {
350 this.logQuery(sql, { sql, params, ...cleanCtx, level: 'error' });
351 throw e;
352 }
353 }
354
355 /** @inheritDoc */
356 override async executeDump(dump: string): Promise<void> {

Callers

nothing calls this directly

Calls 6

extractAbortOptionsFunction · 0.85
getClientMethod · 0.80
isPlainObjectMethod · 0.80
streamMethod · 0.65
logQueryMethod · 0.65
transformRawResultMethod · 0.45

Tested by

no test coverage detected