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