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

Function ref

packages/core/src/entity/Reference.ts:424–449  ·  view source on GitHub ↗
(
  entityOrType?: T | Ref<T> | EntityClass<T> | null,
  pk?: T | PKV | null,
)

Source from the content-addressed store, hash-verified

422 * shortcut for `wrap(entity).toReference()`
423 */
424export function ref<T, PKV extends Primary<T> = Primary<T>>(
425 entityOrType?: T | Ref<T> | EntityClass<T> | null,
426 pk?: T | PKV | null,
427): Ref<T> | undefined | null {
428 if (entityOrType == null) {
429 return entityOrType as unknown as null;
430 }
431
432 if (Utils.isEntity(entityOrType, true)) {
433 return helper(entityOrType).toReference() as Ref<T>;
434 }
435
436 if (Utils.isEntity(pk, true)) {
437 return helper(pk).toReference() as Ref<T>;
438 }
439
440 if (arguments.length === 1) {
441 return new ScalarReference<T>(entityOrType, true) as Ref<T>;
442 }
443
444 if (pk == null) {
445 return pk as null;
446 }
447
448 return Reference.createFromPK(entityOrType as EntityClass<T>, pk as PKV) as Ref<T>;
449}
450
451/**
452 * Unwraps a reference to its underlying value, returning the entity or scalar. Works on any of:

Callers 15

types.test.tsFile · 0.90
createEntitiesFunction · 0.90
createEntitiesFunction · 0.90
lazy-ref.test.tsFile · 0.90
createEntitiesFunction · 0.90
constructorMethod · 0.90
constructorMethod · 0.90

Calls 4

helperFunction · 0.85
createFromPKMethod · 0.80
toReferenceMethod · 0.65
isEntityMethod · 0.45

Tested by 15

createEntitiesFunction · 0.72
createEntitiesFunction · 0.72
createEntitiesFunction · 0.72
constructorMethod · 0.72
constructorMethod · 0.72
constructorMethod · 0.72
constructorMethod · 0.72
constructorMethod · 0.72
constructorMethod · 0.72
constructorMethod · 0.72
constructorMethod · 0.72
constructorMethod · 0.72