vholik / medusa-custom-attributes

Plugin that extends MedusaJs with custom attributes API
70 stars 17 forks source link

Enhancement Suggestion: Optimize Product Information Retrieval by Revising Data Relations #18

Closed KabyleBOT closed 4 months ago

KabyleBOT commented 5 months ago

Hello,

First, I'd like to express my appreciation for the plugin—it's been fantastic to work with. However, I'd like to discuss a potential enhancement concerning the data model, particularly regarding the retrieval of product information through custom attributes.

In the current implementation, the relation is defined as attribute_values.attribute. This setup requires additional computations on the frontend to access an attribute's specific values for a product. Such an approach, while functional, seems to introduce unnecessary complexity when fetching product details.

I propose a modification to the data model that could streamline data retrieval: introducing a direct relation from attribute to attribute_values within the product entity. This change would allow for more straightforward and efficient access to product information, as it aligns more naturally with how product attributes are typically displayed and utilized in e-commerce platforms.

For example, in our specific use case, we require custom attributes to enrich product information. Implementing a attribute.attribute_values relation would directly serve this need, reducing the frontend logic required to map attributes to their respective values.

I believe this adjustment could enhance the plugin's efficiency and usability, especially for use cases similar to ours that demand extensive use of custom product attributes. I'm eager to hear your thoughts on this suggestion and discuss potential impacts or implementation strategies.

Thank you for considering this enhancement.

vholik commented 4 months ago

Done in 1.0.15, you can upgrade your plugin. Now your store products responses should look like this.

{
  "custom_attributes": [
    {
      "name": "new attribute",
      "description": "",
      "type": "multi",
      "values": [
        {
          "value": "new value",
          "metadata": null,
          "rank": 0,
          "id": "attr_val_01HPFP2TYJ567EMPHJ9F4BETQM",
          "created_at": "2024-02-12T22:12:58.449Z",
          "updated_at": "2024-02-12T22:12:58.449Z"
        },
        {
          "value": "new value 2",
          "metadata": null,
          "rank": 1,
          "id": "attr_val_01HQ3NWW3PXAV86CZNXS6HHBYC",
          "created_at": "2024-02-20T16:34:31.670Z",
          "updated_at": "2024-02-20T16:34:31.670Z"
        }
      ],
      "handle": "new-attribute",
      "filterable": true,
      "metadata": null,
      "id": "attr_01HPFP2TYJBSNX4C0CVDE66KW8",
      "created_at": "2024-02-12T22:12:58.449Z",
      "updated_at": "2024-02-20T16:34:31.670Z"
    },
    {
      "name": "New int attribute",
      "description": "",
      "type": "range",
      "handle": "int-attribute",
      "filterable": true,
      "metadata": null,
      "id": "attr_01HQ3NADM2SMV1N2FVQXM72QE4",
      "created_at": "2024-02-20T16:24:27.009Z",
      "updated_at": "2024-02-20T16:24:27.009Z",
      "value": {
        "value": 234,
        "id": "int_attr_val_01HQ3NQ7D9NQHAV5636YBFDGYA",
        "created_at": "2024-02-20T15:31:26.230Z",
        "updated_at": "2024-02-20T15:31:26.230Z"
      }
    }
  ]
}
KabyleBOT commented 4 months ago

I am having this error when I try to select custom_attributes : {"level":"error","message":"Cannot read properties of null (reading 'id')","stack":[{"columnNumber":46,"fileName":"/app/node_modules/medusa-custom-attributes/dist/services/product.js","functionName":null,"lineNumber":20,"methodName":null,"native":false,"typeName":null},{"columnNumber":null,"fileName":null,"functionName":"Array.forEach","lineNumber":null,"methodName":"forEach","native":false,"typeName":"Array"},{"columnNumber":34,"fileName":"/app/node_modules/medusa-custom-attributes/dist/services/product.js","functionName":"ProductService.decorateProductWithAttributes","lineNumber":18,"methodName":"decorateProductWithAttributes","native":false,"typeName":"ProductService"},{"columnNumber":22,"fileName":"/app/node_modules/medusa-custom-attributes/dist/services/product.js","functionName":null,"lineNumber":105,"methodName":null,"native":false,"typeName":null},{"columnNumber":null,"fileName":null,"functionName":"Array.forEach","lineNumber":null,"methodName":"forEach","native":false,"typeName":"Array"},{"columnNumber":22,"fileName":"/app/node_modules/medusa-custom-attributes/dist/services/product.js","functionName":"ProductService.listAndCount","lineNumber":104,"methodName":"listAndCount","native":false,"typeName":"ProductService"},{"columnNumber":5,"fileName":"node:internal/process/task_queues","functionName":"process.processTicksAndRejections","lineNumber":95,"methodName":"processTicksAndRejections","native":false,"typeName":"process"},{"columnNumber":null,"fileName":null,"functionName":"async Promise.allSettled","lineNumber":null,"methodName":"allSettled","native":false,"typeName":"async Promise"}],"timestamp":"2024-02-26 11:15:56"}