fix: product module joiner config (#8170)
* fix: product module joiner config * fix aliases * revert package.json * add js doc
This commit is contained in:
1 parent
c58a35f0c0
commit
398e39d163
2 files changed
+97
-3
No files matched your search
@@ -3,6 +3,7 @@ import { medusaIntegrationTestRunner } from "medusa-test-utils"
|
|||||||
import CurrencyModule from "@medusajs/currency"
|
import CurrencyModule from "@medusajs/currency"
|
||||||
import { MedusaModule } from "@medusajs/modules-sdk"
|
import { MedusaModule } from "@medusajs/modules-sdk"
|
||||||
import RegionModule from "@medusajs/region"
|
import RegionModule from "@medusajs/region"
|
||||||
|
import ProductModule from "@medusajs/product"
|
||||||
import { defineLink } from "@medusajs/utils"
|
import { defineLink } from "@medusajs/utils"
|
||||||
|
|
||||||
jest.setTimeout(50000)
|
jest.setTimeout(50000)
|
||||||
@@ -92,6 +93,91 @@ medusaIntegrationTestRunner({
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it("should generate a proper link definition with multi parts entity name", async () => {
|
||||||
|
const productVariantLinks = ProductModule.linkable
|
||||||
|
const regionLinks = RegionModule.linkable
|
||||||
|
|
||||||
|
const link = defineLink(
|
||||||
|
productVariantLinks.productVariant,
|
||||||
|
regionLinks.region
|
||||||
|
)
|
||||||
|
|
||||||
|
const linkDefinition = MedusaModule.getCustomLinks()
|
||||||
|
.map((linkDefinition: any) => {
|
||||||
|
const definition = linkDefinition(
|
||||||
|
MedusaModule.getAllJoinerConfigs()
|
||||||
|
)
|
||||||
|
return definition.serviceName === link.serviceName && definition
|
||||||
|
})
|
||||||
|
.filter(Boolean)[0]
|
||||||
|
|
||||||
|
expect(link.serviceName).toEqual(linkDefinition.serviceName)
|
||||||
|
expect(link.entryPoint).toEqual(linkDefinition.alias[0].name[0])
|
||||||
|
expect(linkDefinition).toEqual({
|
||||||
|
serviceName: "productProductVariantRegionRegionLink",
|
||||||
|
isLink: true,
|
||||||
|
alias: [
|
||||||
|
{
|
||||||
|
name: ["product_variant_region"],
|
||||||
|
args: {
|
||||||
|
entity: "LinkProductServiceProductVariantRegionRegion",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
primaryKeys: ["id", "product_variant_id", "region_id"],
|
||||||
|
relationships: [
|
||||||
|
{
|
||||||
|
serviceName: "productService",
|
||||||
|
primaryKey: "id",
|
||||||
|
foreignKey: "product_variant_id",
|
||||||
|
alias: "product_variant",
|
||||||
|
args: {
|
||||||
|
methodSuffix: "ProductVariants",
|
||||||
|
},
|
||||||
|
deleteCascade: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
serviceName: "region",
|
||||||
|
primaryKey: "id",
|
||||||
|
foreignKey: "region_id",
|
||||||
|
alias: "region",
|
||||||
|
args: {
|
||||||
|
methodSuffix: "Regions",
|
||||||
|
},
|
||||||
|
deleteCascade: false,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
extends: [
|
||||||
|
{
|
||||||
|
serviceName: "productService",
|
||||||
|
fieldAlias: {
|
||||||
|
region: "region_link.region",
|
||||||
|
},
|
||||||
|
relationship: {
|
||||||
|
serviceName: "productProductVariantRegionRegionLink",
|
||||||
|
primaryKey: "product_variant_id",
|
||||||
|
foreignKey: "id",
|
||||||
|
alias: "region_link",
|
||||||
|
isList: false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
serviceName: "region",
|
||||||
|
fieldAlias: {
|
||||||
|
product_variant: "product_variant_link.product_variant",
|
||||||
|
},
|
||||||
|
relationship: {
|
||||||
|
serviceName: "productProductVariantRegionRegionLink",
|
||||||
|
primaryKey: "region_id",
|
||||||
|
foreignKey: "id",
|
||||||
|
alias: "product_variant_link",
|
||||||
|
isList: false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
it("should flag deleteCascade in the link definition", async () => {
|
it("should flag deleteCascade in the link definition", async () => {
|
||||||
const currencyLinks = CurrencyModule.linkable
|
const currencyLinks = CurrencyModule.linkable
|
||||||
const regionLinks = RegionModule.linkable
|
const regionLinks = RegionModule.linkable
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { LinkModulesExtraFields, ModuleJoinerConfig } from "@medusajs/types"
|
import { LinkModulesExtraFields, ModuleJoinerConfig } from "@medusajs/types"
|
||||||
import { isObject, pluralize, toPascalCase } from "../common"
|
import { camelToSnakeCase, isObject, pluralize, toPascalCase } from "../common"
|
||||||
import { composeLinkName } from "../link"
|
import { composeLinkName } from "../link"
|
||||||
|
|
||||||
export const DefineLinkSymbol = Symbol.for("DefineLink")
|
export const DefineLinkSymbol = Symbol.for("DefineLink")
|
||||||
@@ -74,7 +74,7 @@ function prepareServiceConfig(input: DefineLinkInputSource) {
|
|||||||
|
|
||||||
serviceConfig = {
|
serviceConfig = {
|
||||||
key: source.linkable,
|
key: source.linkable,
|
||||||
alias: source.field,
|
alias: camelToSnakeCase(source.field),
|
||||||
primaryKey: source.primaryKey,
|
primaryKey: source.primaryKey,
|
||||||
isList: false,
|
isList: false,
|
||||||
deleteCascade: false,
|
deleteCascade: false,
|
||||||
@@ -87,7 +87,7 @@ function prepareServiceConfig(input: DefineLinkInputSource) {
|
|||||||
|
|
||||||
serviceConfig = {
|
serviceConfig = {
|
||||||
key: source.linkable,
|
key: source.linkable,
|
||||||
alias: source.field,
|
alias: camelToSnakeCase(source.field),
|
||||||
primaryKey: source.primaryKey,
|
primaryKey: source.primaryKey,
|
||||||
isList: input.isList ?? false,
|
isList: input.isList ?? false,
|
||||||
deleteCascade: input.deleteCascade ?? false,
|
deleteCascade: input.deleteCascade ?? false,
|
||||||
@@ -106,6 +106,14 @@ function prepareServiceConfig(input: DefineLinkInputSource) {
|
|||||||
return serviceConfig
|
return serviceConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate a ModuleJoinerConfig for the link definition on the fly.
|
||||||
|
* All naming, aliases etc are following our conventional naming.
|
||||||
|
*
|
||||||
|
* @param leftService
|
||||||
|
* @param rightService
|
||||||
|
* @param linkServiceOptions
|
||||||
|
*/
|
||||||
export function defineLink(
|
export function defineLink(
|
||||||
leftService: DefineLinkInputSource,
|
leftService: DefineLinkInputSource,
|
||||||
rightService: DefineLinkInputSource,
|
rightService: DefineLinkInputSource,
|
||||||
|
|||||||
Reference in new issue
Block a user