1 import { BaseRelationship } from "./base"
2 import { RelationNullableModifier } from "./nullable"
3
4 /**
5 * HasOne relationship defines a relationship between two entities
6 * where the owner of the relationship has exactly one instance
7 * of the related entity.
8 *
9 * For example: A user HasOne profile
10 *
11 * You may use the "BelongsTo" relationship to define the inverse
12 * of the "HasOne" relationship
13 */
14 export class HasOneWithForeignKey<
15 T,
16 const OptionalForeignKeyName extends string | undefined = undefined
17 > extends BaseRelationship<T> {
18 type = "hasOneWithFK" as const
19 declare $foreignKey: true
20 declare $foreignKeyName: OptionalForeignKeyName
21
22 static isHasOneWithForeignKey<T>(
23 relationship: any
24 ): relationship is HasOneWithForeignKey<T, any> {
25 return relationship?.type === "hasOneWithFK"
26 }
27
28 /**
29 * Apply nullable modifier on the schema
30 */
31 nullable() {
32 return new RelationNullableModifier<
33 T,
34 HasOneWithForeignKey<T, OptionalForeignKeyName>,
35 true
36 >(this)
37 }
38 }