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