chore: Initialize method for modules (#3649)
What:
- Export initialize methods for all modules to be used as a package.
- Export `runMigrations` and `revertMigration` on modules that DB migrations are available
```typescript
import {
initialize as initializeInventory,
InventoryServiceInitializeOptions,
runMigrations as runMigrationsInventory,
} from "@medusajs/inventory";
import { initialize as eventBusInitialize } from "@medusajs/event-bus-redis";
import { initialize as cacheInitialize } from "@medusajs/cache-redis";
const eventBus = await eventBusInitialize({
redisUrl: "localhost",
});
const cache = await cacheInitialize({
redisUrl: "localhost",
});
const options: InventoryServiceInitializeOptions = {
database: {
type: "postgres",
url: `postgres://postgres:@localhost/inventory`,
},
};
await runMigrationsInventory({
options,
});
const inventoryService = await initializeInventory(options, {
eventBusService: eventBus,
});
const sku = "sku_123"
const item = await service.createInventoryItem({
sku,
});
cache.set(sku, { item });
```
This commit is contained in:
@@ -4,21 +4,29 @@ import {
|
||||
MODULE_SCOPE,
|
||||
} from "@medusajs/types"
|
||||
|
||||
export const MODULE_DEFINITIONS: ModuleDefinition[] = [
|
||||
{
|
||||
key: "eventBus",
|
||||
export enum Modules {
|
||||
EVENT_BUS = "eventBus",
|
||||
STOCK_LOCATION = "stockLocationService",
|
||||
INVENTORY = "inventoryService",
|
||||
CACHE = "cacheService",
|
||||
}
|
||||
|
||||
export const ModulesDefinition: { [key: string]: ModuleDefinition } = {
|
||||
[Modules.EVENT_BUS]: {
|
||||
key: Modules.EVENT_BUS,
|
||||
registrationName: "eventBusModuleService",
|
||||
defaultPackage: "@medusajs/event-bus-local",
|
||||
label: "EventBusModuleService",
|
||||
canOverride: true,
|
||||
isRequired: true,
|
||||
dependencies: ["logger"],
|
||||
defaultModuleDeclaration: {
|
||||
scope: MODULE_SCOPE.INTERNAL,
|
||||
resources: MODULE_RESOURCE_TYPE.SHARED,
|
||||
},
|
||||
},
|
||||
{
|
||||
key: "stockLocationService",
|
||||
[Modules.STOCK_LOCATION]: {
|
||||
key: Modules.STOCK_LOCATION,
|
||||
registrationName: "stockLocationService",
|
||||
defaultPackage: false,
|
||||
label: "StockLocationService",
|
||||
@@ -30,8 +38,8 @@ export const MODULE_DEFINITIONS: ModuleDefinition[] = [
|
||||
resources: MODULE_RESOURCE_TYPE.SHARED,
|
||||
},
|
||||
},
|
||||
{
|
||||
key: "inventoryService",
|
||||
[Modules.INVENTORY]: {
|
||||
key: Modules.INVENTORY,
|
||||
registrationName: "inventoryService",
|
||||
defaultPackage: false,
|
||||
label: "InventoryService",
|
||||
@@ -43,8 +51,8 @@ export const MODULE_DEFINITIONS: ModuleDefinition[] = [
|
||||
resources: MODULE_RESOURCE_TYPE.SHARED,
|
||||
},
|
||||
},
|
||||
{
|
||||
key: "cacheService",
|
||||
[Modules.CACHE]: {
|
||||
key: Modules.CACHE,
|
||||
registrationName: "cacheService",
|
||||
defaultPackage: "@medusajs/cache-inmemory",
|
||||
label: "CacheService",
|
||||
@@ -55,6 +63,9 @@ export const MODULE_DEFINITIONS: ModuleDefinition[] = [
|
||||
resources: MODULE_RESOURCE_TYPE.SHARED,
|
||||
},
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
export const MODULE_DEFINITIONS: ModuleDefinition[] =
|
||||
Object.values(ModulesDefinition)
|
||||
|
||||
export default MODULE_DEFINITIONS
|
||||
|
||||
@@ -50,9 +50,10 @@ export class MedusaModule {
|
||||
const services = {}
|
||||
|
||||
for (const resolution of Object.values(moduleResolutions)) {
|
||||
const keyName = resolution.definition.key
|
||||
const registrationName = resolution.definition.registrationName
|
||||
|
||||
services[registrationName] = container.resolve(registrationName)
|
||||
services[keyName] = container.resolve(registrationName)
|
||||
}
|
||||
|
||||
return services
|
||||
|
||||
Reference in New Issue
Block a user