|
1
|
import { MedusaModule, MODULE_SCOPE } from "@medusajs/framework/modules-sdk" |
|
2
|
import { |
|
3
|
ExternalModuleDeclaration, |
|
4
|
ILinkModule, |
|
5
|
InternalModuleDeclaration, |
|
6
|
LinkModuleDefinition, |
|
7
|
ModuleExports, |
|
8
|
ModuleJoinerConfig, |
|
9
|
ModuleServiceInitializeCustomDataLayerOptions, |
|
10
|
ModuleServiceInitializeOptions, |
|
11
|
} from "@medusajs/framework/types" |
|
12
|
import { |
|
13
|
arrayDifference, |
|
14
|
composeLinkName, |
|
15
|
composeTableName, |
|
16
|
ContainerRegistrationKeys, |
|
17
|
isFileSkipped, |
|
18
|
Modules, |
|
19
|
promiseAll, |
|
20
|
simpleHash, |
|
21
|
toPascalCase, |
|
22
|
} from "@medusajs/framework/utils" |
|
23
|
import * as linkDefinitions from "../definitions" |
|
24
|
import { MigrationsExecutionPlanner } from "../migration" |
|
25
|
import { InitializeModuleInjectableDependencies } from "../types" |
|
26
|
import { generateGraphQLSchema } from "../utils" |
|
27
|
import { getLinkModuleDefinition } from "./module-definition" |
|
28
|
|
|
29
|
export const initialize = async ( |
|
30
|
options?: |
|
31
|
| ModuleServiceInitializeOptions |
|
32
|
| ModuleServiceInitializeCustomDataLayerOptions |
|
33
|
| ExternalModuleDeclaration |
|
34
|
| InternalModuleDeclaration, |
|
35
|
pluginLinksDefinitions?: ModuleJoinerConfig[], |
|
36
|
injectedDependencies?: InitializeModuleInjectableDependencies, |
|
37
|
cwd?: string, |
|
38
|
migrationOnly?: boolean, |
|
39
|
schemaOnly?: boolean |
|
40
|
): Promise<{ [link: string]: ILinkModule }> => { |
|
41
|
const allLinks = {} |
|
42
|
const modulesLoadedKeys = MedusaModule.getLoadedModules().map( |
|
43
|
(mod) => Object.keys(mod)[0] |
|
44
|
) |
|
45
|
|
|
46
|
const allLinksToLoad = Object.values(linkDefinitions) |
|
47
|
.concat(pluginLinksDefinitions ?? []) |
|
48
|
.filter((linkDefinition) => !isFileSkipped(linkDefinition)) |
|
49
|
|
|
50
|
await promiseAll( |
|
51
|
allLinksToLoad.map(async (linkDefinition) => { |
|
52
|
const definition: ModuleJoinerConfig = JSON.parse( |
|
53
|
JSON.stringify(linkDefinition) |
|
54
|
) |
|
55
|
|
|
56
|
const [primary, foreign] = definition.relationships ?? [] |
|
57
|
|
|
58
|
if ( |
|
59
|
definition.relationships?.length !== 2 && |
|
60
|
!definition.isReadOnlyLink |
|
61
|
) { |
|
62
|
throw new Error( |
|
63
|
`Link module ${definition.serviceName} can only link 2 modules.` |
|
64
|
) |
|
65
|
} else if ( |
|
66
|
foreign?.foreignKey?.split(",").length > 1 && |
|
67
|
!definition.isReadOnlyLink |
|
68
|
) { |
|
69
|
throw new Error(`Foreign key cannot be a composed key.`) |
|
70
|
} |
|
71
|
|
|
72
|
if (Array.isArray(definition.extraDataFields)) { |
|
73
|
const extraDataFields = definition.extraDataFields |
|
74
|
const definedDbFields = Object.keys( |
|
75
|
definition.databaseConfig?.extraFields || {} |
|
76
|
) |
|
77
|
const difference = arrayDifference(extraDataFields, definedDbFields) |
|
78
|
|
|
79
|
if (difference.length) { |
|
80
|
throw new Error( |
|
81
|
`extraDataFields (fieldNames: ${difference.join( |
|
82
|
"," |
|
83
|
)}) need to be configured under databaseConfig (serviceName: ${ |
|
84
|
definition.serviceName |
|
85
|
}).` |
|
86
|
) |
|
87
|
} |
|
88
|
} |
|
89
|
|
|
90
|
const serviceKey = !definition.isReadOnlyLink |
|
91
|
? definition.serviceName ?? |
|
92
|
composeLinkName( |
|
93
|
primary.serviceName, |
|
94
|
primary.foreignKey, |
|
95
|
foreign.serviceName, |
|
96
|
foreign.foreignKey |
|
97
|
) |
|
98
|
: simpleHash(JSON.stringify(definition.extends)) |
|
99
|
|
|
100
|
if (modulesLoadedKeys.includes(serviceKey)) { |
|
101
|
return |
|
102
|
} else if (serviceKey in allLinks) { |
|
103
|
throw new Error(`Link module ${serviceKey} already defined.`) |
|
104
|
} |
|
105
|
|
|
106
|
if (definition.isReadOnlyLink) { |
|
107
|
const extended: any[] = [] |
|
108
|
for (const extension of definition.extends ?? []) { |
|
109
|
if ( |
|
110
|
modulesLoadedKeys.includes(extension.serviceName) && |
|
111
|
modulesLoadedKeys.includes(extension.relationship.serviceName) |
|
112
|
) { |
|
113
|
extended.push(extension) |
|
114
|
} |
|
115
|
} |
|
116
|
|
|
117
|
definition.extends = extended |
|
118
|
if (extended.length === 0) { |
|
119
|
return |
|
120
|
} |
|
121
|
} else if ( |
|
122
|
!modulesLoadedKeys.includes(primary.serviceName) || |
|
123
|
!modulesLoadedKeys.includes(foreign.serviceName) |
|
124
|
) { |
|
125
|
return |
|
126
|
} |
|
127
|
|
|
128
|
const logger = |
|
129
|
injectedDependencies?.[ContainerRegistrationKeys.LOGGER] ?? console |
|
130
|
|
|
131
|
definition.schema = generateGraphQLSchema(definition, primary, foreign, { |
|
132
|
logger, |
|
133
|
}) |
|
134
|
|
|
135
|
if (!Array.isArray(definition.alias)) { |
|
136
|
definition.alias = definition.alias ? [definition.alias] : [] |
|
137
|
} |
|
138
|
|
|
139
|
for (const alias of definition.alias) { |
|
140
|
alias.args ??= {} |
|
141
|
|
|
142
|
alias.entity = toPascalCase( |
|
143
|
"Link_" + |
|
144
|
(definition.databaseConfig?.tableName ?? |
|
145
|
composeTableName( |
|
146
|
primary.serviceName, |
|
147
|
primary.foreignKey, |
|
148
|
foreign.serviceName, |
|
149
|
foreign.foreignKey |
|
150
|
)) |
|
151
|
) |
|
152
|
} |
|
153
|
|
|
154
|
const moduleDefinition = getLinkModuleDefinition( |
|
155
|
definition, |
|
156
|
primary, |
|
157
|
foreign |
|
158
|
) as ModuleExports |
|
159
|
|
|
160
|
const linkModuleDefinition: LinkModuleDefinition = { |
|
161
|
key: serviceKey, |
|
162
|
label: serviceKey, |
|
163
|
dependencies: [Modules.EVENT_BUS], |
|
164
|
defaultModuleDeclaration: { |
|
165
|
scope: MODULE_SCOPE.INTERNAL, |
|
166
|
}, |
|
167
|
} |
|
168
|
|
|
169
|
const loaded = await MedusaModule.bootstrapLink({ |
|
170
|
definition: linkModuleDefinition, |
|
171
|
declaration: options as InternalModuleDeclaration, |
|
172
|
moduleExports: moduleDefinition, |
|
173
|
injectedDependencies, |
|
174
|
cwd, |
|
175
|
migrationOnly, |
|
176
|
schemaOnly, |
|
177
|
}) |
|
178
|
|
|
179
|
allLinks[serviceKey as string] = Object.values(loaded)[0] |
|
180
|
}) |
|
181
|
) |
|
182
|
|
|
183
|
return allLinks |
|
184
|
} |
|
185
|
|
|
186
|
/** |
|
187
|
* Prepare an execution plan and run the migrations accordingly. |
|
188
|
* It includes creating, updating, deleting the tables according to the execution plan. |
|
189
|
* If any unsafe sql is identified then we will notify the user to act manually. |
|
190
|
* |
|
191
|
* @param options |
|
192
|
* @param pluginLinksDefinition |
|
193
|
*/ |
|
194
|
export function getMigrationPlanner( |
|
195
|
options: ModuleServiceInitializeOptions, |
|
196
|
pluginLinksDefinition?: ModuleJoinerConfig[] |
|
197
|
) { |
|
198
|
const modulesLoadedKeys = MedusaModule.getLoadedModules().map( |
|
199
|
(mod) => Object.keys(mod)[0] |
|
200
|
) |
|
201
|
|
|
202
|
const allLinksToLoad = Object.values(linkDefinitions).concat( |
|
203
|
pluginLinksDefinition ?? [] |
|
204
|
) |
|
205
|
|
|
206
|
const allLinks = new Set<string>() |
|
207
|
for (const definition of allLinksToLoad) { |
|
208
|
if (definition.isReadOnlyLink) { |
|
209
|
continue |
|
210
|
} |
|
211
|
|
|
212
|
if (definition.relationships?.length !== 2) { |
|
213
|
throw new Error( |
|
214
|
`Link module ${definition.serviceName} must have 2 relationships.` |
|
215
|
) |
|
216
|
} |
|
217
|
|
|
218
|
const [primary, foreign] = definition.relationships ?? [] |
|
219
|
const serviceKey = |
|
220
|
definition.serviceName ?? |
|
221
|
composeLinkName( |
|
222
|
primary.serviceName, |
|
223
|
primary.foreignKey, |
|
224
|
foreign.serviceName, |
|
225
|
foreign.foreignKey |
|
226
|
) |
|
227
|
|
|
228
|
if (allLinks.has(serviceKey)) { |
|
229
|
throw new Error(`Link module ${serviceKey} already exists.`) |
|
230
|
} |
|
231
|
|
|
232
|
allLinks.add(serviceKey) |
|
233
|
|
|
234
|
if ( |
|
235
|
!modulesLoadedKeys.includes(primary.serviceName) || |
|
236
|
!modulesLoadedKeys.includes(foreign.serviceName) |
|
237
|
) { |
|
238
|
continue |
|
239
|
} |
|
240
|
} |
|
241
|
|
|
242
|
return new MigrationsExecutionPlanner(allLinksToLoad, options) |
|
243
|
} |