1 import {
2 Constructor,
3 ILinkModule,
4 ModuleJoinerConfig,
5 } from "@medusajs/framework/types"
6 import { isDefined } from "@medusajs/framework/utils"
7 import { LinkModuleService } from "@services"
8
9 export function getModuleService(
10 joinerConfig: ModuleJoinerConfig
11 ): Constructor<ILinkModule> {
12 const joinerConfig_ = JSON.parse(JSON.stringify(joinerConfig))
13 const databaseConfig = joinerConfig_.databaseConfig
14
15 delete joinerConfig_.databaseConfig
16
17 // Keep the physical table name exposed: the query layer needs it to build
18 // cross-module SQL joins against the link table.
19 if (databaseConfig?.tableName) {
20 joinerConfig_.databaseConfig = { tableName: databaseConfig.tableName }
21 }
22
23 // If extraDataFields is not defined, pick the fields to populate and validate from the
24 // database config if any fields are provided.
25 if (!isDefined(joinerConfig_.extraDataFields)) {
26 joinerConfig_.extraDataFields = Object.keys(
27 databaseConfig?.extraFields || {}
28 )
29 }
30
31 return class LinkService extends LinkModuleService {
32 override __joinerConfig(): ModuleJoinerConfig {
33 return joinerConfig_ as ModuleJoinerConfig
34 }
35 }
36 }
37
38 export function getReadOnlyModuleService(joinerConfig: ModuleJoinerConfig) {
39 return class ReadOnlyLinkService {
40 __joinerConfig(): ModuleJoinerConfig {
41 return joinerConfig as ModuleJoinerConfig
42 }
43 }
44 }