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

Method stream

packages/sql/src/query/QueryBuilder.ts:2538–2585  ·  view source on GitHub ↗

* Executes the query and returns an async iterable (async generator) that yields results one by one. * By default, the results are merged and mapped to entity instances, without adding them to the identity map. * You can disable merging and mapping by passing the options `{ mergeResults: false

(options?: QBStreamOptions)

Source from the content-addressed store, hash-verified

2536 * ```
2537 */
2538 async *stream(options?: QBStreamOptions): AsyncIterableIterator<Loaded<Entity, Hint, Fields>> {
2539 options ??= {};
2540 options.mergeResults ??= true;
2541 options.mapResults ??= true;
2542 const chunkSize = options.chunkSize ?? 100;
2543
2544 const query = this.toQuery();
2545 const loggerContext = { id: this.em?.id, ...this.loggerContext, ...this.#abortOptions };
2546 const res = this.getConnection().stream(query.sql, query.params, this.context, loggerContext, chunkSize);
2547 const meta = this.mainAlias.meta;
2548
2549 if (options.rawResults || !meta) {
2550 yield* res as AsyncIterableIterator<Loaded<Entity, Hint, Fields>>;
2551 return;
2552 }
2553
2554 const joinedProps = this.driver.joinedProps(meta, this.#state.populate);
2555 const stack = [] as EntityData<Entity>[];
2556 const hash = (data: EntityData<Entity>) => {
2557 return Utils.getPrimaryKeyHash(meta.primaryKeys.map(pk => data[pk as EntityKey]));
2558 };
2559
2560 for await (const row of res) {
2561 const mapped = this.driver.mapResult<Entity>(row as Entity, meta, this.#state.populate, this as any)!;
2562
2563 if (!options.mergeResults || joinedProps.length === 0) {
2564 yield this.mapResult(mapped, options.mapResults);
2565 continue;
2566 }
2567
2568 if (stack.length > 0 && hash(stack[stack.length - 1]) !== hash(mapped)) {
2569 const res = this.driver.mergeJoinedResult(stack, this.mainAlias.meta, joinedProps);
2570
2571 for (const row of res) {
2572 yield this.mapResult(row, options.mapResults);
2573 }
2574
2575 stack.length = 0;
2576 }
2577
2578 stack.push(mapped);
2579 }
2580
2581 if (stack.length > 0) {
2582 const merged = this.driver.mergeJoinedResult(stack, this.mainAlias.meta, joinedProps);
2583 yield this.mapResult(merged[0], options.mapResults);
2584 }
2585 }
2586
2587 /**
2588 * Alias for `qb.getResultList()`

Callers

nothing calls this directly

Calls 6

toQueryMethod · 0.95
getConnectionMethod · 0.95
mapResultMethod · 0.95
pushMethod · 0.80
streamMethod · 0.65
mapResultMethod · 0.65

Tested by

no test coverage detected