-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(cart): Data models #5986
feat(cart): Data models #5986
Changes from 16 commits
ef0dd4b
fd933b8
daaa75a
12cb585
07289e9
2994f1d
2e7092c
cc8f845
e2074fb
068febf
923cceb
cbc8383
8f074a1
b658e46
ee02ba2
0653093
9308ea5
2050fab
251f54d
a5e5697
cabcb6b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { DAL } from "@medusajs/types" | ||
import { generateEntityId } from "@medusajs/utils" | ||
import { | ||
BeforeCreate, | ||
Entity, | ||
OnInit, | ||
OptionalProps, | ||
PrimaryKey, | ||
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 | ||
|
||
@Property({ columnType: "text", nullable: true }) | ||
customer_id?: string | null | ||
olivermrbl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
@Property({ columnType: "text", nullable: true }) | ||
company?: string | null | ||
|
||
@Property({ columnType: "text", nullable: true }) | ||
first_name?: string | null | ||
|
||
@Property({ columnType: "text", nullable: true }) | ||
last_name?: string | null | ||
|
||
@Property({ columnType: "text", nullable: true }) | ||
address_1?: string | null | ||
|
||
@Property({ columnType: "text", nullable: true }) | ||
address_2?: string | null | ||
olivermrbl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
@Property({ columnType: "text", nullable: true }) | ||
city?: string | null | ||
|
||
@Property({ columnType: "text", nullable: true }) | ||
country_code?: string | null | ||
|
||
@Property({ columnType: "text", nullable: true }) | ||
province?: string | null | ||
|
||
@Property({ columnType: "text", nullable: true }) | ||
postal_code?: string | null | ||
|
||
@Property({ columnType: "text", nullable: true }) | ||
phone?: string | null | ||
|
||
@Property({ columnType: "jsonb", nullable: true }) | ||
metadata?: Record<string, unknown> | null | ||
|
||
@Property({ | ||
onCreate: () => new Date(), | ||
columnType: "timestamptz", | ||
defaultRaw: "now()", | ||
}) | ||
created_at: Date | ||
|
||
@Property({ | ||
onCreate: () => new Date(), | ||
onUpdate: () => new Date(), | ||
columnType: "timestamptz", | ||
defaultRaw: "now()", | ||
}) | ||
updated_at: Date | ||
|
||
@BeforeCreate() | ||
onCreate() { | ||
this.id = generateEntityId(this.id, "caaddr") | ||
olivermrbl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
@OnInit() | ||
onInit() { | ||
this.id = generateEntityId(this.id, "caaddr") | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { DAL } from "@medusajs/types" | ||
import { OptionalProps, PrimaryKey, Property } from "@mikro-orm/core" | ||
|
||
type OptionalAdjustmentLineProps = DAL.EntityDateColumns // TODO: To be revisited when more clear | ||
|
||
export default class AdjustmentLine { | ||
olivermrbl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
[OptionalProps]: OptionalAdjustmentLineProps | ||
|
||
@PrimaryKey({ columnType: "text" }) | ||
id: string | ||
|
||
@Property({ columnType: "text", nullable: true }) | ||
description?: string | null | ||
|
||
@Property({ columnType: "text", nullable: true }) | ||
promotion_id?: string | null | ||
|
||
@Property({ columnType: "text" }) | ||
code: string | ||
|
||
@Property({ columnType: "numeric" }) | ||
amount: number | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. question: do we need the serializer/deserializer? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, nice catch. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need a util to be used for money amounts everywhere to normalize this. Probably a good time to sync and update pricing, promotion and cart. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, would make sense. Were you thinking something along the lines of what this guy proposes, but instead of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes. we could install bignumber.js on our utils and use it from there. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, that sounds good. Since bignumberjs also works with strings, this would amount to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the property still needs to be typed as a
But I am actually not sure. Would like others to pitch in too. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes the property can be kept as number, it doesn't prevent us from using float numbers for example. But it would be more about the serizliation and deserialization related no? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in a first moment we keep it as number. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just to summarize the idea from carlos
|
||
|
||
@Property({ columnType: "text", nullable: true }) | ||
provider_id?: string | null | ||
|
||
@Property({ | ||
onCreate: () => new Date(), | ||
columnType: "timestamptz", | ||
defaultRaw: "now()", | ||
}) | ||
created_at: Date | ||
|
||
@Property({ | ||
onCreate: () => new Date(), | ||
onUpdate: () => new Date(), | ||
columnType: "timestamptz", | ||
defaultRaw: "now()", | ||
}) | ||
updated_at: Date | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,11 @@ | ||
export { default as Cart } from "./cart"; | ||
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" | ||
olivermrbl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { generateEntityId } from "@medusajs/utils" | ||
import { | ||
BeforeCreate, | ||
Entity, | ||
ManyToOne, | ||
OnInit | ||
} from "@mikro-orm/core" | ||
import AdjustmentLine from "./adjustment-line" | ||
import LineItem from "./line-item" | ||
|
||
@Entity({ tableName: "cart_line_item_adjustment_line" }) | ||
export default class LineItemAdjustmentLine extends AdjustmentLine { | ||
olivermrbl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@ManyToOne(() => LineItem, { | ||
joinColumn: "line_item", | ||
fieldName: "line_item_id", | ||
}) | ||
line_item: LineItem | ||
|
||
@BeforeCreate() | ||
onCreate() { | ||
this.id = generateEntityId(this.id, "caliadj") | ||
} | ||
|
||
@OnInit() | ||
onInit() { | ||
this.id = generateEntityId(this.id, "caliadj") | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { generateEntityId } from "@medusajs/utils" | ||
import { BeforeCreate, Entity, ManyToOne, OnInit } from "@mikro-orm/core" | ||
import LineItem from "./line-item" | ||
import TaxLine from "./tax-line" | ||
|
||
@Entity({ tableName: "cart_line_item_tax_line" }) | ||
export default class LineItemTaxLine extends TaxLine { | ||
@ManyToOne(() => LineItem, { | ||
joinColumn: "line_item", | ||
fieldName: "line_item_id", | ||
}) | ||
line_item: LineItem | ||
|
||
@BeforeCreate() | ||
onCreate() { | ||
this.id = generateEntityId(this.id, "calitxl") | ||
} | ||
|
||
@OnInit() | ||
onInit() { | ||
this.id = generateEntityId(this.id, "calitxl") | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question: any reason we store that in cart_address and not address?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to couple table names to the module for when we start combining them.
E.g.
@medusajs/cart
and@medusajs/customer
will both ship an address table. To avoid clashes/conflicts with tables across the modules, we need to differentiate them. Otherwise, theaddress
table would be shared, which can lead to unexpected issues.It will be the consumer's responsibility to ensure that addresses are managed correctly for these cases where the model lives in both modules. Here, with the Customer and Cart module, you would likely use the customer's address from the Customer Module to create a cart address in the Cart Module
This will introduce some data redundancy, but it is for the better. It will make the modules much more usable in a standalone context.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes you are entirely right, I ve indentified that with the rest of the pr :D my bad