| 422 | * shortcut for `wrap(entity).toReference()` |
| 423 | */ |
| 424 | export 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: |