medusajs / medusa

The world's most flexible commerce platform.
https://medusajs.com
MIT License
25.58k stars 2.55k forks source link

[V1.X] StockLocationService cannot be "properly" extended #7789

Closed adevinwild closed 1 month ago

adevinwild commented 4 months ago

Bug report (it's not a bug technically)

Describe the bug

Hi everyone, The way the repository is resolved in the StockLocation plugin doesn't allow for optimal model extension.

Indeed, since the repository is resolved directly via the original model, it's impossible in a project to override properly the StockLocation entity without having any errors.

Let's take an example, I override the StockLocation model in my project as follows:

// src/models/stock-location.ts

// ... imports

import {
    StockLocation as MedusaStockLocation,
} from "@medusajs/stock-location/dist/models";
import { Column, Entity } from "typeorm";

@Entity()
export class StockLocation extends MedusaStockLocation {
    @Column()
    custom_field: string;
}

After the migration I'll extend the service to force this new field to be selected when retrieving stock locations :

// src/services/stock-location.ts

// ... imports

class StockLocationService extends MedusaStockLocationService {
    // ... rest

    async list(
        selector: FilterableStockLocationProps = {},
        config: FindConfig<StockLocation> = { relations: [], skip: 0, take: 10 },
        @MedusaContext() context: SharedContext = {}
    ): Promise<StockLocation[]> {

        config.select?.push('custom_field')

        const [locations] = await super.listAndCount(selector, config, context)
        return locations
    }

}

Whenever this function is called, it will always throw an error on the terminal like this :

Property "custom-field" was not found in "StockLocation". Make sure your query is correct.
EntityPropertyNotFoundError: Property "custom_field" was not found in "StockLocation". Make sure your query is correct.

And it makes sense, because of the way the StockLocationRepository is resolved on the stock-location module. You can check this "here"

We can definitely improve this to make it more customizable, maybe by changing the way the StockLocation repo and model are resolved

adevinwild commented 4 months ago

Oh and btw, I will suggest you a PR when I have freetime

BorisKamp commented 3 months ago

+1, I have this problem as well.

I extended the customerGroup model with a single_customer_id field

import { Column, Entity } from 'typeorm'

import { CustomerGroup as MedusaCustomerGroup } from '@medusajs/medusa'

@Entity()
export class CustomerGroup extends MedusaCustomerGroup {
  @Column()
  single_customer_id: string
}

And added it to src/index.d.ts:

...
export declare module '@medusajs/medusa/dist/models/customer-groups' {
  declare interface CustomerGroup {
    single_customer_id: string
  }
}
...

I added the validator as well, it all works perfectly when used with the native endpoints, I can use the single_customer_id params for example. However, when I want to use a repo like this:

      const groups = await customerGroupRepo.find({
        where: { single_customer_id: req.params.id },
      })

I get an error: Object literal may only specify known properties, and 'single_customer_id' does not exist in type 'FindOptionsWhere<CustomerGroup> | FindOptionsWhere<CustomerGroup>[]'.ts(2353)

How can we fix this?

Thanks!

github-actions[bot] commented 1 month ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 3 days.

github-actions[bot] commented 1 month ago

This issue was closed because it has been stalled for 3 days with no activity.