1 import { model, ProductUtils } from "@medusajs/framework/utils"
2
3 import ProductCategory from "./product-category"
4 import ProductCollection from "./product-collection"
5 import ProductImage from "./product-image"
6 import ProductOption from "./product-option"
7 import ProductProductOption from "./product-product-option"
8 import ProductTag from "./product-tag"
9 import ProductType from "./product-type"
10 import ProductVariant from "./product-variant"
11
12 /**
13 * A product in the catalog.
14 */
15 const Product = model
16 .define("Product", {
17 id: model.id({ prefix: "prod" }).primaryKey(),
18 /**
19 * The product's display title.
20 */
21 title: model.text().searchable().translatable(),
22 /**
23 * The product's unique URL handle.
24 */
25 handle: model.text(),
26 /**
27 * The product's subtitle.
28 */
29 subtitle: model.text().searchable().translatable().nullable(),
30 /**
31 * The product's description.
32 */
33 description: model.text().searchable().translatable().nullable(),
34 /**
35 * Whether the product is a gift card.
36 */
37 is_giftcard: model.boolean().default(false),
38 /**
39 * The product's status.
40 */
41 status: model
42 .enum(ProductUtils.ProductStatus)
43 .default(ProductUtils.ProductStatus.DRAFT),
44 /**
45 * The product's thumbnail image URL.
46 */
47 thumbnail: model.text().nullable(),
48 /**
49 * The product's weight.
50 */
51 weight: model.float().nullable(),
52 /**
53 * The product's length.
54 */
55 length: model.float().nullable(),
56 /**
57 * The product's height.
58 */
59 height: model.float().nullable(),
60 /**
61 * The product's width.
62 */
63 width: model.float().nullable(),
64 /**
65 * The product's country of origin.
66 */
67 origin_country: model.text().nullable(),
68 /**
69 * The product's Harmonized System code.
70 */
71 hs_code: model.text().nullable(),
72 /**
73 * The product's MID code.
74 */
75 mid_code: model.text().nullable(),
76 /**
77 * The product's material.
78 */
79 material: model.text().translatable().nullable(),
80 /**
81 * Whether the product is eligible for discounts.
82 */
83 discountable: model.boolean().default(true),
84 /**
85 * The product's external identifier.
86 */
87 external_id: model.text().nullable(),
88 /**
89 * The product's metadata.
90 */
91 metadata: model.json().nullable(),
92 /**
93 * The product's variants.
94 */
95 variants: model
96 .hasMany(() => ProductVariant, {
97 mappedBy: "product",
98 })
99 .searchable(),
100 /**
101 * The associated product type.
102 */
103 type: model
104 .belongsTo(() => ProductType, {
105 mappedBy: "products",
106 })
107 .nullable(),
108 /**
109 * The associated product tags.
110 */
111 tags: model.manyToMany(() => ProductTag, {
112 mappedBy: "products",
113 pivotTable: "product_tags",
114 }),
115 options: model.manyToMany(() => ProductOption, {
116 pivotEntity: () => ProductProductOption,
117 }),
118 /**
119 * @since 2.16.0
120 */
121 product_options: model.hasMany(() => ProductProductOption, {
122 mappedBy: "product",
123 }),
124 /**
125 * The product's images.
126 */
127 images: model.hasMany(() => ProductImage, {
128 mappedBy: "product",
129 }),
130 /**
131 * The associated product collection.
132 */
133 collection: model
134 .belongsTo(() => ProductCollection, {
135 mappedBy: "products",
136 })
137 .nullable(),
138 /**
139 * The associated product categories.
140 */
141 categories: model.manyToMany(() => ProductCategory, {
142 pivotTable: "product_category_product",
143 mappedBy: "products",
144 }),
145 })
146 .cascades({
147 delete: ["variants", "images"],
148 detach: ["options"],
149 })
150 .indexes([
151 {
152 name: "IDX_product_handle_unique",
153 on: ["handle"],
154 unique: true,
155 where: "deleted_at IS NULL",
156 },
157 {
158 name: "IDX_product_type_id",
159 on: ["type_id"],
160 unique: false,
161 where: "deleted_at IS NULL",
162 },
163 {
164 name: "IDX_product_collection_id",
165 on: ["collection_id"],
166 unique: false,
167 where: "deleted_at IS NULL",
168 },
169 {
170 name: "IDX_product_status",
171 on: ["status"],
172 unique: false,
173 where: "deleted_at IS NULL",
174 },
175 ])
176
177 export default Product