Skip to content

Commit

Permalink
feat(medusa): Use query relation load strategy on Carts (medusajs#3984)
Browse files Browse the repository at this point in the history
  • Loading branch information
olivermrbl authored May 2, 2023
1 parent 4e8045a commit b7a7826
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 23 deletions.
5 changes: 5 additions & 0 deletions .changeset/healthy-carpets-joke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@medusajs/medusa": patch
---

feat(medusa): Use query relation load strategy on Carts
2 changes: 1 addition & 1 deletion packages/medusa/src/loaders/database.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { AwilixContainer } from "awilix"
import {
DataSource,
DataSourceOptions,
Repository,
TreeRepository,
} from "typeorm"
import { AwilixContainer } from "awilix"
import { ConfigModule } from "../types/global"
import "../utils/naming-strategy"

Expand Down
14 changes: 3 additions & 11 deletions packages/medusa/src/models/cart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,6 @@
*/

import {
AfterLoad,
BeforeInsert,
Column,
Entity,
Expand All @@ -226,14 +225,16 @@ import {
ManyToMany,
ManyToOne,
OneToMany,
OneToOne,
OneToOne
} from "typeorm"
import { DbAwareColumn, resolveDbType } from "../utils/db-aware-column"
import {
FeatureFlagColumn,
FeatureFlagDecorators,
} from "../utils/feature-flag-decorators"

import { SoftDeletableEntity } from "../interfaces/models/soft-deletable-entity"
import { generateEntityId } from "../utils/generate-entity-id"
import { Address } from "./address"
import { Customer } from "./customer"
import { Discount } from "./discount"
Expand All @@ -244,8 +245,6 @@ import { PaymentSession } from "./payment-session"
import { Region } from "./region"
import { SalesChannel } from "./sales-channel"
import { ShippingMethod } from "./shipping-method"
import { SoftDeletableEntity } from "../interfaces/models/soft-deletable-entity"
import { generateEntityId } from "../utils/generate-entity-id"

export enum CartType {
DEFAULT = "default",
Expand Down Expand Up @@ -391,13 +390,6 @@ export class Cart extends SoftDeletableEntity {
gift_card_total?: number
gift_card_tax_total?: number

@AfterLoad()
private afterLoad(): void {
if (this.payment_sessions) {
this.payment_session = this.payment_sessions.find((p) => p.is_selected)!
}
}

@BeforeInsert()
private beforeInsert(): void {
this.id = generateEntityId(this.id, "cart")
Expand Down
10 changes: 5 additions & 5 deletions packages/medusa/src/models/shipping-method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ import {
PrimaryColumn,
} from "typeorm"

import TaxInclusivePricingFeatureFlag from "../loaders/feature-flags/tax-inclusive-pricing"
import { DbAwareColumn } from "../utils/db-aware-column"
import { FeatureFlagColumn } from "../utils/feature-flag-decorators"
import { generateEntityId } from "../utils/generate-entity-id"
import { Cart } from "./cart"
import { ClaimOrder } from "./claim-order"
import { DbAwareColumn } from "../utils/db-aware-column"
import { Order } from "./order"
import { Return } from "./return"
import { ShippingMethodTaxLine } from "./shipping-method-tax-line"
import { ShippingOption } from "./shipping-option"
import { Swap } from "./swap"
import { generateEntityId } from "../utils/generate-entity-id"
import { FeatureFlagColumn } from "../utils/feature-flag-decorators"
import TaxInclusivePricingFeatureFlag from "../loaders/feature-flags/tax-inclusive-pricing"

@Check(
`"claim_order_id" IS NOT NULL OR "order_id" IS NOT NULL OR "cart_id" IS NOT NULL OR "swap_id" IS NOT NULL OR "return_id" IS NOT NULL`
Expand Down Expand Up @@ -56,7 +56,7 @@ export class ShippingMethod {
@Column({ nullable: true })
cart_id: string

@ManyToOne(() => Cart)
@ManyToOne(() => Cart, (cart) => cart.shipping_methods)
@JoinColumn({ name: "cart_id" })
cart: Cart

Expand Down
4 changes: 2 additions & 2 deletions packages/medusa/src/models/tax-rate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import {

import { BaseEntity } from "../interfaces/models/base-entity"
import { DbAwareColumn } from "../utils/db-aware-column"
import { generateEntityId } from "../utils/generate-entity-id"
import { Product } from "./product"
import { ProductType } from "./product-type"
import { Region } from "./region"
import { ShippingOption } from "./shipping-option"
import { generateEntityId } from "../utils/generate-entity-id"

@Entity()
export class TaxRate extends BaseEntity {
Expand All @@ -30,7 +30,7 @@ export class TaxRate extends BaseEntity {
@Column()
region_id: string

@ManyToOne(() => Region)
@ManyToOne(() => Region, (region) => region.tax_rates)
@JoinColumn({ name: "region_id" })
region: Region

Expand Down
15 changes: 13 additions & 2 deletions packages/medusa/src/repositories/cart.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import { Cart } from "../models"
import { ExtendedFindConfig } from "@medusajs/types"
import { dataSource } from "../loaders/database"
import { Cart } from "../models"

export const CartRepository = dataSource.getRepository(Cart).extend({
async findOne(options: ExtendedFindConfig<Cart>) {
const [cart] = await this.find(options)

if (cart?.payment_sessions?.length) {
cart.payment_session = cart.payment_sessions.find((p) => p.is_selected)!
}

export const CartRepository = dataSource.getRepository(Cart)
return cart
},
})
export default CartRepository
4 changes: 2 additions & 2 deletions packages/medusa/src/repositories/order.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { objectToStringPath } from "@medusajs/utils"
import { flatten, groupBy, map, merge } from "lodash"
import { FindManyOptions, FindOptionsRelations, In } from "typeorm"
import { Order } from "../models"
import { objectToStringPath } from "@medusajs/utils"
import { dataSource } from "../loaders/database"
import { Order } from "../models"

const ITEMS_REL_NAME = "items"
const REGION_REL_NAME = "region"
Expand Down
1 change: 1 addition & 0 deletions packages/medusa/src/services/__tests__/cart.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ describe("CartService", () => {
it("calls cart model functions", () => {
expect(cartRepository.findOne).toHaveBeenCalledTimes(1)
expect(cartRepository.findOne).toHaveBeenCalledWith({
relationLoadStrategy: "query",
where: { id: IdMap.getId("emptyCart") },
select: undefined,
relations: undefined,
Expand Down
1 change: 1 addition & 0 deletions packages/medusa/src/services/cart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ class CartService extends TransactionBaseService {
const cartRepo = this.activeManager_.withRepository(this.cartRepository_)

const query = buildQuery({ id: cartId }, options)
query.relationLoadStrategy = "query"

if ((options.select || []).length === 0) {
query.select = undefined
Expand Down

0 comments on commit b7a7826

Please sign in to comment.