1 import { PropertyMetadata, PropertyType } from "@medusajs/types"
2 import { BaseSchemaProperty } from "../../schema"
3 import { ComputedProperty } from "./computed"
4 import { NullableModifier } from "./nullable"
5
6 /**
7 * The BaseProperty class offers shared affordances to define
8 * property classes for DML entities. Extends the shared schema
9 * base with DML-specific index / unique / computed modifiers.
10 */
11 export abstract class BaseProperty<T>
12 extends BaseSchemaProperty<T>
13 implements PropertyType<T>
14 {
15 /**
16 * Defined indexes and relationships
17 */
18 #indexes: PropertyMetadata["indexes"] = []
19 #relationships: PropertyMetadata["relationships"] = []
20
21 /**
22 * Default value for the property
23 */
24 #defaultValue?: T
25
26 /**
27 * The runtime dataType for the schema. It is not the same as
28 * the "$dataType".
29 */
30 protected abstract dataType: PropertyMetadata<T>["dataType"]
31
32 /**
33 * This method indicates that a property's value can be `null`.
34 *
35 * @example
36 * import { model } from "@medusajs/framework/utils"
37 *
38 * const MyCustom = model.define("my_custom", {
39 * price: model.bigNumber().nullable(),
40 * // ...
41 * })
42 *
43 * export default MyCustom
44 *
45 * @customNamespace Property Configuration Methods
46 */
47 nullable() {
48 return new NullableModifier<T, this>(this)
49 }
50
51 /**
52 * This method indicated that the property is a computed property.
53 * Computed properties are not stored in the database but are
54 * computed on the fly.
55 *
56 * @example
57 * import { model } from "@medusajs/framework/utils"
58 *
59 * const MyCustom = model.define("my_custom", {
60 * calculated_price: model.bigNumber().computed(),
61 * // ...
62 * })
63 *
64 * export default MyCustom
65 *
66 * @customNamespace Property Configuration Methods
67 */
68 computed() {
69 return new ComputedProperty<T | null, this>(this)
70 }
71
72 /**
73 * This method defines an index on a property.
74 *
75 * @param {string} name - The index's name. If not provided,
76 * Medusa generates the name.
77 *
78 * @example
79 * import { model } from "@medusajs/framework/utils"
80 *
81 * const MyCustom = model.define("my_custom", {
82 * id: model.id(),
83 * name: model.text().index(
84 * "IDX_MY_CUSTOM_NAME"
85 * ),
86 * })
87 *
88 * export default MyCustom
89 *
90 * @customNamespace Property Configuration Methods
91 */
92 index(name?: string) {
93 this.#indexes.push({ name, type: "index" })
94 return this
95 }
96
97 /**
98 * This method indicates that a property's value must be unique in the database.
99 * A unique index is created on the property.
100 *
101 * @param {string} name - The unique index's name. If not provided,
102 * Medusa generates the name.
103 *
104 * @example
105 * import { model } from "@medusajs/framework/utils"
106 *
107 * const User = model.define("user", {
108 * email: model.text().unique(),
109 * // ...
110 * })
111 *
112 * export default User
113 *
114 * @customNamespace Property Configuration Methods
115 */
116 unique(name?: string) {
117 this.#indexes.push({ name, type: "unique" })
118 return this
119 }
120
121 /**
122 * This method defines the default value of a property.
123 *
124 * @param {T} value - The default value.
125 *
126 * @example
127 * import { model } from "@medusajs/framework/utils"
128 *
129 * const MyCustom = model.define("my_custom", {
130 * color: model
131 * .enum(["black", "white"])
132 * .default("black"),
133 * age: model
134 * .number()
135 * .default(0),
136 * // ...
137 * })
138 *
139 * export default MyCustom
140 *
141 * @customNamespace Property Configuration Methods
142 */
143 default(value: T) {
144 this.#defaultValue = value
145 return this
146 }
147
148 /**
149 * Returns the serialized metadata
150 */
151 parse(fieldName: string): PropertyMetadata<T> {
152 const base = super.parse(fieldName)
153 return {
154 ...base,
155 dataType: this.dataType,
156 defaultValue: this.#defaultValue,
157 indexes: this.#indexes,
158 relationships: this.#relationships,
159 }
160 }
161 }