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