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

Method stream

packages/core/src/EntityManager.ts:334–385  ·  view source on GitHub ↗

* Finds all entities and returns an async iterable (async generator) that yields results one by one. * The results are merged and mapped to entity instances, without adding them to the identity map. * You can disable merging by passing the options `{ mergeResults: false }`. * With `mergeRes

(
    entityName: EntityName<Entity>,
    options: WithUsingOptions<
      StreamOptions<NoInfer<Entity>, Hint, Fields, Excludes>,
      NoInfer<Entity>,
      Using
    > = {} as any,
  )

Source from the content-addressed store, hash-verified

332 * ```
333 */
334 async *stream<
335 Entity extends object,
336 Hint extends string = never,
337 Fields extends string = never,
338 Excludes extends string = never,
339 Using extends string = never,
340 >(
341 entityName: EntityName<Entity>,
342 options: WithUsingOptions<
343 StreamOptions<NoInfer<Entity>, Hint, Fields, Excludes>,
344 NoInfer<Entity>,
345 Using
346 > = {} as any,
347 ): AsyncIterableIterator<Loaded<Entity, Hint, Fields, Excludes>> {
348 const em = this.getContext();
349 em.prepareOptions(options);
350 (options as Dictionary).strategy = 'joined';
351 await em.tryFlush(entityName, options);
352 const where = (await em.processWhere(entityName, options.where ?? {}, options, 'read')) as FilterQuery<Entity>;
353 validateParams(where);
354 options.orderBy = options.orderBy || {};
355 options.populate = (await em.preparePopulate(entityName, options)) as any;
356 const meta = this.metadata.get<Entity>(entityName);
357 em.validateIndexUsage(meta, options.where ?? {}, options);
358 options = { ...options };
359 // save the original hint value so we know it was infer/all
360 (options as Dictionary)._populateWhere = options.populateWhere ?? this.config.get('populateWhere');
361 options.populateWhere = this.createPopulateWhere({ ...where } as ObjectQuery<Entity>, options);
362 options.populateFilter = await this.getJoinedFilters(meta, options);
363 const stream = em.driver.stream(entityName, where, {
364 ctx: em.#transactionContext,
365 mapResults: false,
366 ...options,
367 } as StreamOptions<Entity>);
368
369 for await (const data of stream) {
370 const fork = em.fork();
371 const entity = fork.#entityFactory.create(entityName, data as EntityData<Entity>, {
372 refresh: options.refresh,
373 schema: options.schema,
374 convertCustomTypes: true,
375 }) as Loaded<Entity, Hint, Fields, Excludes>;
376 helper(entity).setSerializationContext({
377 populate: options.populate,
378 fields: options.fields,
379 exclude: options.exclude,
380 } as any);
381 await fork.#unitOfWork.dispatchOnLoadEvent();
382 fork.clear();
383 yield entity;
384 }
385 }
386
387 /**
388 * Finds all entities of given type, optionally matching the `where` condition provided in the `options` parameter.

Callers

nothing calls this directly

Calls 15

getContextMethod · 0.95
createPopulateWhereMethod · 0.95
getJoinedFiltersMethod · 0.95
validateParamsFunction · 0.85
helperFunction · 0.85
tryFlushMethod · 0.80
preparePopulateMethod · 0.80
validateIndexUsageMethod · 0.80
forkMethod · 0.80
dispatchOnLoadEventMethod · 0.80
getMethod · 0.65
streamMethod · 0.65

Tested by

no test coverage detected