|
1
|
import { |
|
2
|
ExternalModuleDeclaration, |
|
3
|
IModuleService, |
|
4
|
InternalModuleDeclaration, |
|
5
|
LinkModuleDefinition, |
|
6
|
LoadedModule, |
|
7
|
MedusaContainer, |
|
8
|
ModuleBootstrapDeclaration, |
|
9
|
ModuleDefinition, |
|
10
|
ModuleExports, |
|
11
|
ModuleJoinerConfig, |
|
12
|
ModuleResolution, |
|
13
|
ModuleServiceInitializeOptions, |
|
14
|
SearchTypes, |
|
15
|
} from "@medusajs/types" |
|
16
|
import { |
|
17
|
ContainerRegistrationKeys, |
|
18
|
createMedusaContainer, |
|
19
|
loadDatabaseConfig, |
|
20
|
MedusaError, |
|
21
|
promiseAll, |
|
22
|
simpleHash, |
|
23
|
stringifyCircular, |
|
24
|
} from "@medusajs/utils" |
|
25
|
import { asValue } from "@medusajs/deps/awilix" |
|
26
|
import { EOL } from "os" |
|
27
|
import { |
|
28
|
moduleLoader, |
|
29
|
registerMedusaLinkModule, |
|
30
|
registerMedusaModule, |
|
31
|
} from "./loaders" |
|
32
|
import { loadModuleMigrations } from "./loaders/utils" |
|
33
|
import { MODULE_SCOPE } from "./types" |
|
34
|
|
|
35
|
const logger: any = { |
|
36
|
log: (a) => console.log(a), |
|
37
|
info: (a) => console.log(a), |
|
38
|
warn: (a) => console.warn(a), |
|
39
|
error: (a) => console.error(a), |
|
40
|
} |
|
41
|
|
|
42
|
declare global { |
|
43
|
interface MedusaModule { |
|
44
|
getLoadedModules( |
|
45
|
aliases?: Map<string, string> |
|
46
|
): { [key: string]: LoadedModule }[] |
|
47
|
getModuleInstance(moduleKey: string, alias?: string): LoadedModule |
|
48
|
} |
|
49
|
} |
|
50
|
|
|
51
|
type ModuleAlias = { |
|
52
|
key: string |
|
53
|
hash: string |
|
54
|
isLink: boolean |
|
55
|
alias?: string |
|
56
|
main?: boolean |
|
57
|
} |
|
58
|
|
|
59
|
export type MigrationOptions = { |
|
60
|
moduleKey: string |
|
61
|
modulePath: string |
|
62
|
container?: MedusaContainer |
|
63
|
options?: Record<string, any> |
|
64
|
moduleExports?: ModuleExports |
|
65
|
cwd?: string |
|
66
|
} |
|
67
|
|
|
68
|
export type ModuleBootstrapOptions = { |
|
69
|
moduleKey: string |
|
70
|
defaultPath: string |
|
71
|
declaration?: ModuleBootstrapDeclaration |
|
72
|
moduleExports?: ModuleExports |
|
73
|
sharedContainer?: MedusaContainer |
|
74
|
moduleDefinition?: ModuleDefinition |
|
75
|
injectedDependencies?: Record<string, any> |
|
76
|
/** |
|
77
|
* In this mode, all instances are partially loaded, meaning that the module will not be fully loaded and the services will not be available. |
|
78
|
* Don't forget to clear the instances (MedusaModule.clearInstances()) after the migration are done. |
|
79
|
*/ |
|
80
|
migrationOnly?: boolean |
|
81
|
/** |
|
82
|
* Forces the modules bootstrapper to only run the modules loaders and return prematurely. This |
|
83
|
* is meant for modules that have data loader. In a test env, in order to clear all data |
|
84
|
* and load them back, we need to run those loader again |
|
85
|
*/ |
|
86
|
loaderOnly?: boolean |
|
87
|
workerMode?: "shared" | "worker" | "server" |
|
88
|
cwd?: string |
|
89
|
} |
|
90
|
|
|
91
|
export type LinkModuleBootstrapOptions = { |
|
92
|
definition: LinkModuleDefinition |
|
93
|
declaration?: InternalModuleDeclaration |
|
94
|
moduleExports?: ModuleExports |
|
95
|
injectedDependencies?: Record<string, any> |
|
96
|
cwd?: string |
|
97
|
migrationOnly?: boolean |
|
98
|
schemaOnly?: boolean |
|
99
|
} |
|
100
|
|
|
101
|
export type RegisterModuleJoinerConfig = |
|
102
|
| ModuleJoinerConfig |
|
103
|
| ((modules: ModuleJoinerConfig[]) => ModuleJoinerConfig) |
|
104
|
|
|
105
|
class MedusaModule { |
|
106
|
private static instances_: Map<string, { [key: string]: IModuleService }> = |
|
107
|
new Map() |
|
108
|
private static modules_: Map<string, ModuleAlias[]> = new Map() |
|
109
|
private static customLinks_: RegisterModuleJoinerConfig[] = [] |
|
110
|
private static searchIndexes_: Map< |
|
111
|
string, |
|
112
|
{ definition: SearchTypes.SearchIndexDefinition; filePath?: string } |
|
113
|
> = new Map() |
|
114
|
private static loading_: Map<string, Promise<any>> = new Map() |
|
115
|
private static joinerConfig_: Map<string, ModuleJoinerConfig> = new Map() |
|
116
|
private static moduleResolutions_: Map<string, ModuleResolution> = new Map() |
|
117
|
|
|
118
|
public static getLoadedModules( |
|
119
|
aliases?: Map<string, string> |
|
120
|
): { [key: string]: LoadedModule }[] { |
|
121
|
return [...MedusaModule.modules_.entries()].map(([key]) => { |
|
122
|
if (aliases?.has(key)) { |
|
123
|
return MedusaModule.getModuleInstance(key, aliases.get(key)) |
|
124
|
} |
|
125
|
|
|
126
|
return MedusaModule.getModuleInstance(key) |
|
127
|
}) |
|
128
|
} |
|
129
|
|
|
130
|
public static async onApplicationStart( |
|
131
|
onApplicationStartCb?: () => void |
|
132
|
): Promise<void> { |
|
133
|
await promiseAll( |
|
134
|
[...MedusaModule.instances_.values()] |
|
135
|
.map((instances) => { |
|
136
|
return Object.values(instances).map((instance: IModuleService) => { |
|
137
|
return instance.__hooks?.onApplicationStart |
|
138
|
?.bind(instance)() |
|
139
|
.then(() => { |
|
140
|
onApplicationStartCb?.() |
|
141
|
}) |
|
142
|
.catch(() => { |
|
143
|
// The module should handle this and log it |
|
144
|
return void 0 |
|
145
|
}) |
|
146
|
}) |
|
147
|
}) |
|
148
|
.flat() |
|
149
|
) |
|
150
|
} |
|
151
|
public static async onApplicationShutdown(): Promise<void> { |
|
152
|
await promiseAll( |
|
153
|
[...MedusaModule.instances_.values()] |
|
154
|
.map((instances) => { |
|
155
|
return Object.values(instances).map((instance: IModuleService) => { |
|
156
|
return instance.__hooks?.onApplicationShutdown |
|
157
|
?.bind(instance)() |
|
158
|
.catch(() => { |
|
159
|
// The module should handle this and log it |
|
160
|
return void 0 |
|
161
|
}) |
|
162
|
}) |
|
163
|
}) |
|
164
|
.flat() |
|
165
|
) |
|
166
|
} |
|
167
|
|
|
168
|
public static async onApplicationPrepareShutdown(): Promise<void> { |
|
169
|
await promiseAll( |
|
170
|
[...MedusaModule.instances_.values()] |
|
171
|
.map((instances) => { |
|
172
|
return Object.values(instances).map((instance: IModuleService) => { |
|
173
|
return instance.__hooks?.onApplicationPrepareShutdown |
|
174
|
?.bind(instance)() |
|
175
|
.catch(() => { |
|
176
|
// The module should handle this and log it |
|
177
|
return void 0 |
|
178
|
}) |
|
179
|
}) |
|
180
|
}) |
|
181
|
.flat() |
|
182
|
) |
|
183
|
} |
|
184
|
|
|
185
|
public static clearInstances(): void { |
|
186
|
MedusaModule.instances_.clear() |
|
187
|
MedusaModule.modules_.clear() |
|
188
|
MedusaModule.joinerConfig_.clear() |
|
189
|
MedusaModule.moduleResolutions_.clear() |
|
190
|
MedusaModule.customLinks_.length = 0 |
|
191
|
MedusaModule.searchIndexes_.clear() |
|
192
|
} |
|
193
|
|
|
194
|
public static isInstalled(moduleKey: string, alias?: string): boolean { |
|
195
|
if (alias) { |
|
196
|
return ( |
|
197
|
MedusaModule.modules_.has(moduleKey) && |
|
198
|
MedusaModule.modules_.get(moduleKey)!.some((m) => m.alias === alias) |
|
199
|
) |
|
200
|
} |
|
201
|
|
|
202
|
return MedusaModule.modules_.has(moduleKey) |
|
203
|
} |
|
204
|
|
|
205
|
public static getJoinerConfig(moduleKey: string): ModuleJoinerConfig { |
|
206
|
return MedusaModule.joinerConfig_.get(moduleKey)! |
|
207
|
} |
|
208
|
|
|
209
|
public static getAllJoinerConfigs(): ModuleJoinerConfig[] { |
|
210
|
return [...MedusaModule.joinerConfig_.values()] |
|
211
|
} |
|
212
|
|
|
213
|
public static getModuleResolutions(moduleKey: string): ModuleResolution { |
|
214
|
return MedusaModule.moduleResolutions_.get(moduleKey)! |
|
215
|
} |
|
216
|
|
|
217
|
public static getAllModuleResolutions(): ModuleResolution[] { |
|
218
|
return [...MedusaModule.moduleResolutions_.values()] |
|
219
|
} |
|
220
|
|
|
221
|
public static unregisterModuleResolution(moduleKey: string): void { |
|
222
|
MedusaModule.moduleResolutions_.delete(moduleKey) |
|
223
|
MedusaModule.joinerConfig_.delete(moduleKey) |
|
224
|
const moduleAliases = MedusaModule.modules_ |
|
225
|
.get(moduleKey) |
|
226
|
?.map((m) => m.alias || m.hash) |
|
227
|
if (moduleAliases) { |
|
228
|
for (const alias of moduleAliases) { |
|
229
|
MedusaModule.instances_.delete(alias) |
|
230
|
} |
|
231
|
} |
|
232
|
MedusaModule.modules_.delete(moduleKey) |
|
233
|
} |
|
234
|
|
|
235
|
public static setModuleResolution( |
|
236
|
moduleKey: string, |
|
237
|
resolution: ModuleResolution |
|
238
|
): ModuleResolution { |
|
239
|
MedusaModule.moduleResolutions_.set(moduleKey, resolution) |
|
240
|
|
|
241
|
return resolution |
|
242
|
} |
|
243
|
|
|
244
|
public static setJoinerConfig( |
|
245
|
moduleKey: string, |
|
246
|
config: ModuleJoinerConfig |
|
247
|
): ModuleJoinerConfig { |
|
248
|
MedusaModule.joinerConfig_.set(moduleKey, config) |
|
249
|
|
|
250
|
return config |
|
251
|
} |
|
252
|
|
|
253
|
public static setCustomLink(config: RegisterModuleJoinerConfig): void { |
|
254
|
MedusaModule.customLinks_.push(config) |
|
255
|
} |
|
256
|
|
|
257
|
public static getCustomLinks(): RegisterModuleJoinerConfig[] { |
|
258
|
return MedusaModule.customLinks_ |
|
259
|
} |
|
260
|
|
|
261
|
/** |
|
262
|
* Keyed by index name so that loading the same file twice registers the same index. |
|
263
|
* Two different definitions with the same name (eg. in separate files) is a real conflict, and throws. |
|
264
|
*/ |
|
265
|
public static setSearchIndex( |
|
266
|
definition: SearchTypes.SearchIndexDefinition, |
|
267
|
filePath?: string |
|
268
|
): void { |
|
269
|
const existing = MedusaModule.searchIndexes_.get(definition.name) |
|
270
|
|
|
271
|
// The same object arriving twice is one declaration, not a conflict: |
|
272
|
// `defineSearchIndex` registers the definition it returns, and the returned |
|
273
|
// value may come back through the Search Module's options. |
|
274
|
if (existing?.definition === definition) { |
|
275
|
return |
|
276
|
} |
|
277
|
|
|
278
|
// Same source re-registering (a re-boot in one process) is fine; `filePath` |
|
279
|
// is undefined for both registrations of an inline definition. |
|
280
|
if (existing && existing.filePath !== filePath) { |
|
281
|
const describe = (path?: string) => path ?? "the Search Module's options" |
|
282
|
|
|
283
|
throw new MedusaError( |
|
284
|
MedusaError.Types.INVALID_DATA, |
|
285
|
`Search index "${definition.name}" is defined twice: in ${describe( |
|
286
|
existing.filePath |
|
287
|
)} and ${describe(filePath)}` |
|
288
|
) |
|
289
|
} |
|
290
|
|
|
291
|
MedusaModule.searchIndexes_.set(definition.name, { definition, filePath }) |
|
292
|
} |
|
293
|
|
|
294
|
public static getSearchIndexes(): SearchTypes.SearchIndexDefinition[] { |
|
295
|
return [...MedusaModule.searchIndexes_.values()].map( |
|
296
|
({ definition }) => definition |
|
297
|
) |
|
298
|
} |
|
299
|
|
|
300
|
public static getModuleInstance( |
|
301
|
moduleKey: string, |
|
302
|
alias?: string |
|
303
|
): any | undefined { |
|
304
|
if (!MedusaModule.modules_.has(moduleKey)) { |
|
305
|
return |
|
306
|
} |
|
307
|
|
|
308
|
let mod |
|
309
|
const modules = MedusaModule.modules_.get(moduleKey)! |
|
310
|
if (alias) { |
|
311
|
mod = modules.find((m) => m.alias === alias) |
|
312
|
|
|
313
|
return MedusaModule.instances_.get(mod?.hash) |
|
314
|
} |
|
315
|
|
|
316
|
mod = modules.find((m) => m.main) ?? modules[0] |
|
317
|
|
|
318
|
return MedusaModule.instances_.get(mod?.hash) |
|
319
|
} |
|
320
|
|
|
321
|
private static registerModule( |
|
322
|
moduleKey: string, |
|
323
|
loadedModule: ModuleAlias |
|
324
|
): void { |
|
325
|
if (!MedusaModule.modules_.has(moduleKey)) { |
|
326
|
MedusaModule.modules_.set(moduleKey, []) |
|
327
|
} |
|
328
|
|
|
329
|
const modules = MedusaModule.modules_.get(moduleKey)! |
|
330
|
|
|
331
|
if (modules.some((m) => m.alias === loadedModule.alias)) { |
|
332
|
throw new Error( |
|
333
|
`Module ${moduleKey} already registed as '${loadedModule.alias}'. Please choose a different alias.` |
|
334
|
) |
|
335
|
} |
|
336
|
|
|
337
|
if (loadedModule.main) { |
|
338
|
if (modules.some((m) => m.main)) { |
|
339
|
throw new Error(`Module ${moduleKey} already have a 'main' registered.`) |
|
340
|
} |
|
341
|
} |
|
342
|
|
|
343
|
modules.push(loadedModule) |
|
344
|
MedusaModule.modules_.set(moduleKey, modules!) |
|
345
|
} |
|
346
|
|
|
347
|
/** |
|
348
|
* Load all modules and resolve them once they are loaded |
|
349
|
* @param modulesOptions |
|
350
|
* @param migrationOnly |
|
351
|
* @param loaderOnly |
|
352
|
* @param workerMode |
|
353
|
*/ |
|
354
|
public static async bootstrapAll( |
|
355
|
modulesOptions: Omit< |
|
356
|
ModuleBootstrapOptions, |
|
357
|
"migrationOnly" | "loaderOnly" | "workerMode" | "schemaOnly" |
|
358
|
>[], |
|
359
|
{ |
|
360
|
migrationOnly, |
|
361
|
loaderOnly, |
|
362
|
workerMode, |
|
363
|
schemaOnly, |
|
364
|
cwd, |
|
365
|
}: { |
|
366
|
migrationOnly?: boolean |
|
367
|
loaderOnly?: boolean |
|
368
|
workerMode?: ModuleBootstrapOptions["workerMode"] |
|
369
|
cwd?: string |
|
370
|
schemaOnly?: boolean |
|
371
|
} |
|
372
|
): Promise< |
|
373
|
{ |
|
374
|
[key: string]: any |
|
375
|
}[] |
|
376
|
> { |
|
377
|
return await MedusaModule.bootstrap_(modulesOptions, { |
|
378
|
migrationOnly, |
|
379
|
loaderOnly, |
|
380
|
workerMode, |
|
381
|
cwd, |
|
382
|
schemaOnly, |
|
383
|
}) |
|
384
|
} |
|
385
|
|
|
386
|
/** |
|
387
|
* Load a single module and resolve it once it is loaded |
|
388
|
* @param moduleKey |
|
389
|
* @param defaultPath |
|
390
|
* @param declaration |
|
391
|
* @param moduleExports |
|
392
|
* @param sharedContainer |
|
393
|
* @param moduleDefinition |
|
394
|
* @param injectedDependencies |
|
395
|
* @param migrationOnly |
|
396
|
* @param loaderOnly |
|
397
|
* @param workerMode |
|
398
|
*/ |
|
399
|
public static async bootstrap<T>({ |
|
400
|
moduleKey, |
|
401
|
defaultPath, |
|
402
|
declaration, |
|
403
|
moduleExports, |
|
404
|
sharedContainer, |
|
405
|
moduleDefinition, |
|
406
|
injectedDependencies, |
|
407
|
migrationOnly, |
|
408
|
loaderOnly, |
|
409
|
workerMode, |
|
410
|
cwd, |
|
411
|
}: ModuleBootstrapOptions): Promise<{ |
|
412
|
[key: string]: T |
|
413
|
}> { |
|
414
|
const [service] = await MedusaModule.bootstrap_( |
|
415
|
[ |
|
416
|
{ |
|
417
|
moduleKey, |
|
418
|
defaultPath, |
|
419
|
declaration, |
|
420
|
moduleExports, |
|
421
|
sharedContainer, |
|
422
|
moduleDefinition, |
|
423
|
injectedDependencies, |
|
424
|
}, |
|
425
|
], |
|
426
|
{ |
|
427
|
migrationOnly, |
|
428
|
loaderOnly, |
|
429
|
workerMode, |
|
430
|
cwd, |
|
431
|
} |
|
432
|
) |
|
433
|
|
|
434
|
return service as { |
|
435
|
[key: string]: T |
|
436
|
} |
|
437
|
} |
|
438
|
|
|
439
|
/** |
|
440
|
* Load all modules and then resolve them once they are loaded |
|
441
|
* |
|
442
|
* @param modulesOptions |
|
443
|
* @param migrationOnly |
|
444
|
* @param loaderOnly |
|
445
|
* @param workerMode |
|
446
|
* @protected |
|
447
|
*/ |
|
448
|
protected static async bootstrap_<T>( |
|
449
|
modulesOptions: Omit< |
|
450
|
ModuleBootstrapOptions, |
|
451
|
"migrationOnly" | "loaderOnly" | "workerMode" | "cwd" | "schemaOnly" |
|
452
|
>[], |
|
453
|
{ |
|
454
|
migrationOnly, |
|
455
|
loaderOnly, |
|
456
|
workerMode, |
|
457
|
cwd = process.cwd(), |
|
458
|
schemaOnly, |
|
459
|
}: { |
|
460
|
migrationOnly?: boolean |
|
461
|
loaderOnly?: boolean |
|
462
|
workerMode?: "shared" | "worker" | "server" |
|
463
|
cwd?: string |
|
464
|
schemaOnly?: boolean |
|
465
|
} |
|
466
|
): Promise< |
|
467
|
{ |
|
468
|
[key: string]: T |
|
469
|
}[] |
|
470
|
> { |
|
471
|
let loadedModules: { |
|
472
|
hashKey: string |
|
473
|
modDeclaration: InternalModuleDeclaration | ExternalModuleDeclaration |
|
474
|
moduleResolutions: Record<string, ModuleResolution> |
|
475
|
container: MedusaContainer |
|
476
|
finishLoading: (arg: { [Key: string]: any }) => void |
|
477
|
}[] = [] |
|
478
|
|
|
479
|
const services: { [Key: string]: any }[] = [] |
|
480
|
|
|
481
|
await promiseAll( |
|
482
|
modulesOptions.map(async (moduleOptions) => { |
|
483
|
const { |
|
484
|
moduleKey, |
|
485
|
defaultPath, |
|
486
|
declaration, |
|
487
|
moduleExports, |
|
488
|
sharedContainer, |
|
489
|
moduleDefinition, |
|
490
|
injectedDependencies, |
|
491
|
} = moduleOptions |
|
492
|
|
|
493
|
const hashKey = simpleHash( |
|
494
|
stringifyCircular({ moduleKey, defaultPath, declaration }) |
|
495
|
) |
|
496
|
|
|
497
|
let finishLoading: any |
|
498
|
let errorLoading: any |
|
499
|
|
|
500
|
const loadingPromise = new Promise((resolve, reject) => { |
|
501
|
finishLoading = resolve |
|
502
|
errorLoading = reject |
|
503
|
}) |
|
504
|
|
|
505
|
if (!loaderOnly && MedusaModule.instances_.has(hashKey)) { |
|
506
|
services.push(MedusaModule.instances_.get(hashKey)!) |
|
507
|
return |
|
508
|
} |
|
509
|
|
|
510
|
if (!loaderOnly && MedusaModule.loading_.has(hashKey)) { |
|
511
|
services.push(await MedusaModule.loading_.get(hashKey)) |
|
512
|
return |
|
513
|
} |
|
514
|
|
|
515
|
if (!loaderOnly) { |
|
516
|
MedusaModule.loading_.set(hashKey, loadingPromise) |
|
517
|
} |
|
518
|
|
|
519
|
let modDeclaration = |
|
520
|
declaration ?? |
|
521
|
({} as InternalModuleDeclaration | ExternalModuleDeclaration) |
|
522
|
|
|
523
|
if (declaration?.scope !== MODULE_SCOPE.EXTERNAL) { |
|
524
|
modDeclaration = { |
|
525
|
scope: declaration?.scope || MODULE_SCOPE.INTERNAL, |
|
526
|
resolve: defaultPath, |
|
527
|
options: declaration?.options ?? declaration, |
|
528
|
dependencies: |
|
529
|
(declaration as InternalModuleDeclaration)?.dependencies ?? [], |
|
530
|
alias: declaration?.alias, |
|
531
|
main: declaration?.main, |
|
532
|
worker_mode: workerMode, |
|
533
|
} as InternalModuleDeclaration |
|
534
|
} |
|
535
|
|
|
536
|
const container = sharedContainer ?? createMedusaContainer() |
|
537
|
|
|
538
|
if (injectedDependencies) { |
|
539
|
for (const service in injectedDependencies) { |
|
540
|
container.register(service, asValue(injectedDependencies[service])) |
|
541
|
if (!container.hasRegistration(service)) { |
|
542
|
container.register( |
|
543
|
service, |
|
544
|
asValue(injectedDependencies[service]) |
|
545
|
) |
|
546
|
} |
|
547
|
} |
|
548
|
} |
|
549
|
|
|
550
|
const moduleResolutions = registerMedusaModule({ |
|
551
|
moduleKey, |
|
552
|
moduleDeclaration: modDeclaration!, |
|
553
|
moduleExports, |
|
554
|
definition: moduleDefinition, |
|
555
|
cwd, |
|
556
|
}) |
|
557
|
|
|
558
|
const logger_ = |
|
559
|
container.resolve(ContainerRegistrationKeys.LOGGER, { |
|
560
|
allowUnregistered: true, |
|
561
|
}) ?? logger |
|
562
|
|
|
563
|
try { |
|
564
|
await moduleLoader({ |
|
565
|
container, |
|
566
|
moduleResolutions, |
|
567
|
logger: logger_, |
|
568
|
migrationOnly, |
|
569
|
schemaOnly, |
|
570
|
loaderOnly, |
|
571
|
}) |
|
572
|
} catch (err) { |
|
573
|
errorLoading(err) |
|
574
|
throw err |
|
575
|
} |
|
576
|
|
|
577
|
loadedModules.push({ |
|
578
|
hashKey, |
|
579
|
modDeclaration, |
|
580
|
moduleResolutions, |
|
581
|
container, |
|
582
|
finishLoading, |
|
583
|
}) |
|
584
|
}) |
|
585
|
) |
|
586
|
|
|
587
|
if (loaderOnly) { |
|
588
|
loadedModules.forEach(({ finishLoading }) => finishLoading({})) |
|
589
|
return [{}] |
|
590
|
} |
|
591
|
|
|
592
|
const resolvedServices = await promiseAll( |
|
593
|
loadedModules.map( |
|
594
|
async ({ |
|
595
|
hashKey, |
|
596
|
modDeclaration, |
|
597
|
moduleResolutions, |
|
598
|
container, |
|
599
|
finishLoading, |
|
600
|
}) => { |
|
601
|
const service = await MedusaModule.resolveLoadedModule({ |
|
602
|
hashKey, |
|
603
|
modDeclaration, |
|
604
|
moduleResolutions, |
|
605
|
container, |
|
606
|
}) |
|
607
|
|
|
608
|
MedusaModule.instances_.set(hashKey, service) |
|
609
|
finishLoading(service) |
|
610
|
MedusaModule.loading_.delete(hashKey) |
|
611
|
return service |
|
612
|
} |
|
613
|
) |
|
614
|
) |
|
615
|
|
|
616
|
services.push(...resolvedServices) |
|
617
|
|
|
618
|
return services |
|
619
|
} |
|
620
|
|
|
621
|
/** |
|
622
|
* Resolve all the modules once they all have been loaded through the bootstrap |
|
623
|
* and store their references in the instances_ map and return them |
|
624
|
* |
|
625
|
* @param hashKey |
|
626
|
* @param modDeclaration |
|
627
|
* @param moduleResolutions |
|
628
|
* @param container |
|
629
|
* @private |
|
630
|
*/ |
|
631
|
private static async resolveLoadedModule({ |
|
632
|
hashKey, |
|
633
|
modDeclaration, |
|
634
|
moduleResolutions, |
|
635
|
container, |
|
636
|
}: { |
|
637
|
hashKey: string |
|
638
|
modDeclaration: InternalModuleDeclaration | ExternalModuleDeclaration |
|
639
|
moduleResolutions: Record<string, ModuleResolution> |
|
640
|
container: MedusaContainer |
|
641
|
}): Promise<{ |
|
642
|
[key: string]: any |
|
643
|
}> { |
|
644
|
const logger_ = |
|
645
|
container.resolve(ContainerRegistrationKeys.LOGGER, { |
|
646
|
allowUnregistered: true, |
|
647
|
}) ?? logger |
|
648
|
|
|
649
|
const services: { [key: string]: any } = {} |
|
650
|
|
|
651
|
for (const resolution of Object.values( |
|
652
|
moduleResolutions |
|
653
|
) as ModuleResolution[]) { |
|
654
|
const keyName = resolution.definition.key |
|
655
|
|
|
656
|
services[keyName] = container.resolve(keyName) |
|
657
|
services[keyName].__definition = resolution.definition |
|
658
|
services[keyName].__definition.resolvePath = |
|
659
|
"resolve" in modDeclaration && |
|
660
|
typeof modDeclaration.resolve === "string" |
|
661
|
? modDeclaration.resolve |
|
662
|
: undefined |
|
663
|
|
|
664
|
if (resolution.definition.isQueryable) { |
|
665
|
let joinerConfig!: ModuleJoinerConfig |
|
666
|
|
|
667
|
try { |
|
668
|
// TODO: rework that to store on a separate property |
|
669
|
joinerConfig = |
|
670
|
typeof services[keyName].__joinerConfig === "function" |
|
671
|
? await services[keyName].__joinerConfig?.() |
|
672
|
: services[keyName].__joinerConfig |
|
673
|
} catch { |
|
674
|
// noop |
|
675
|
} |
|
676
|
|
|
677
|
if (!joinerConfig) { |
|
678
|
throw new Error( |
|
679
|
`Your module is missing a joiner config: ${keyName}. If this module is not queryable, please set { definition: { isQueryable: false } } in your module configuration.` |
|
680
|
) |
|
681
|
} |
|
682
|
|
|
683
|
if (!joinerConfig.primaryKeys) { |
|
684
|
logger_.warn( |
|
685
|
`Primary keys are not defined by the module ${keyName}. Setting default primary key to 'id'${EOL}` |
|
686
|
) |
|
687
|
|
|
688
|
joinerConfig.primaryKeys = ["id"] |
|
689
|
} |
|
690
|
|
|
691
|
joinerConfig = { |
|
692
|
...joinerConfig, |
|
693
|
databaseClientUrl: resolveJoinerConfigDatabaseClientUrl(resolution), |
|
694
|
} |
|
695
|
|
|
696
|
services[keyName].__joinerConfig = joinerConfig |
|
697
|
MedusaModule.setJoinerConfig(keyName, joinerConfig) |
|
698
|
} |
|
699
|
|
|
700
|
MedusaModule.setModuleResolution(keyName, resolution) |
|
701
|
|
|
702
|
MedusaModule.registerModule(keyName, { |
|
703
|
key: keyName, |
|
704
|
hash: hashKey, |
|
705
|
alias: modDeclaration.alias ?? hashKey, |
|
706
|
main: !!modDeclaration.main, |
|
707
|
isLink: false, |
|
708
|
}) |
|
709
|
} |
|
710
|
|
|
711
|
return services |
|
712
|
} |
|
713
|
|
|
714
|
public static async bootstrapLink({ |
|
715
|
definition, |
|
716
|
declaration, |
|
717
|
moduleExports, |
|
718
|
injectedDependencies, |
|
719
|
cwd, |
|
720
|
migrationOnly, |
|
721
|
schemaOnly, |
|
722
|
}: LinkModuleBootstrapOptions): Promise<{ |
|
723
|
[key: string]: unknown |
|
724
|
}> { |
|
725
|
const moduleKey = definition.key |
|
726
|
const hashKey = simpleHash(stringifyCircular({ moduleKey, declaration })) |
|
727
|
|
|
728
|
if (MedusaModule.instances_.has(hashKey)) { |
|
729
|
return { [moduleKey]: MedusaModule.instances_.get(hashKey) } |
|
730
|
} |
|
731
|
|
|
732
|
if (MedusaModule.loading_.has(hashKey)) { |
|
733
|
return await MedusaModule.loading_.get(hashKey) |
|
734
|
} |
|
735
|
|
|
736
|
let finishLoading: any |
|
737
|
let errorLoading: any |
|
738
|
MedusaModule.loading_.set( |
|
739
|
hashKey, |
|
740
|
new Promise((resolve, reject) => { |
|
741
|
finishLoading = resolve |
|
742
|
errorLoading = reject |
|
743
|
}) |
|
744
|
) |
|
745
|
|
|
746
|
let modDeclaration = |
|
747
|
declaration ?? ({} as Partial<InternalModuleDeclaration>) |
|
748
|
|
|
749
|
const moduleDefinition: ModuleDefinition = { |
|
750
|
key: definition.key, |
|
751
|
dependencies: definition.dependencies, |
|
752
|
defaultPackage: "", |
|
753
|
label: definition.label, |
|
754
|
isRequired: false, |
|
755
|
isQueryable: true, |
|
756
|
defaultModuleDeclaration: definition.defaultModuleDeclaration, |
|
757
|
} |
|
758
|
|
|
759
|
modDeclaration = { |
|
760
|
resolve: "", |
|
761
|
options: declaration, |
|
762
|
alias: declaration?.alias, |
|
763
|
main: declaration?.main, |
|
764
|
} |
|
765
|
|
|
766
|
const container = createMedusaContainer() |
|
767
|
|
|
768
|
if (injectedDependencies) { |
|
769
|
for (const service in injectedDependencies) { |
|
770
|
container.register(service, asValue(injectedDependencies[service])) |
|
771
|
} |
|
772
|
} |
|
773
|
|
|
774
|
const moduleResolutions = registerMedusaLinkModule( |
|
775
|
moduleDefinition, |
|
776
|
modDeclaration as InternalModuleDeclaration, |
|
777
|
moduleExports, |
|
778
|
cwd |
|
779
|
) |
|
780
|
|
|
781
|
const logger_ = |
|
782
|
container.resolve(ContainerRegistrationKeys.LOGGER, { |
|
783
|
allowUnregistered: true, |
|
784
|
}) ?? logger |
|
785
|
|
|
786
|
try { |
|
787
|
await moduleLoader({ |
|
788
|
container, |
|
789
|
moduleResolutions, |
|
790
|
migrationOnly, |
|
791
|
schemaOnly, |
|
792
|
logger: logger_, |
|
793
|
}) |
|
794
|
} catch (err) { |
|
795
|
errorLoading(err) |
|
796
|
throw err |
|
797
|
} |
|
798
|
|
|
799
|
const services = {} |
|
800
|
|
|
801
|
for (const resolution of Object.values( |
|
802
|
moduleResolutions |
|
803
|
) as ModuleResolution[]) { |
|
804
|
const keyName = resolution.definition.key |
|
805
|
|
|
806
|
services[keyName] = container.resolve(keyName) |
|
807
|
services[keyName].__definition = resolution.definition |
|
808
|
|
|
809
|
if (resolution.definition.isQueryable) { |
|
810
|
let joinerConfig!: ModuleJoinerConfig |
|
811
|
|
|
812
|
try { |
|
813
|
joinerConfig = await services[keyName].__joinerConfig?.() |
|
814
|
} catch { |
|
815
|
// noop |
|
816
|
} |
|
817
|
|
|
818
|
if (!joinerConfig) { |
|
819
|
throw new Error( |
|
820
|
`Your module is missing a joiner config: ${keyName}. If this module is not queryable, please set { definition: { isQueryable: false } } in your module configuration.` |
|
821
|
) |
|
822
|
} |
|
823
|
|
|
824
|
joinerConfig = { |
|
825
|
...joinerConfig, |
|
826
|
databaseClientUrl: resolveJoinerConfigDatabaseClientUrl(resolution), |
|
827
|
} |
|
828
|
|
|
829
|
services[keyName].__joinerConfig = joinerConfig |
|
830
|
MedusaModule.setJoinerConfig(keyName, joinerConfig) |
|
831
|
|
|
832
|
if (!joinerConfig.isLink) { |
|
833
|
throw new Error( |
|
834
|
"MedusaModule.bootstrapLink must be used only for Link Modules" |
|
835
|
) |
|
836
|
} |
|
837
|
} |
|
838
|
|
|
839
|
MedusaModule.setModuleResolution(keyName, resolution) |
|
840
|
MedusaModule.registerModule(keyName, { |
|
841
|
key: keyName, |
|
842
|
hash: hashKey, |
|
843
|
alias: modDeclaration.alias ?? hashKey, |
|
844
|
main: !!modDeclaration.main, |
|
845
|
isLink: true, |
|
846
|
}) |
|
847
|
} |
|
848
|
|
|
849
|
MedusaModule.instances_.set(hashKey, services) |
|
850
|
finishLoading(services) |
|
851
|
MedusaModule.loading_.delete(hashKey) |
|
852
|
|
|
853
|
return services |
|
854
|
} |
|
855
|
|
|
856
|
public static async migrateGenerate({ |
|
857
|
options, |
|
858
|
container, |
|
859
|
moduleExports, |
|
860
|
moduleKey, |
|
861
|
modulePath, |
|
862
|
cwd, |
|
863
|
}: MigrationOptions): Promise<void> { |
|
864
|
const moduleResolutions = registerMedusaModule({ |
|
865
|
moduleKey, |
|
866
|
moduleDeclaration: { |
|
867
|
scope: MODULE_SCOPE.INTERNAL, |
|
868
|
resolve: modulePath, |
|
869
|
options, |
|
870
|
}, |
|
871
|
cwd, |
|
872
|
}) |
|
873
|
|
|
874
|
const logger_ = |
|
875
|
container?.resolve(ContainerRegistrationKeys.LOGGER, { |
|
876
|
allowUnregistered: true, |
|
877
|
}) ?? logger |
|
878
|
|
|
879
|
container ??= createMedusaContainer() |
|
880
|
|
|
881
|
for (const mod in moduleResolutions) { |
|
882
|
const { generateMigration } = await loadModuleMigrations( |
|
883
|
container, |
|
884
|
moduleResolutions[mod], |
|
885
|
moduleExports |
|
886
|
) |
|
887
|
|
|
888
|
if (typeof generateMigration === "function") { |
|
889
|
await generateMigration({ |
|
890
|
options, |
|
891
|
container: container!, |
|
892
|
logger: logger_, |
|
893
|
}) |
|
894
|
} |
|
895
|
} |
|
896
|
} |
|
897
|
|
|
898
|
public static async migrateUp({ |
|
899
|
options, |
|
900
|
container, |
|
901
|
moduleExports, |
|
902
|
moduleKey, |
|
903
|
modulePath, |
|
904
|
cwd, |
|
905
|
}: MigrationOptions): Promise<{ name: string; path: string }[]> { |
|
906
|
const moduleResolutions = registerMedusaModule({ |
|
907
|
moduleKey, |
|
908
|
moduleDeclaration: { |
|
909
|
scope: MODULE_SCOPE.INTERNAL, |
|
910
|
resolve: modulePath, |
|
911
|
options, |
|
912
|
}, |
|
913
|
cwd, |
|
914
|
}) |
|
915
|
|
|
916
|
const logger_ = |
|
917
|
container?.resolve(ContainerRegistrationKeys.LOGGER, { |
|
918
|
allowUnregistered: true, |
|
919
|
}) ?? logger |
|
920
|
|
|
921
|
container ??= createMedusaContainer() |
|
922
|
|
|
923
|
let result: { name: string; path: string }[] = [] |
|
924
|
for (const mod in moduleResolutions) { |
|
925
|
const { runMigrations } = await loadModuleMigrations( |
|
926
|
container, |
|
927
|
moduleResolutions[mod], |
|
928
|
moduleExports |
|
929
|
) |
|
930
|
|
|
931
|
if (typeof runMigrations === "function") { |
|
932
|
const res = await runMigrations({ |
|
933
|
options, |
|
934
|
container: container!, |
|
935
|
logger: logger_, |
|
936
|
}) |
|
937
|
result.push(...res) |
|
938
|
} |
|
939
|
} |
|
940
|
|
|
941
|
return result |
|
942
|
} |
|
943
|
|
|
944
|
public static async migrateDown( |
|
945
|
{ |
|
946
|
options, |
|
947
|
container, |
|
948
|
moduleExports, |
|
949
|
moduleKey, |
|
950
|
modulePath, |
|
951
|
cwd, |
|
952
|
}: MigrationOptions, |
|
953
|
migrationNames?: string[] |
|
954
|
): Promise<void> { |
|
955
|
const moduleResolutions = registerMedusaModule({ |
|
956
|
moduleKey, |
|
957
|
moduleDeclaration: { |
|
958
|
scope: MODULE_SCOPE.INTERNAL, |
|
959
|
resolve: modulePath, |
|
960
|
options, |
|
961
|
}, |
|
962
|
cwd, |
|
963
|
}) |
|
964
|
|
|
965
|
const logger_ = |
|
966
|
container?.resolve(ContainerRegistrationKeys.LOGGER, { |
|
967
|
allowUnregistered: true, |
|
968
|
}) ?? logger |
|
969
|
|
|
970
|
container ??= createMedusaContainer() |
|
971
|
|
|
972
|
for (const mod in moduleResolutions) { |
|
973
|
const { revertMigration } = await loadModuleMigrations( |
|
974
|
container, |
|
975
|
moduleResolutions[mod], |
|
976
|
moduleExports |
|
977
|
) |
|
978
|
|
|
979
|
if (typeof revertMigration === "function") { |
|
980
|
await revertMigration({ |
|
981
|
options, |
|
982
|
container: container!, |
|
983
|
logger: logger_, |
|
984
|
migrationNames, |
|
985
|
}) |
|
986
|
} |
|
987
|
} |
|
988
|
} |
|
989
|
} |
|
990
|
|
|
991
|
function resolveJoinerConfigDatabaseClientUrl( |
|
992
|
resolution: ModuleResolution |
|
993
|
): string | undefined { |
|
994
|
const declaration = resolution.moduleDeclaration |
|
995
|
const options = |
|
996
|
declaration && typeof declaration === "object" && "options" in declaration |
|
997
|
? (declaration.options as ModuleServiceInitializeOptions | undefined) |
|
998
|
: (resolution.options as ModuleServiceInitializeOptions | undefined) |
|
999
|
|
|
1000
|
if (!options) { |
|
1001
|
return undefined |
|
1002
|
} |
|
1003
|
|
|
1004
|
try { |
|
1005
|
return loadDatabaseConfig(resolution.definition.key, options, true) |
|
1006
|
.clientUrl |
|
1007
|
} catch { |
|
1008
|
return undefined |
|
1009
|
} |
|
1010
|
} |
|
1011
|
|
|
1012
|
global.MedusaModule ??= MedusaModule |
|
1013
|
const GlobalMedusaModule = global.MedusaModule as typeof MedusaModule |
|
1014
|
|
|
1015
|
export { GlobalMedusaModule as MedusaModule } |