Skip to content

Commit

Permalink
Upgrade mikro orm + make CI pass
Browse files Browse the repository at this point in the history
  • Loading branch information
olivermrbl committed Jan 9, 2024
1 parent d0965d5 commit d91ac3a
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 64 deletions.
6 changes: 3 additions & 3 deletions packages/cart/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@
"@medusajs/modules-sdk": "^1.12.5",
"@medusajs/types": "^1.11.9",
"@medusajs/utils": "^1.11.2",
"@mikro-orm/core": "5.9.7",
"@mikro-orm/migrations": "5.9.7",
"@mikro-orm/postgresql": "5.9.7",
"@mikro-orm/core": "^6.0.1",
"@mikro-orm/migrations": "^6.0.1",
"@mikro-orm/postgresql": "^6.0.1",
"awilix": "^8.0.0",
"dotenv": "^16.1.4",
"knex": "2.4.2"
Expand Down
14 changes: 4 additions & 10 deletions packages/cart/src/models/address.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
import { DAL } from "@medusajs/types"
import { generateEntityId } from "@medusajs/utils"
import {
BeforeCreate,
Entity,
OnInit,
OptionalProps,
Opt,
PrimaryKey,
Property
Property,
} from "@mikro-orm/core"


type OptionalAddressProps = DAL.EntityDateColumns // TODO: To be revisited when more clear

@Entity({ tableName: "cart_address" })
export default class Address {
[OptionalProps]: OptionalAddressProps

@PrimaryKey({ columnType: "text" })
id!: string

Expand Down Expand Up @@ -60,15 +54,15 @@ export default class Address {
columnType: "timestamptz",
defaultRaw: "now()",
})
created_at: Date
created_at: Opt<Date>

@Property({
onCreate: () => new Date(),
onUpdate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
})
updated_at: Date
updated_at: Opt<Date>

@BeforeCreate()
onCreate() {
Expand Down
11 changes: 3 additions & 8 deletions packages/cart/src/models/adjustment-line.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import { DAL } from "@medusajs/types"
import { OptionalProps, PrimaryKey, Property } from "@mikro-orm/core"

type OptionalAdjustmentLineProps = DAL.EntityDateColumns // TODO: To be revisited when more clear
import { Opt, PrimaryKey, Property } from "@mikro-orm/core"

/**
* As per the Mikro ORM docs, superclasses should use the abstract class definition
* Source: https://mikro-orm.io/docs/inheritance-mapping
*/
export default abstract class AdjustmentLine {
[OptionalProps]: OptionalAdjustmentLineProps

@PrimaryKey({ columnType: "text" })
id: string

Expand All @@ -33,13 +28,13 @@ export default abstract class AdjustmentLine {
columnType: "timestamptz",
defaultRaw: "now()",
})
created_at: Date
created_at: Opt<Date>

@Property({
onCreate: () => new Date(),
onUpdate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
})
updated_at: Date
updated_at: Opt<Date>
}
17 changes: 5 additions & 12 deletions packages/cart/src/models/cart.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { DAL } from "@medusajs/types"
import { generateEntityId } from "@medusajs/utils"
import {
BeforeCreate,
Expand All @@ -8,22 +7,16 @@ import {
OnInit,
OneToMany,
OneToOne,
Opt,
PrimaryKey,
Property,
} from "@mikro-orm/core"
import Address from "./address"
import LineItem from "./line-item"
import ShippingMethod from "./shipping-method"

type OptionalCartProps =
| "shipping_address"
| "billing_address"
| DAL.EntityDateColumns // TODO: To be revisited when more clear

@Entity({ tableName: "cart" })
export default class Cart {
[OptionalProps]?: OptionalCartProps

@PrimaryKey({ columnType: "text" })
id: string

Expand Down Expand Up @@ -52,15 +45,15 @@ export default class Cart {
cascade: [Cascade.REMOVE],
nullable: true,
})
shipping_address?: Address | null
shipping_address?: Opt<Address | null>

@OneToOne({
entity: () => Address,
joinColumn: "billing_address_id",
cascade: [Cascade.REMOVE],
nullable: true,
})
billing_address?: Address | null
billing_address?: Opt<Address | null>

@Property({ columnType: "jsonb", nullable: true })
metadata?: Record<string, unknown> | null
Expand Down Expand Up @@ -114,15 +107,15 @@ export default class Cart {
columnType: "timestamptz",
defaultRaw: "now()",
})
created_at: Date
created_at: Opt<Date>

@Property({
onCreate: () => new Date(),
onUpdate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
})
updated_at: Date
updated_at: Opt<Date>

@BeforeCreate()
onCreate() {
Expand Down
2 changes: 0 additions & 2 deletions packages/cart/src/models/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
export { default as Address } from "./address"
export { default as AdjustmentLine } from "./adjustment-line"
export { default as Cart } from "./cart"
export { default as LineItem } from "./line-item"
export { default as LineItemAdjustmentLine } from "./line-item-adjustment-line"
export { default as LineItemTaxLine } from "./line-item-tax-line"
export { default as ShippingMethod } from "./shipping-method"
export { default as ShippingMethodAdjustmentLine } from "./shipping-method-adjustment-line"
export { default as ShippingMethodTaxLine } from "./shipping-method-tax-line"
export { default as TaxLine } from "./tax-line"

31 changes: 10 additions & 21 deletions packages/cart/src/models/line-item.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,28 @@
import { DAL } from "@medusajs/types"
import { generateEntityId } from "@medusajs/utils"
import {
BeforeCreate,
Cascade,

Check,
Collection,
Entity,
ManyToOne,
OnInit,
OneToMany,
OptionalProps,
Opt,
PrimaryKey,

Property
Property,
} from "@mikro-orm/core"
import Cart from "./cart"
import LineItemAdjustmentLine from "./line-item-adjustment-line"
import LineItemTaxLine from "./line-item-tax-line"

type OptionalLineItemProps =
| "is_discoutable"
| "is_tax_inclusive"
| "compare_at_unit_price"
| "requires_shipping"
| DAL.EntityDateColumns

@Entity({ tableName: "cart_line_item" })
export default class LineItem {
[OptionalProps]?: OptionalLineItemProps

@PrimaryKey({ columnType: "text" })
id: string

@ManyToOne(() => Cart, {
onDelete: "cascade",
cascade: [Cascade.REMOVE],
index: "IDX_line_item_cart_id",
fieldName: "cart_id",
})
Expand Down Expand Up @@ -92,16 +81,16 @@ export default class LineItem {
variant_option_values?: Record<string, unknown> | null

@Property({ columnType: "boolean" })
requires_shipping = true
requires_shipping: Opt<boolean> = true

@Property({ columnType: "boolean" })
is_discountable = true
is_discountable: Opt<boolean> = true

@Property({ columnType: "boolean" })
is_tax_inclusive = false
is_tax_inclusive: Opt<boolean> = false

@Property({ columnType: "numeric", nullable: true })
compare_at_unit_price?: number
compare_at_unit_price?: Opt<number>

@Property({ columnType: "numeric", serializer: Number })
@Check({ expression: "unit_price >= 0" }) // TODO: Validate that numeric types work with the expression
Expand Down Expand Up @@ -148,15 +137,15 @@ export default class LineItem {
columnType: "timestamptz",
defaultRaw: "now()",
})
created_at: Date
created_at: Opt<Date>

@Property({
onCreate: () => new Date(),
onUpdate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
})
updated_at: Date
updated_at: Opt<Date>

@BeforeCreate()
onCreate() {
Expand Down
9 changes: 5 additions & 4 deletions packages/cart/src/models/shipping-method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
ManyToOne,
OnInit,
OneToMany,
Opt,
PrimaryKey,
Property,
} from "@mikro-orm/core"
Expand All @@ -21,7 +22,7 @@ export default class ShippingMethod {
id: string

@ManyToOne(() => Cart, {
onDelete: "cascade",
cascade: [Cascade.REMOVE],
index: "IDX_shipping_method_cart_id",
fieldName: "cart_id",
})
Expand All @@ -38,7 +39,7 @@ export default class ShippingMethod {
amount: number

@Property({ columnType: "boolean" })
is_tax_inclusive = false
is_tax_inclusive: Opt<boolean> = false

@Property({ columnType: "text", nullable: true })
shipping_option_id?: string | null
Expand Down Expand Up @@ -86,15 +87,15 @@ export default class ShippingMethod {
columnType: "timestamptz",
defaultRaw: "now()",
})
created_at: Date
created_at: Opt<Date>

@Property({
onCreate: () => new Date(),
onUpdate: () => new Date(),
columnType: "timestamptz",
defaultRaw: "now()",
})
updated_at: Date
updated_at: Opt<Date>

@BeforeCreate()
onCreate() {
Expand Down
Loading

0 comments on commit d91ac3a

Please sign in to comment.