diff --git a/packages/cart/src/models/address.ts b/packages/cart/src/models/address.ts index a603018efa..c26baa66b3 100644 --- a/packages/cart/src/models/address.ts +++ b/packages/cart/src/models/address.ts @@ -6,10 +6,9 @@ import { OnInit, OptionalProps, PrimaryKey, - Property + Property, } from "@mikro-orm/core" - type OptionalAddressProps = DAL.EntityDateColumns // TODO: To be revisited when more clear @Entity({ tableName: "cart_address" }) @@ -20,40 +19,40 @@ export default class Address { id!: string @Property({ columnType: "text", nullable: true }) - customer_id?: string | null + customer_id: string | null = null @Property({ columnType: "text", nullable: true }) - company?: string | null + company: string | null = null @Property({ columnType: "text", nullable: true }) - first_name?: string | null + first_name: string | null = null @Property({ columnType: "text", nullable: true }) - last_name?: string | null + last_name: string | null = null @Property({ columnType: "text", nullable: true }) - address_1?: string | null + address_1: string | null = null @Property({ columnType: "text", nullable: true }) - address_2?: string | null + address_2: string | null = null @Property({ columnType: "text", nullable: true }) - city?: string | null + city: string | null = null @Property({ columnType: "text", nullable: true }) - country_code?: string | null + country_code: string | null = null @Property({ columnType: "text", nullable: true }) - province?: string | null + province: string | null = null @Property({ columnType: "text", nullable: true }) - postal_code?: string | null + postal_code: string | null = null @Property({ columnType: "text", nullable: true }) - phone?: string | null + phone: string | null = null @Property({ columnType: "jsonb", nullable: true }) - metadata?: Record | null + metadata: Record | null = null @Property({ onCreate: () => new Date(), diff --git a/packages/cart/src/models/cart.ts b/packages/cart/src/models/cart.ts index b83d1b534e..42b5db19fc 100644 --- a/packages/cart/src/models/cart.ts +++ b/packages/cart/src/models/cart.ts @@ -5,7 +5,6 @@ import { Cascade, Collection, Entity, - Index, ManyToOne, OnInit, OneToMany, @@ -34,50 +33,54 @@ export default class Cart { nullable: true, index: "IDX_cart_region_id", }) - region_id?: string | null + region_id: string | null = null @Property({ columnType: "text", nullable: true, index: "IDX_cart_customer_id", }) - customer_id?: string | null + customer_id: string | null = null @Property({ columnType: "text", nullable: true, index: "IDX_cart_sales_channel_id", }) - sales_channel_id?: string | null + sales_channel_id: string | null = null @Property({ columnType: "text", nullable: true }) - email?: string | null + email: string | null = null @Property({ columnType: "text", index: "IDX_cart_curency_code" }) currency_code: string - @Index({ name: "IDX_cart_shipping_address_id" }) @Property({ columnType: "text", nullable: true }) shipping_address_id?: string | null - @ManyToOne(() => Address, { + @ManyToOne({ + entity: () => Address, fieldName: "shipping_address_id", nullable: true, + index: "IDX_cart_shipping_address_id", + cascade: [Cascade.PERSIST], }) shipping_address?: Address | null - @Index({ name: "IDX_cart_billing_address_id" }) @Property({ columnType: "text", nullable: true }) billing_address_id?: string | null - @ManyToOne(() => Address, { + @ManyToOne({ + entity: () => Address, fieldName: "billing_address_id", nullable: true, + index: "IDX_cart_billing_address_id", + cascade: [Cascade.PERSIST], }) billing_address?: Address | null @Property({ columnType: "jsonb", nullable: true }) - metadata?: Record | null + metadata: Record | null = null @OneToMany(() => LineItem, (lineItem) => lineItem.cart, { cascade: [Cascade.REMOVE], @@ -89,46 +92,12 @@ export default class Cart { }) shipping_methods = new Collection(this) - /** COMPUTED PROPERTIES - START */ - - // compare_at_item_total?: number - // compare_at_item_subtotal?: number - // compare_at_item_tax_total?: number - - // original_item_total: number - // original_item_subtotal: number - // original_item_tax_total: number - - // item_total: number - // item_subtotal: number - // item_tax_total: number - - // original_total: number - // original_subtotal: number - // original_tax_total: number - - // total: number - // subtotal: number - // tax_total: number - // discount_total: number - // discount_tax_total: number - - // shipping_total: number - // shipping_subtotal: number - // shipping_tax_total: number - - // original_shipping_total: number - // original_shipping_subtotal: number - // original_shipping_tax_total: number - - /** COMPUTED PROPERTIES - END */ - @Property({ onCreate: () => new Date(), columnType: "timestamptz", defaultRaw: "now()", }) - created_at?: Date + created_at: Date @Property({ onCreate: () => new Date(), @@ -136,10 +105,10 @@ export default class Cart { columnType: "timestamptz", defaultRaw: "now()", }) - updated_at?: Date + updated_at: Date @Property({ columnType: "timestamptz", nullable: true }) - deleted_at?: Date + deleted_at: Date | null = null @BeforeCreate() onCreate() { diff --git a/packages/cart/src/models/line-item-adjustment.ts b/packages/cart/src/models/line-item-adjustment.ts index 341b22b793..a1134a1393 100644 --- a/packages/cart/src/models/line-item-adjustment.ts +++ b/packages/cart/src/models/line-item-adjustment.ts @@ -1,6 +1,7 @@ import { generateEntityId } from "@medusajs/utils" import { BeforeCreate, + Cascade, Check, Entity, ManyToOne, @@ -15,12 +16,12 @@ import LineItem from "./line-item" expression: (columns) => `${columns.amount} >= 0`, }) export default class LineItemAdjustment extends AdjustmentLine { - @ManyToOne(() => LineItem, { - onDelete: "cascade", - nullable: true, + @ManyToOne({ + entity: () => LineItem, index: "IDX_adjustment_item_id", + cascade: [Cascade.REMOVE, Cascade.PERSIST], }) - item?: LineItem | null + item: LineItem @Property({ columnType: "text" }) item_id: string diff --git a/packages/cart/src/models/line-item-tax-line.ts b/packages/cart/src/models/line-item-tax-line.ts index 149d767fec..59a37dad20 100644 --- a/packages/cart/src/models/line-item-tax-line.ts +++ b/packages/cart/src/models/line-item-tax-line.ts @@ -1,6 +1,7 @@ import { generateEntityId } from "@medusajs/utils" import { BeforeCreate, + Cascade, Entity, ManyToOne, OnInit, @@ -11,12 +12,12 @@ import TaxLine from "./tax-line" @Entity({ tableName: "cart_line_item_tax_line" }) export default class LineItemTaxLine extends TaxLine { - @ManyToOne(() => LineItem, { - onDelete: "cascade", - nullable: true, + @ManyToOne({ + entity: () => LineItem, index: "IDX_tax_line_item_id", + cascade: [Cascade.REMOVE, Cascade.PERSIST] }) - item: LineItem | null + item: LineItem @Property({ columnType: "text" }) item_id: string diff --git a/packages/cart/src/models/line-item.ts b/packages/cart/src/models/line-item.ts index 19a152d070..5ab63a6565 100644 --- a/packages/cart/src/models/line-item.ts +++ b/packages/cart/src/models/line-item.ts @@ -35,21 +35,22 @@ export default class LineItem { @Property({ columnType: "text" }) cart_id: string - @ManyToOne(() => Cart, { + @ManyToOne({ + entity: () => Cart, onDelete: "cascade", index: "IDX_line_item_cart_id", - nullable: true, + cascade: [Cascade.REMOVE, Cascade.PERSIST], }) - cart?: Cart | null + cart: Cart @Property({ columnType: "text" }) title: string @Property({ columnType: "text", nullable: true }) - subtitle: string | null + subtitle: string | null = null @Property({ columnType: "text", nullable: true }) - thumbnail?: string | null + thumbnail: string | null = null @Property({ columnType: "integer" }) quantity: number @@ -59,44 +60,44 @@ export default class LineItem { nullable: true, index: "IDX_line_item_variant_id", }) - variant_id?: string | null + variant_id: string | null = null @Property({ columnType: "text", nullable: true, index: "IDX_line_item_product_id", }) - product_id?: string | null + product_id: string | null = null @Property({ columnType: "text", nullable: true }) - product_title?: string | null + product_title: string | null = null @Property({ columnType: "text", nullable: true }) - product_description?: string | null + product_description: string | null = null @Property({ columnType: "text", nullable: true }) - product_subtitle?: string | null + product_subtitle: string | null = null @Property({ columnType: "text", nullable: true }) - product_type?: string | null + product_type: string | null = null @Property({ columnType: "text", nullable: true }) - product_collection?: string | null + product_collection: string | null = null @Property({ columnType: "text", nullable: true }) - product_handle?: string | null + product_handle: string | null = null @Property({ columnType: "text", nullable: true }) - variant_sku?: string | null + variant_sku: string | null = null @Property({ columnType: "text", nullable: true }) - variant_barcode?: string | null + variant_barcode: string | null = null @Property({ columnType: "text", nullable: true }) - variant_title?: string | null + variant_title: string | null = null @Property({ columnType: "jsonb", nullable: true }) - variant_option_values?: Record | null + variant_option_values: Record | null = null @Property({ columnType: "boolean" }) requires_shipping = true @@ -108,8 +109,9 @@ export default class LineItem { is_tax_inclusive = false @Property({ columnType: "numeric", nullable: true }) - compare_at_unit_price?: number + compare_at_unit_price: number | null = null + // TODO: Rework when BigNumber has been introduced @Property({ columnType: "numeric", serializer: Number }) @Check({ expression: "unit_price >= 0" }) // TODO: Validate that numeric types work with the expression unit_price: number @@ -124,34 +126,12 @@ export default class LineItem { }) adjustments = new Collection(this) - /** COMPUTED PROPERTIES - START */ - - // compare_at_total?: number - // compare_at_subtotal?: number - // compare_at_tax_total?: number - - // original_total: number - // original_subtotal: number - // original_tax_total: number - - // item_total: number - // item_subtotal: number - // item_tax_total: number - - // total: number - // subtotal: number - // tax_total: number - // discount_total: number - // discount_tax_total: number - - /** COMPUTED PROPERTIES - END */ - @Property({ onCreate: () => new Date(), columnType: "timestamptz", defaultRaw: "now()", }) - created_at?: Date + created_at: Date @Property({ onCreate: () => new Date(), @@ -159,7 +139,7 @@ export default class LineItem { columnType: "timestamptz", defaultRaw: "now()", }) - updated_at?: Date + updated_at: Date @BeforeCreate() onCreate() { diff --git a/packages/cart/src/models/shipping-method-adjustment.ts b/packages/cart/src/models/shipping-method-adjustment.ts index 63b6473791..16a49fdb43 100644 --- a/packages/cart/src/models/shipping-method-adjustment.ts +++ b/packages/cart/src/models/shipping-method-adjustment.ts @@ -1,6 +1,7 @@ import { generateEntityId } from "@medusajs/utils" import { BeforeCreate, + Cascade, Entity, ManyToOne, OnInit, @@ -11,12 +12,12 @@ import ShippingMethod from "./shipping-method" @Entity({ tableName: "cart_shipping_method_adjustment" }) export default class ShippingMethodAdjustment extends AdjustmentLine { - @ManyToOne(() => ShippingMethod, { - onDelete: "cascade", - nullable: true, + @ManyToOne({ + entity: () => ShippingMethod, index: "IDX_adjustment_shipping_method_id", + cascade: [Cascade.REMOVE, Cascade.PERSIST], }) - shipping_method: ShippingMethod | null + shipping_method: ShippingMethod @Property({ columnType: "text" }) shipping_method_id: string diff --git a/packages/cart/src/models/shipping-method-tax-line.ts b/packages/cart/src/models/shipping-method-tax-line.ts index e5a022ab78..4673d19a43 100644 --- a/packages/cart/src/models/shipping-method-tax-line.ts +++ b/packages/cart/src/models/shipping-method-tax-line.ts @@ -1,6 +1,7 @@ import { generateEntityId } from "@medusajs/utils" import { BeforeCreate, + Cascade, Entity, ManyToOne, OnInit, @@ -11,12 +12,12 @@ import TaxLine from "./tax-line" @Entity({ tableName: "cart_shipping_method_tax_line" }) export default class ShippingMethodTaxLine extends TaxLine { - @ManyToOne(() => ShippingMethod, { - onDelete: "cascade", - nullable: true, + @ManyToOne({ + entity: () => ShippingMethod, index: "IDX_tax_line_shipping_method_id", + cascade: [Cascade.REMOVE, Cascade.PERSIST], }) - shipping_method: ShippingMethod | null + shipping_method: ShippingMethod @Property({ columnType: "text" }) shipping_method_id: string diff --git a/packages/cart/src/models/shipping-method.ts b/packages/cart/src/models/shipping-method.ts index ec41b17b91..c3a2ed448a 100644 --- a/packages/cart/src/models/shipping-method.ts +++ b/packages/cart/src/models/shipping-method.ts @@ -24,18 +24,18 @@ export default class ShippingMethod { @Property({ columnType: "text" }) cart_id: string - @ManyToOne(() => Cart, { - onDelete: "cascade", + @ManyToOne({ + entity: () => Cart, index: "IDX_shipping_method_cart_id", - nullable: true, + cascade: [Cascade.REMOVE, Cascade.PERSIST], }) - cart?: Cart | null + cart: Cart @Property({ columnType: "text" }) name: string @Property({ columnType: "jsonb", nullable: true }) - description?: string | null + description: string | null = null @Property({ columnType: "numeric", serializer: Number }) amount: number @@ -48,13 +48,13 @@ export default class ShippingMethod { nullable: true, index: "IDX_shipping_method_option_id", }) - shipping_option_id?: string | null + shipping_option_id: string | null = null @Property({ columnType: "jsonb", nullable: true }) - data?: Record | null + data: Record | null = null @Property({ columnType: "jsonb", nullable: true }) - metadata?: Record | null + metadata: Record | null = null @OneToMany( () => ShippingMethodTaxLine, @@ -74,20 +74,6 @@ export default class ShippingMethod { ) adjustments = new Collection(this) - /** COMPUTED PROPERTIES - START */ - - // original_total: number - // original_subtotal: number - // original_tax_total: number - - // total: number - // subtotal: number - // tax_total: number - // discount_total: number - // discount_tax_total: number - - /** COMPUTED PROPERTIES - END */ - @Property({ onCreate: () => new Date(), columnType: "timestamptz",