Skip to content
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

Merged
merged 21 commits into from
Jan 5, 2024
Merged

feat(cart): Data models #5986

merged 21 commits into from
Jan 5, 2024

Conversation

olivermrbl
Copy link
Contributor

@olivermrbl olivermrbl commented Jan 2, 2024

What
Adds Cart Module data models

Copy link

changeset-bot bot commented Jan 2, 2024

⚠️ No Changeset found

Latest commit: cabcb6b

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link

vercel bot commented Jan 2, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

3 Ignored Deployments
Name Status Preview Comments Updated (UTC)
api-reference ⬜️ Ignored (Inspect) Visit Preview Jan 5, 2024 5:20pm
docs-ui ⬜️ Ignored (Inspect) Visit Preview Jan 5, 2024 5:20pm
medusa-docs ⬜️ Ignored (Inspect) Visit Preview Jan 5, 2024 5:20pm

@olivermrbl olivermrbl force-pushed the feat/cart-module-data-models branch 3 times, most recently from 9444d07 to 63bbf63 Compare January 4, 2024 15:56
@olivermrbl olivermrbl force-pushed the feat/cart-module-data-models branch from 63bbf63 to 0653093 Compare January 4, 2024 15:57
@olivermrbl olivermrbl changed the title [wip] feat(cart): Data models feat(cart): Data models Jan 4, 2024
@olivermrbl olivermrbl marked this pull request as ready for review January 4, 2024 16:01
@olivermrbl olivermrbl requested a review from a team as a code owner January 4, 2024 16:01
Copy link
Contributor

@pKorsholm pKorsholm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

packages/cart/src/models/address.ts Show resolved Hide resolved
packages/cart/src/models/adjustment-line.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@fPolic fPolic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Copy link
Member

@adrien2p adrien2p left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, few comments and questions


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

@Entity({ tableName: "cart_address" })
Copy link
Member

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?

Copy link
Contributor Author

@olivermrbl olivermrbl Jan 5, 2024

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, the address 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.

Copy link
Member

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

packages/cart/src/models/address.ts Show resolved Hide resolved
packages/cart/src/models/address.ts Show resolved Hide resolved
packages/cart/src/models/adjustment-line.ts Outdated Show resolved Hide resolved
code: string

@Property({ columnType: "numeric" })
amount: number
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: do we need the serializer/deserializer?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, nice catch.

Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 parseFloat use bignumber.js to return a BigNumber when reading the column?

Copy link
Contributor

Choose a reason for hiding this comment

The 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.
however that will impact the calculations. but we can do it in two steps.
first normalizing all the db + serialize but returning the BigNumber .toNumber(), and next step use the methods to perform the calculations where it is needed.

Copy link
Contributor

Choose a reason for hiding this comment

The 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 amount: string? or are we still serializing it back to a number.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the property still needs to be typed as a number, so we work with numbers on data mutations:

manager.create(LineItem, { amount: 10000000 })

But I am actually not sure. Would like others to pitch in too.

Copy link
Member

Choose a reason for hiding this comment

The 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?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in a first moment we keep it as number.
later we change it to a BigNumber type and refactor all the math operations to use the methods to calculate

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just to summarize the idea from carlos

  • step1: everything number with current ser/deser
  • step2: include bignumbers or similar as the ser/deser to number
  • step3: change number usage into all calculations

packages/cart/src/models/cart.ts Outdated Show resolved Hide resolved
packages/cart/src/models/cart.ts Outdated Show resolved Hide resolved
packages/cart/src/models/line-item.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@carlos-r-l-rodrigues carlos-r-l-rodrigues left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM
I just left a few ideas

code: string

@Property({ columnType: "numeric" })
amount: number
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

packages/cart/src/models/cart.ts Outdated Show resolved Hide resolved
Comment on lines 63 to 91
@Property({ columnType: "text", nullable: true })
product_title: string | null

@Property({ columnType: "text", nullable: true })
product_description: string | null

@Property({ columnType: "text", nullable: true })
product_subtitle: string | null

@Property({ columnType: "text", nullable: true })
product_type: string | null

@Property({ columnType: "text", nullable: true })
product_collection: string | null

@Property({ columnType: "text", nullable: true })
product_handle: string | null

@Property({ columnType: "text", nullable: true })
variant_sku: string | null

@Property({ columnType: "text", nullable: true })
variant_barcode: string | null

@Property({ columnType: "text", nullable: true })
variant_title: string | null

@Property({ columnType: "jsonb", nullable: true })
variant_option_values: Record<string, unknown> | null
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: I'm wondering if we could have another table to relate the property (id) and the value. That way we could have a more generic cart system not mapping product properties.

item_property
line_item
line_item_item_property

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having the variant and product properties specifically was meant to solve for two things:

  • Model properties that are easy to work with
  • Cart line items similar to how many other providers define them. E.g. Shopify and commercetools.

What other properties do you think could be relevant to have – potentially via the line item property approach?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wil merge this now, so I can move forward.

But I will not commit any migrations yet nor will I work with items yet, so we have time next week to revisit :)

Copy link
Contributor

@riqwan riqwan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

packages/cart/src/models/cart.ts Outdated Show resolved Hide resolved
packages/cart/src/models/index.ts Show resolved Hide resolved
@kodiakhq kodiakhq bot merged commit b57ad67 into develop Jan 5, 2024
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants