chore: rename source to linkable in defineLink options (#8016)

* chore: rename source to linkable in defineLink options

* rm unused const
This commit is contained in:
Adrien de Peretti
2024-07-08 16:44:03 +02:00
committed by GitHub
parent fc7f5ff71a
commit 10848c8bf2
2 changed files with 90 additions and 16 deletions
@@ -9,14 +9,6 @@ jest.setTimeout(50000)
medusaIntegrationTestRunner({ medusaIntegrationTestRunner({
testSuite: ({ getContainer }) => { testSuite: ({ getContainer }) => {
describe("defineLink", () => { describe("defineLink", () => {
let appContainer
let remoteQuery
beforeAll(async () => {
appContainer = getContainer()
remoteQuery = appContainer.resolve("remoteQuery")
})
it("should generate a proper link definition", async () => { it("should generate a proper link definition", async () => {
const currencyLinks = CurrencyModule.linkable const currencyLinks = CurrencyModule.linkable
const regionLinks = RegionModule.linkable const regionLinks = RegionModule.linkable
@@ -95,6 +87,88 @@ medusaIntegrationTestRunner({
], ],
}) })
}) })
it("should generate a proper link definition passing an object as option", async () => {
const currencyLinks = CurrencyModule.linkable
const regionLinks = RegionModule.linkable
const link = defineLink(currencyLinks.currency, {
linkable: regionLinks.region,
isList: true,
})
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("currencyCurrencyRegionRegionLink")
expect(linkDefinition).toEqual({
serviceName: "currencyCurrencyRegionRegionLink",
isLink: true,
alias: [
{
name: ["currency_region"],
args: {
entity: "LinkCurrencyCurrencyRegionRegion",
},
},
],
primaryKeys: ["id", "currency_code", "region_id"],
relationships: [
{
serviceName: "currency",
primaryKey: "code",
foreignKey: "currency_code",
alias: "currency",
args: {
methodSuffix: "Currencies",
},
},
{
serviceName: "region",
primaryKey: "id",
foreignKey: "region_id",
alias: "region",
args: {
methodSuffix: "Regions",
},
},
],
extends: [
{
serviceName: "currency",
fieldAlias: {
regions: "region_link.region",
},
relationship: {
serviceName: "currencyCurrencyRegionRegionLink",
primaryKey: "currency_code",
foreignKey: "code",
alias: "region_link",
isList: true,
},
},
{
serviceName: "region",
fieldAlias: {
currency: "currency_link.currency",
},
relationship: {
serviceName: "currencyCurrencyRegionRegionLink",
primaryKey: "region_id",
foreignKey: "id",
alias: "currency_link",
isList: false,
},
},
],
})
})
}) })
}, },
}) })
@@ -23,7 +23,7 @@ type InputToJson = {
type CombinedSource = Record<any, any> & InputToJson type CombinedSource = Record<any, any> & InputToJson
type InputOptions = { type InputOptions = {
source: CombinedSource | InputSource linkable: CombinedSource | InputSource
isList?: boolean isList?: boolean
} }
@@ -52,7 +52,7 @@ type ModuleLinkableKeyConfig = {
} }
function isInputOptions(input: any): input is InputOptions { function isInputOptions(input: any): input is InputOptions {
return isObject(input) && "source" in input return isObject(input) && "linkable" in input
} }
function isInputSource(input: any): input is InputSource { function isInputSource(input: any): input is InputSource {
@@ -82,9 +82,9 @@ export function defineLink(
module: source.serviceName, module: source.serviceName,
} }
} else if (isInputOptions(leftService)) { } else if (isInputOptions(leftService)) {
const source = isToJSON(leftService.source) const source = isToJSON(leftService.linkable)
? leftService.source.toJSON() ? leftService.linkable.toJSON()
: leftService.source : leftService.linkable
serviceAObj = { serviceAObj = {
key: source.linkable, key: source.linkable,
@@ -108,9 +108,9 @@ export function defineLink(
module: source.serviceName, module: source.serviceName,
} }
} else if (isInputOptions(rightService)) { } else if (isInputOptions(rightService)) {
const source = isToJSON(rightService.source) const source = isToJSON(rightService.linkable)
? rightService.source.toJSON() ? rightService.linkable.toJSON()
: rightService.source : rightService.linkable
serviceBObj = { serviceBObj = {
key: source.linkable, key: source.linkable,