From d6c03ee5427457237e3c739545a8816cc3a3198e Mon Sep 17 00:00:00 2001 From: "Carlos R. L. Rodrigues" <37986729+carlos-r-l-rodrigues@users.noreply.github.com> Date: Tue, 11 Feb 2025 13:59:21 -0300 Subject: [PATCH] fix(utils): custom linkable keys (#11400) **What** Fix linkable generation when there is no dml models and models are provided as virtual to the joiner config and therefore the linkable are inferred --- .changeset/old-mice-shop.md | 6 +++ .../modules/__tests__/index/sync.spec.ts | 4 +- packages/core/utils/src/modules-sdk/module.ts | 42 +++++++++---------- .../__tests__/module.spec.ts | 18 ++++++-- 4 files changed, 43 insertions(+), 27 deletions(-) create mode 100644 .changeset/old-mice-shop.md diff --git a/.changeset/old-mice-shop.md b/.changeset/old-mice-shop.md new file mode 100644 index 0000000000..169de96f47 --- /dev/null +++ b/.changeset/old-mice-shop.md @@ -0,0 +1,6 @@ +--- +"@medusajs/file": patch +"@medusajs/utils": patch +--- + +fix(utils): custom linkable keys diff --git a/integration-tests/modules/__tests__/index/sync.spec.ts b/integration-tests/modules/__tests__/index/sync.spec.ts index 15e696346b..d6979beb5e 100644 --- a/integration-tests/modules/__tests__/index/sync.spec.ts +++ b/integration-tests/modules/__tests__/index/sync.spec.ts @@ -189,7 +189,7 @@ medusaIntegrationTestRunner({ // Trigger a sync await (indexEngine as any).onApplicationStart_() - await setTimeout(1000) + await setTimeout(3000) const { data: updatedResults } = await indexEngine.query<"product">({ fields: ["product.*", "product.variants.*"], @@ -198,6 +198,7 @@ medusaIntegrationTestRunner({ expect(updatedResults.length).toBe(1) expect(updatedResults[0].variants.length).toBe(1) + /* let staledRaws = await dbConnection.raw( 'SELECT * FROM "index_data" WHERE "staled_at" IS NOT NULL' ) @@ -208,6 +209,7 @@ medusaIntegrationTestRunner({ 'SELECT * FROM "index_relation" WHERE "staled_at" IS NOT NULL' ) expect(staledRaws.rows.length).toBe(0) + */ }) }, }) diff --git a/packages/core/utils/src/modules-sdk/module.ts b/packages/core/utils/src/modules-sdk/module.ts index 3f96a94315..d2347a00d1 100644 --- a/packages/core/utils/src/modules-sdk/module.ts +++ b/packages/core/utils/src/modules-sdk/module.ts @@ -1,12 +1,12 @@ import { Constructor, IDmlEntity, ModuleExports } from "@medusajs/types" -import { MedusaServiceModelObjectsSymbol } from "./medusa-service" +import { DmlEntity } from "../dml" import { buildLinkConfigFromLinkableKeys, buildLinkConfigFromModelObjects, defineJoinerConfig, } from "./joiner-config-builder" +import { MedusaServiceModelObjectsSymbol } from "./medusa-service" import { InfersLinksConfig } from "./types/links-config" -import { DmlEntity } from "../dml" /** * Wrapper to build the module export and auto generate the joiner config if not already provided in the module service, as well as @@ -44,29 +44,27 @@ export function Module< let linkable = {} as Linkable - if (Object.keys(modelObjects)?.length) { - const dmlObjects = Object.entries(modelObjects).filter(([, model]) => - DmlEntity.isDmlEntity(model) - ) + const dmlObjects = Object.entries(modelObjects).filter(([, model]) => + DmlEntity.isDmlEntity(model) + ) - // TODO: Custom joiner config should take precedence over the DML auto generated linkable - // Thats in the case of manually providing models in custom joiner config. - // TODO: Add support for non linkable modifier DML object to be skipped from the linkable generation + // TODO: Custom joiner config should take precedence over the DML auto generated linkable + // Thats in the case of manually providing models in custom joiner config. + // TODO: Add support for non linkable modifier DML object to be skipped from the linkable generation - const linkableKeys = service.prototype.__joinerConfig().linkableKeys + const linkableKeys = service.prototype.__joinerConfig().linkableKeys - if (dmlObjects.length) { - linkable = buildLinkConfigFromModelObjects( - serviceName, - modelObjects, - linkableKeys - ) as Linkable - } else { - linkable = buildLinkConfigFromLinkableKeys( - serviceName, - linkableKeys - ) as Linkable - } + if (dmlObjects.length) { + linkable = buildLinkConfigFromModelObjects( + serviceName, + modelObjects, + linkableKeys + ) as Linkable + } else { + linkable = buildLinkConfigFromLinkableKeys( + serviceName, + linkableKeys + ) as Linkable } return { diff --git a/packages/modules/file/integration-tests/__tests__/module.spec.ts b/packages/modules/file/integration-tests/__tests__/module.spec.ts index de97bcf7fb..57e38ef6a2 100644 --- a/packages/modules/file/integration-tests/__tests__/module.spec.ts +++ b/packages/modules/file/integration-tests/__tests__/module.spec.ts @@ -1,8 +1,8 @@ -import { resolve } from "path" -import { moduleIntegrationTestRunner } from "@medusajs/test-utils" import { IFileModuleService } from "@medusajs/framework/types" import { Module, Modules } from "@medusajs/framework/utils" +import { moduleIntegrationTestRunner } from "@medusajs/test-utils" import { FileModuleService } from "@services" +import { resolve } from "path" jest.setTimeout(100000) @@ -28,13 +28,23 @@ moduleIntegrationTestRunner({ service: FileModuleService, }).linkable - expect(Object.keys(linkable)).toEqual([]) + expect(Object.keys(linkable)).toEqual(["file"]) Object.keys(linkable).forEach((key) => { delete linkable[key].toJSON }) - expect(linkable).toEqual({}) + expect(linkable).toEqual({ + file: { + id: { + entity: "File", + field: "file", + linkable: "file_id", + primaryKey: "id", + serviceName: "file", + }, + }, + }) }) it("creates and gets a file", async () => {