1 import { model } from "@medusajs/framework/utils"
2 import { Product, ProductImage, ProductOptionValue } from "@models"
3 import ProductVariantProductImage from "./product-variant-product-image"
4
5 const ProductVariant = model
6 .define("ProductVariant", {
7 id: model.id({ prefix: "variant" }).primaryKey(),
8 title: model.text().searchable().translatable(),
9 sku: model.text().searchable().nullable(),
10 barcode: model.text().searchable().nullable(),
11 ean: model.text().searchable().nullable(),
12 upc: model.text().searchable().nullable(),
13 allow_backorder: model.boolean().default(false),
14 manage_inventory: model.boolean().default(true),
15 hs_code: model.text().nullable(),
16 origin_country: model.text().nullable(),
17 mid_code: model.text().nullable(),
18 material: model.text().translatable().nullable(),
19 weight: model.float().nullable(),
20 length: model.float().nullable(),
21 height: model.float().nullable(),
22 width: model.float().nullable(),
23 metadata: model.json().nullable(),
24 variant_rank: model.number().default(0).nullable(),
25 /**
26 * @since 2.11.2
27 */
28 thumbnail: model.text().nullable(),
29 product: model
30 .belongsTo(() => Product, {
31 mappedBy: "variants",
32 })
33 .searchable()
34 .nullable(),
35 /**
36 * @since 2.11.2
37 */
38 images: model.manyToMany(() => ProductImage, {
39 mappedBy: "variants",
40 pivotEntity: () => ProductVariantProductImage,
41 }),
42 options: model.manyToMany(() => ProductOptionValue, {
43 pivotTable: "product_variant_option",
44 mappedBy: "variants",
45 joinColumn: "variant_id",
46 inverseJoinColumn: "option_value_id",
47 }),
48 })
49 .indexes([
50 {
51 name: "IDX_product_variant_id_product_id",
52 on: ["id", "product_id"],
53 where: "deleted_at IS NULL",
54 },
55 {
56 name: "IDX_product_variant_product_id",
57 on: ["product_id"],
58 where: "deleted_at IS NULL",
59 },
60 {
61 name: "IDX_product_variant_sku_unique",
62 on: ["sku"],
63 unique: true,
64 where: "deleted_at IS NULL",
65 },
66 {
67 name: "IDX_product_variant_barcode_unique",
68 on: ["barcode"],
69 unique: true,
70 where: "deleted_at IS NULL",
71 },
72 {
73 name: "IDX_product_variant_ean_unique",
74 on: ["ean"],
75 unique: true,
76 where: "deleted_at IS NULL",
77 },
78 {
79 name: "IDX_product_variant_upc_unique",
80 on: ["upc"],
81 unique: true,
82 where: "deleted_at IS NULL",
83 },
84 ])
85
86 export default ProductVariant