|
1
|
import { moduleProviderLoader } from "@medusajs/framework/modules-sdk" |
|
2
|
import { LoaderOptions, ModulesSdkTypes } from "@medusajs/framework/types" |
|
3
|
import { |
|
4
|
ContainerRegistrationKeys, |
|
5
|
getProviderRegistrationKey, |
|
6
|
} from "@medusajs/framework/utils" |
|
7
|
import { CachingProviderService } from "@services" |
|
8
|
import { |
|
9
|
CachingDefaultProvider, |
|
10
|
CachingIdentifiersRegistrationName, |
|
11
|
CachingModuleOptions, |
|
12
|
CachingProviderRegistrationPrefix, |
|
13
|
} from "@types" |
|
14
|
import { aliasTo, asFunction, asValue, Lifetime } from "awilix" |
|
15
|
import { MemoryCachingProvider } from "../providers/memory-cache" |
|
16
|
import { DefaultCacheStrategy } from "../utils/strategy" |
|
17
|
|
|
18
|
const registrationFn = async (klass, container, { id }) => { |
|
19
|
const key = CachingProviderService.getRegistrationIdentifier(klass) |
|
20
|
|
|
21
|
if (!id) { |
|
22
|
throw new Error(`No "id" provided for provider ${key}`) |
|
23
|
} |
|
24
|
|
|
25
|
const regKey = getProviderRegistrationKey({ |
|
26
|
providerId: id, |
|
27
|
providerIdentifier: key, |
|
28
|
}) |
|
29
|
|
|
30
|
container.register({ |
|
31
|
[CachingProviderRegistrationPrefix + id]: aliasTo(regKey), |
|
32
|
}) |
|
33
|
|
|
34
|
container.registerAdd(CachingIdentifiersRegistrationName, asValue(key)) |
|
35
|
} |
|
36
|
|
|
37
|
export default async ({ |
|
38
|
container, |
|
39
|
options, |
|
40
|
}: LoaderOptions< |
|
41
|
( |
|
42
|
| ModulesSdkTypes.ModuleServiceInitializeOptions |
|
43
|
| ModulesSdkTypes.ModuleServiceInitializeCustomDataLayerOptions |
|
44
|
) & |
|
45
|
CachingModuleOptions |
|
46
|
>): Promise<void> => { |
|
47
|
container.registerAdd(CachingIdentifiersRegistrationName, asValue(undefined)) |
|
48
|
|
|
49
|
const strategy = DefaultCacheStrategy // Re enable custom strategy another time |
|
50
|
container.register("strategy", asValue(strategy)) |
|
51
|
|
|
52
|
const inMemoryOptions = options?.in_memory ?? {} |
|
53
|
const { enable: isInMemoryEnabled, ...restInmemoryOptions } = inMemoryOptions |
|
54
|
|
|
55
|
if (isInMemoryEnabled) { |
|
56
|
// MemoryCachingProvider - default provider |
|
57
|
container.register({ |
|
58
|
[CachingProviderRegistrationPrefix + MemoryCachingProvider.identifier]: |
|
59
|
asFunction( |
|
60
|
(cradle) => new MemoryCachingProvider(cradle, restInmemoryOptions), |
|
61
|
{ |
|
62
|
lifetime: Lifetime.SINGLETON, |
|
63
|
} |
|
64
|
), |
|
65
|
}) |
|
66
|
container.registerAdd( |
|
67
|
CachingIdentifiersRegistrationName, |
|
68
|
asValue(MemoryCachingProvider.identifier) |
|
69
|
) |
|
70
|
container.register( |
|
71
|
CachingDefaultProvider, |
|
72
|
asValue(MemoryCachingProvider.identifier) |
|
73
|
) |
|
74
|
} |
|
75
|
|
|
76
|
// Load other providers |
|
77
|
await moduleProviderLoader({ |
|
78
|
container, |
|
79
|
providers: options?.providers || [], |
|
80
|
registerServiceFn: registrationFn, |
|
81
|
}) |
|
82
|
|
|
83
|
const isSingleProvider = options?.providers?.length === 1 |
|
84
|
let hasDefaultProvider = false |
|
85
|
for (const provider of options?.providers || []) { |
|
86
|
if (provider.is_default || isSingleProvider) { |
|
87
|
hasDefaultProvider = true |
|
88
|
container.register(CachingDefaultProvider, asValue(provider.id)) |
|
89
|
} |
|
90
|
} |
|
91
|
|
|
92
|
const logger = container.resolve(ContainerRegistrationKeys.LOGGER) |
|
93
|
if (!hasDefaultProvider) { |
|
94
|
if (isInMemoryEnabled) { |
|
95
|
logger.warn( |
|
96
|
`[caching-module]: No default caching provider defined. Using "${container.resolve( |
|
97
|
CachingDefaultProvider |
|
98
|
)}" as default.` |
|
99
|
) |
|
100
|
} else { |
|
101
|
throw new Error( |
|
102
|
"[caching-module]: No providers have been configured and the built in memory cache has not been enabled." |
|
103
|
) |
|
104
|
} |
|
105
|
} |
|
106
|
} |