1 import { model } from "@medusajs/framework/utils"
2 import { Product } from "./index"
3 import ProductOptionValue from "./product-option-value"
4
5 /**
6 * A product option, such as "Color" or "Size", that organizes product variants.
7 */
8 const ProductOption = model
9 .define("ProductOption", {
10 id: model.id({ prefix: "opt" }).primaryKey(),
11 /**
12 * The option's display name.
13 */
14 title: model.text().searchable().translatable(),
15 /**
16 * Whether the option is exclusive to a single product. Exclusive options
17 * are removed when they are no longer associated with any product.
18 * @since 2.16.0
19 */
20 is_exclusive: model.boolean().default(false),
21 /**
22 * Holds custom key-value pairs associated with the option.
23 */
24 metadata: model.json().nullable(),
25 /**
26 * The products associated with this option.
27 * @since 2.16.0
28 */
29 products: model.manyToMany(() => Product),
30 /**
31 * The option's values.
32 */
33 values: model.hasMany(() => ProductOptionValue, {
34 mappedBy: "option",
35 }),
36 })
37 .cascades({
38 delete: ["values"],
39 detach: ["products"],
40 })
41 .indexes([
42 {
43 name: "IDX_product_option_global_title_unique",
44 on: ["title"],
45 unique: true,
46 where: "deleted_at IS NULL AND is_exclusive = false",
47 },
48 ])
49
50 export default ProductOption