1 import { BaseProperty } from "./base"
2 import { PrimaryKeyModifier } from "./primary-key"
3
4 const IsIdProperty = Symbol("IsIdProperty")
5
6 /**
7 * The Id property defines a unique identifier for the schema.
8 * Most of the times it will be the primary as well.
9 */
10 export class IdProperty extends BaseProperty<string> {
11 [IsIdProperty] = true
12
13 static isIdProperty(value: any): value is IdProperty {
14 return !!value?.[IsIdProperty] || value?.dataType?.name === "id"
15 }
16
17 dataType: {
18 name: "id"
19 options: {
20 prefix?: string
21 }
22 } = {
23 name: "id",
24 options: {},
25 }
26
27 constructor(options?: { prefix?: string }) {
28 super()
29 this.dataType.options.prefix = options?.prefix
30 }
31
32 /**
33 * This method indicates that the property is the data model's primary key.
34 *
35 * @example
36 * import { model } from "@medusajs/framework/utils"
37 *
38 * const Product = model.define("Product", {
39 * id: model.id().primaryKey(),
40 * // ...
41 * })
42 *
43 * export default Product
44 *
45 * @customNamespace Property Configuration Methods
46 */
47 primaryKey() {
48 return new PrimaryKeyModifier<string, IdProperty>(this)
49 }
50 }