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

Method load

packages/core/src/entity/Reference.ts:139–167  ·  view source on GitHub ↗

* Ensures the underlying entity is loaded first (without reloading it if it already is loaded). Returns the entity. * If the entity is not found in the database (e.g. it was deleted in the meantime, or currently active filters disallow loading of it) * the method returns `null`. Use `loadOrFai

(
    options: LoadReferenceOptions<TT, P, F, E> = {},
  )

Source from the content-addressed store, hash-verified

137 * the method returns `null`. Use `loadOrFail()` if you want an error to be thrown in such a case.
138 */
139 async load<TT extends T, P extends string = never, F extends string = never, E extends string = never>(
140 options: LoadReferenceOptions<TT, P, F, E> = {},
141 ): Promise<Loaded<TT, P, F, E> | null> {
142 const wrapped = helper(this.entity as TT & object);
143
144 if (!wrapped.__em) {
145 return this.entity as Loaded<TT, P, F, E>;
146 }
147
148 options = { ...options, filters: QueryHelper.mergePropertyFilters(this.property?.filters, options.filters)! };
149
150 if (this.isInitialized() && !options.refresh && options.populate) {
151 await wrapped.__em.populate(this.entity, options.populate as any, options as any);
152 }
153
154 if (!this.isInitialized() || options.refresh) {
155 if (
156 options.dataloader ??
157 [DataloaderType.ALL, DataloaderType.REFERENCE].includes(wrapped.__em.config.getDataloaderType())
158 ) {
159 const dataLoader = await wrapped.__em.getDataLoader('ref');
160 return dataLoader.load([this, options]);
161 }
162
163 return wrapped.init(options);
164 }
165
166 return this.entity as Loaded<TT, P, F, E>;
167 }
168
169 /**
170 * Ensures the underlying entity is loaded first (without reloading it if it already is loaded).

Callers 1

loadOrFailMethod · 0.95

Calls 8

isInitializedMethod · 0.95
helperFunction · 0.85
mergePropertyFiltersMethod · 0.80
getDataloaderTypeMethod · 0.80
populateMethod · 0.65
loadMethod · 0.65
initMethod · 0.65
getDataLoaderMethod · 0.45

Tested by

no test coverage detected