1 import { RelationshipType } from "@medusajs/types"
2 import { IsRelationship } from "./base"
3
4 const IsNullableModifier = Symbol.for("isNullableModifier")
5
6 /**
7 * Nullable modifier marks a schema node as nullable
8 */
9 export class RelationNullableModifier<
10 T,
11 Relation extends RelationshipType<T>,
12 ForeignKey extends boolean
13 > implements RelationshipType<T | null>
14 {
15 [IsNullableModifier]: true = true;
16 [IsRelationship]: true = true
17
18 static isNullableModifier<T>(
19 modifier: any
20 ): modifier is RelationNullableModifier<T, any, any> {
21 return !!modifier?.[IsNullableModifier]
22 }
23
24 declare type: Relation["type"]
25
26 /**
27 * A type-only property to infer the JavScript data-type
28 * of the schema property
29 */
30 declare $dataType: T | null
31 declare $foreignKey: ForeignKey
32
33 /**
34 * The parent schema on which the nullable modifier is
35 * applied
36 */
37 #relation: Relation
38
39 constructor(relation: Relation) {
40 this.#relation = relation
41 this.type = relation.type
42 }
43
44 /**
45 * Returns the serialized metadata
46 */
47 parse(fieldName: string) {
48 const relation = this.#relation.parse(fieldName)
49 relation.nullable = true
50 return relation
51 }
52 }