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

Extended repository example in document is not working #6139

Closed
2 tasks done
vinhnguyen1211 opened this issue Jan 19, 2024 · 14 comments
Closed
2 tasks done

Extended repository example in document is not working #6139

vinhnguyen1211 opened this issue Jan 19, 2024 · 14 comments

Comments

@vinhnguyen1211
Copy link
Contributor

vinhnguyen1211 commented Jan 19, 2024

Preliminary Checks

Issue Summary

I'm following this guide https://docs.medusajs.com/development/entities/extend-repository#step-2-implement-extended-repository
When I call http://localhost:9000/admin/products it keeps saying productRepo.findWithRelationsAndCount is not a function
image

How can this issue be resolved?

I was able to fix the issue by proper deep copy

export const OrderRepository = dataSource.getRepository(Order)
	.extend(Object.assign(MedusaOrderRepository, { target: Order }));

Are you interested in working on this issue?

  • I would like to fix this issue
@shahednasser
Copy link
Member

@adrien2p can I get you to confirm if this solution is correct?

@adrien2p
Copy link
Member

@shahednasser @vinhnguyen1211 yes it seems that the documentation is missing the target replacement. it should be something like

export const ProductRepository = dataSource
  .getRepository(Product)
  .extend({
    // it is important to spread the existing repository here.
    //  Otherwise you will end up losing core properties
    ...Object.assign(MedusaProductRepository, { target: Product })

    /**
     * Here you can create your custom function
     * For example
     */
    customFunction(): void {
      // TODO add custom implementation
      return
    },
  })

@vinhnguyen1211
Copy link
Contributor Author

@adrien2p @shahednasser spread operator doesn't work. I just test with your code snippet, it should be like this

export const OrderRepository = dataSource.getRepository(Order).extend(
  Object.assign(MedusaOrderRepository, {
    customFunction(): void {
      // TODO add custom implementation
      return
    }
  }),
);

seems { target: Product } is redundant

@adrien2p
Copy link
Member

adrien2p commented Jan 22, 2024

Spread operator works, I ve tested it myself, also, target is important if you are using a custom entity to replace the core one, otherwise if the core one is registered it will use the core entity. now if you don't have a custom entity it is not necessary.

Screenshot 2024-01-22 at 10 17 12

Then when I put a breakpoint I can see that all functions are available and accessible. for the test, I just wanted to see if the existing function was called, didn't matched the API just for the test purpose 😄

Screenshot 2024-01-22 at 10 19 46

@vinhnguyen1211
Copy link
Contributor Author

vinhnguyen1211 commented Jan 22, 2024

Have you tried fetching orders at http://localhost:9000/admin/orders? When I use spread operator I got that error again, I also extend Order service

class OrderService extends MedusaOrderService {
  static LIFE_TIME = Lifetime.SCOPED;
  protected readonly loggedInUser_: User | null;

  constructor(container, options) {
    // @ts-expect-error prefer-rest-params
    super(...arguments);

    try {
      this.loggedInUser_ = container.loggedInUser;
    } catch (e) {
      // avoid errors when backend first runs
    }
  }

  async listAndCount(
    selector: QuerySelector<Order> & { store_id?: string },
    config?: FindConfig<Order>,
  ): Promise<[Order[], number]> {
    if (!selector.store_id && this.loggedInUser_?.store_id) {
      selector.store_id = this.loggedInUser_.store_id;
    }

    if (config?.select?.length) {
      config.select.push('store_id');
    }
    if (config?.relations?.length) {
      config.relations.push('store');
    }

    return super.listAndCount(selector, config);
  }
}

@adrien2p
Copy link
Member

Yes, here it is
Screenshot 2024-01-22 at 10 42 46
Screenshot 2024-01-22 at 10 42 54

@vinhnguyen1211
Copy link
Contributor Author

would you mind also extending OrderService, the important is return super.listAndCount(selector, config); will throw error

@adrien2p
Copy link
Member

sure, give me a sec

@adrien2p
Copy link
Member

I don't have the store id so I have to comment it but it works
Screenshot 2024-01-22 at 11 05 22
Screenshot 2024-01-22 at 11 05 37

@vinhnguyen1211
Copy link
Contributor Author

Thanks. So I got there're weird issues from my end.
I'll update PR following your suggestion 🙌

kodiakhq bot pushed a commit that referenced this issue Jan 22, 2024
- Issue ticket #6139
- Update document how to extend repository
@corentinbranchereau
Copy link

I recently updated from medusajs/medusa 1.17.2 to medusajs/medusa 1.20.0, and i have the same problem in 1.20.0 where spread operator does not work (it was working in 1.17.2). The suggestion @shahednasser provided seems to fix the problem

@adrien2p @shahednasser spread operator doesn't work. I just test with your code snippet, it should be like this

export const OrderRepository = dataSource.getRepository(Order).extend(
  Object.assign(MedusaOrderRepository, {
    customFunction(): void {
      // TODO add custom implementation
      return
    }
  }),
);

seems { target: Product } is redundant

@kim-hanho
Copy link

Same here as @corentinbranchereau . I updated from old version to 1.20.0, and re-installed node modules.
The only working solution is not using spread operator.

@raukaute
Copy link

raukaute commented Feb 3, 2024

Facing the same issue. Spread operator breaks the functionality. Unfortunately I depend on it in one case, so it broke admin categories completely.

@ashishbansalmbm
Copy link

Facing the same issue. Spread operator is not working only in the case of Extending Product Entity as in the example.
But working for User and Store Entity.

error: productRepo.findWithRelationsAndCount is not a function TypeError: productRepo.findWithRelationsAndCount is not a function at ProductService.<anonymous> (C:\Users\ashis\Desktop\Panda\backend\node_modules\@medusajs\medusa\dist\services\product.js:211:58) at step (C:\Users\ashis\Desktop\Panda\backend\node_modules\@medusajs\medusa\dist\services\product.js:59:23) at Object.next (C:\Users\ashis\Desktop\Panda\backend\node_modules\@medusajs\medusa\dist\services\product.js:40:53) at C:\Users\ashis\Desktop\Panda\backend\node_modules\@medusajs\medusa\dist\services\product.js:34:71 at new Promise (<anonymous>) at __awaiter (C:\Users\ashis\Desktop\Panda\backend\node_modules\@medusajs\medusa\dist\services\product.js:30:12) at ProductService.listAndCount (C:\Users\ashis\Desktop\Panda\backend\node_modules\@medusajs\medusa\dist\services\product.js:189:16) at ProductService._callee2$ (C:\Users\ashis\Desktop\Panda\backend\dist\services\product.js:78:126) at tryCatch (C:\Users\ashis\Desktop\Panda\backend\node_modules\@babel\runtime\helpers\regeneratorRuntime.js:45:16) at Generator.<anonymous> (C:\Users\ashis\Desktop\Panda\backend\node_modules\@babel\runtime\helpers\regeneratorRuntime.js:133:17) ::1 - - [19/Mar/2024:20:52:08 +0000] "GET /admin/products/ HTTP/1.1" 500 86 "-" "PostmanRuntime/7.37.0"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants