Set Custom Filter in Product Module #10614
Replies: 5 comments 9 replies
-
@shahednasser I have seen this question raised at Discord as well and it's not clear from documentation how to do this. The existing documentation only mentions how to pass additional linked data to product I have spent some time diving into medusa core and could not find answer either. Thanks! |
Beta Was this translation helpful? Give feedback.
-
Brand and product are different modules, the link concept establishes the relation. now if we want to achieve we have to send two requests to the database first fetch the brand and then fetch the product of that brand, but if I like it in a single query request then it is not possible. following is the code: export const GET = async ( it shows an error of Entity 'Product' does not have property 'brand'. |
Beta Was this translation helpful? Give feedback.
-
In Medusa, the order and product modules are typically connected via a linked table without a database-level foreign key constraint. This approach has two major downsides: No Data Integrity at the Database Level – Since the relationship isn't enforced at the database level, data consistency is not guaranteed, which can lead to orphaned records. Inefficient Querying—When filtering data that includes fields from both order and product tables, two separate queries must be made—one for the order table and another for the product table—resulting in additional database requests and slower performance. Proposed Solution: Proposed Change: (Direct Foreign Key in Order Table) ✅ Ensures Data Integrity – Prevents orphan records by enforcing the relationship at the database level. Looking for Community Feedback
Would appreciate any insights! 🚀 |
Beta Was this translation helpful? Give feedback.
-
I have a similar problem that is missing information in the documentation. The section about querying Linked Records only mentions that "You can then apply any filters or pagination configurations." but doesn't demonstrate how to do this exactly. In my case I'm querying a Linked Record of Product and custom entity and I need to apply additional filtering on Product. I've tried many approaches but nothing seems to work. The full query I'm trying to run looks as follows:
The error I'm getting:
And yet the data returned has a structure of:
|
Beta Was this translation helpful? Give feedback.
-
Hi everyone, import { model } from "@medusajs/framework/utils"
export enum AvailabilityStatus {
INCOMING = "incoming",
IN_STOCK = "in_stock",
OUT_OF_STOCK = "out_of_stock",
}
const Availability = model.define("availability", {
id: model.id().primaryKey(),
product_id: model.text().unique(), // Link to product
status: model.enum(AvailabilityStatus).default(AvailabilityStatus.INCOMING),
notes: model.text().nullable(),
})
export default Availability And the following link definition: import { defineLink } from "@medusajs/framework/utils"
import AvailabilityModule from "../modules/availability"
import ProductModule from "@medusajs/medusa/product"
export default defineLink(ProductModule.linkable.product, {
linkable: AvailabilityModule.linkable.availability,
deleteCascade: true,
}) I want to create a /store/products/available route that returns only products that are either "in_stock" or "incoming". The following query doesn't return the products as expected: const query = req.scope.resolve(ContainerRegistrationKeys.QUERY)
const { data: availabilities } = await query.graph({
entity: "availability",
fields: ["*", "product.*"],
filters: {
status: {
$in: [AvailabilityStatus.IN_STOCK, AvailabilityStatus.INCOMING],
},
},
}) And as mentioned in this thread, I can't directly filter products by their availability status. How would you implement this without having to query the database twice? I was previously doing two separate queries, but this creates noticeable latency. Thanks! |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Thank you for always providing excellent libraries and documentation.
Following the documentation, we linked brands to products separately from categories, and all features are working flawlessly on the admin page.
However, we are encountering an issue when attempting to apply a brand filter on a product list component, such as paginated-products.tsx, on the user page.
On the frontend, we have confirmed that the SDK module is generally used to retrieve products. We are wondering how to apply the brand filter in this context.
When we try to incorporate the brand filter into the SDK, a Zod error is returned like this screenshot.
Is there a way to directly apply the brand filter using the SDK?
Or would it be necessary to create a new route for this purpose?
We would be sincerely grateful for your assistance.
Beta Was this translation helpful? Give feedback.
All reactions