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

Method loadOrFail

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

* Ensures the underlying entity is loaded first (without reloading it if it already is loaded). * Returns the entity or throws an error just like `em.findOneOrFail()` (and respects the same config options).

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

Source from the content-addressed store, hash-verified

171 * Returns the entity or throws an error just like `em.findOneOrFail()` (and respects the same config options).
172 */
173 async loadOrFail<TT extends T, P extends string = never, F extends string = never, E extends string = never>(
174 options: LoadReferenceOrFailOptions<TT, P, F, E> = {},
175 ): Promise<Loaded<TT, P, F, E>> {
176 const ret = await this.load(options);
177
178 if (!ret) {
179 const wrapped = helper(this.entity);
180 options.failHandler ??= wrapped.__em!.config.get('findOneOrFailHandler');
181 const entityName = this.entity.constructor.name;
182 const where = wrapped.getPrimaryKey() as any;
183 throw options.failHandler(entityName, where);
184 }
185
186 return ret;
187 }
188
189 private set<TT extends T>(entity: TT | Ref<TT>): void {
190 this.entity = Reference.unwrapReference(entity as T & object);

Callers 1

loadPropertyMethod · 0.95

Calls 4

loadMethod · 0.95
helperFunction · 0.85
getMethod · 0.65
getPrimaryKeyMethod · 0.65

Tested by

no test coverage detected