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:
@@ -0,0 +1,11 @@
|
||||
---
|
||||
"@medusajs/event-bus-local": patch
|
||||
"@medusajs/event-bus-redis": patch
|
||||
"@medusajs/cache-inmemory": patch
|
||||
"@medusajs/stock-location": patch
|
||||
"@medusajs/cache-redis": patch
|
||||
"@medusajs/modules-sdk": patch
|
||||
"@medusajs/inventory": patch
|
||||
---
|
||||
|
||||
Export initialize method for all modules
|
||||
@@ -1,5 +1,4 @@
|
||||
import { ModuleExports } from "@medusajs/modules-sdk"
|
||||
|
||||
import InMemoryCacheService from "./services/inmemory-cache"
|
||||
|
||||
const service = InMemoryCacheService
|
||||
@@ -9,3 +8,5 @@ const moduleDefinition: ModuleExports = {
|
||||
}
|
||||
|
||||
export default moduleDefinition
|
||||
export * from "./initialize"
|
||||
export * from "./types"
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
import {
|
||||
ExternalModuleDeclaration,
|
||||
InternalModuleDeclaration,
|
||||
MedusaModule,
|
||||
Modules,
|
||||
} from "@medusajs/modules-sdk"
|
||||
import { ICacheService } from "@medusajs/types"
|
||||
import { InMemoryCacheModuleOptions } from "../types"
|
||||
|
||||
export const initialize = async (
|
||||
options?: InMemoryCacheModuleOptions | ExternalModuleDeclaration
|
||||
): Promise<ICacheService> => {
|
||||
const serviceKey = Modules.CACHE
|
||||
const loaded = await MedusaModule.bootstrap(
|
||||
serviceKey,
|
||||
"@medusajs/cache-inmemory",
|
||||
options as InternalModuleDeclaration | ExternalModuleDeclaration,
|
||||
undefined
|
||||
)
|
||||
|
||||
return loaded[serviceKey] as ICacheService
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
import { ModuleExports } from "@medusajs/modules-sdk"
|
||||
|
||||
import Loader from "./loaders"
|
||||
import { RedisCacheService } from "./services"
|
||||
|
||||
@@ -12,3 +11,5 @@ const moduleDefinition: ModuleExports = {
|
||||
}
|
||||
|
||||
export default moduleDefinition
|
||||
export * from "./initialize"
|
||||
export * from "./types"
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
import {
|
||||
ExternalModuleDeclaration,
|
||||
InternalModuleDeclaration,
|
||||
MedusaModule,
|
||||
Modules,
|
||||
} from "@medusajs/modules-sdk"
|
||||
import { ICacheService } from "@medusajs/types"
|
||||
import { RedisCacheModuleOptions } from "../types"
|
||||
|
||||
export const initialize = async (
|
||||
options?: RedisCacheModuleOptions | ExternalModuleDeclaration
|
||||
): Promise<ICacheService> => {
|
||||
const serviceKey = Modules.CACHE
|
||||
const loaded = await MedusaModule.bootstrap(
|
||||
serviceKey,
|
||||
"@medusajs/cache-redis",
|
||||
options as InternalModuleDeclaration | ExternalModuleDeclaration,
|
||||
undefined
|
||||
)
|
||||
|
||||
return loaded[serviceKey] as ICacheService
|
||||
}
|
||||
@@ -11,3 +11,4 @@ const moduleDefinition: ModuleExports = {
|
||||
}
|
||||
|
||||
export default moduleDefinition
|
||||
export * from "./initialize"
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
import { MedusaModule, Modules } from "@medusajs/modules-sdk"
|
||||
import { IEventBusService } from "@medusajs/types"
|
||||
|
||||
export const initialize = async (): Promise<IEventBusService> => {
|
||||
const serviceKey = Modules.EVENT_BUS
|
||||
const loaded = await MedusaModule.bootstrap(
|
||||
serviceKey,
|
||||
"@medusajs/event-bus-local"
|
||||
)
|
||||
|
||||
return loaded[serviceKey] as IEventBusService
|
||||
}
|
||||
@@ -11,3 +11,5 @@ const moduleDefinition: ModuleExports = {
|
||||
}
|
||||
|
||||
export default moduleDefinition
|
||||
export * from "./initialize"
|
||||
export * from "./types"
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
import {
|
||||
ExternalModuleDeclaration,
|
||||
InternalModuleDeclaration,
|
||||
MedusaModule,
|
||||
Modules,
|
||||
} from "@medusajs/modules-sdk"
|
||||
import { IEventBusService } from "@medusajs/types"
|
||||
import { EventBusRedisModuleOptions } from "../types"
|
||||
|
||||
export const initialize = async (
|
||||
options?: EventBusRedisModuleOptions | ExternalModuleDeclaration
|
||||
): Promise<IEventBusService> => {
|
||||
const serviceKey = Modules.EVENT_BUS
|
||||
const loaded = await MedusaModule.bootstrap(
|
||||
serviceKey,
|
||||
"@medusajs/event-bus-redis",
|
||||
options as InternalModuleDeclaration | ExternalModuleDeclaration,
|
||||
undefined
|
||||
)
|
||||
|
||||
return loaded[serviceKey] as IEventBusService
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import { InternalModuleDeclaration, Logger } from "@medusajs/modules-sdk"
|
||||
import { ConfigModule, EmitData } from "@medusajs/types"
|
||||
import { EmitData } from "@medusajs/types"
|
||||
import { AbstractEventBusModuleService } from "@medusajs/utils"
|
||||
import { BulkJobOptions, JobsOptions, Queue, Worker } from "bullmq"
|
||||
import { Redis } from "ioredis"
|
||||
@@ -7,7 +7,6 @@ import { BullJob, EmitOptions, EventBusRedisModuleOptions } from "../types"
|
||||
|
||||
type InjectedDependencies = {
|
||||
logger: Logger
|
||||
configModule: ConfigModule
|
||||
eventBusRedisConnection: Redis
|
||||
}
|
||||
|
||||
@@ -17,7 +16,6 @@ type InjectedDependencies = {
|
||||
*/
|
||||
// eslint-disable-next-line max-len
|
||||
export default class RedisEventBusService extends AbstractEventBusModuleService {
|
||||
protected readonly config_: ConfigModule
|
||||
protected readonly logger_: Logger
|
||||
protected readonly moduleOptions_: EventBusRedisModuleOptions
|
||||
// eslint-disable-next-line max-len
|
||||
@@ -26,7 +24,7 @@ export default class RedisEventBusService extends AbstractEventBusModuleService
|
||||
protected queue_: Queue
|
||||
|
||||
constructor(
|
||||
{ configModule, logger, eventBusRedisConnection }: InjectedDependencies,
|
||||
{ logger, eventBusRedisConnection }: InjectedDependencies,
|
||||
moduleOptions: EventBusRedisModuleOptions = {},
|
||||
moduleDeclaration: InternalModuleDeclaration
|
||||
) {
|
||||
@@ -35,7 +33,6 @@ export default class RedisEventBusService extends AbstractEventBusModuleService
|
||||
super(...arguments)
|
||||
|
||||
this.moduleOptions_ = moduleOptions
|
||||
this.config_ = configModule
|
||||
this.logger_ = logger
|
||||
|
||||
this.queue_ = new Queue(moduleOptions.queueName ?? `events-queue`, {
|
||||
|
||||
@@ -22,6 +22,6 @@ const moduleDefinition: ModuleExports = {
|
||||
}
|
||||
|
||||
export default moduleDefinition
|
||||
|
||||
export * from "./initialize"
|
||||
export { revertMigration, runMigrations } from "./migrations/run-migration"
|
||||
export * from "./types"
|
||||
|
||||
@@ -2,17 +2,18 @@ import {
|
||||
ExternalModuleDeclaration,
|
||||
InternalModuleDeclaration,
|
||||
MedusaModule,
|
||||
Modules,
|
||||
} from "@medusajs/modules-sdk"
|
||||
import { IEventBusService, IInventoryService } from "@medusajs/types"
|
||||
import { InventoryServiceInitializeOptions } from "../types"
|
||||
|
||||
export const initialize = async (
|
||||
options?: InventoryServiceInitializeOptions | ExternalModuleDeclaration,
|
||||
options: InventoryServiceInitializeOptions | ExternalModuleDeclaration,
|
||||
injectedDependencies?: {
|
||||
eventBusService: IEventBusService
|
||||
}
|
||||
): Promise<IInventoryService> => {
|
||||
const serviceKey = "inventoryService"
|
||||
const serviceKey = Modules.INVENTORY
|
||||
const loaded = await MedusaModule.bootstrap(
|
||||
serviceKey,
|
||||
"@medusajs/inventory",
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -19,3 +19,6 @@ const moduleDefinition: ModuleExports = {
|
||||
}
|
||||
|
||||
export default moduleDefinition
|
||||
export * from "./initialize"
|
||||
export { revertMigration, runMigrations } from "./migrations/run-migration"
|
||||
export * from "./types"
|
||||
|
||||
@@ -2,17 +2,18 @@ import {
|
||||
ExternalModuleDeclaration,
|
||||
InternalModuleDeclaration,
|
||||
MedusaModule,
|
||||
Modules,
|
||||
} from "@medusajs/modules-sdk"
|
||||
import { IEventBusService, IStockLocationService } from "@medusajs/types"
|
||||
import { StockLocationServiceInitializeOptions } from "../types"
|
||||
|
||||
export const initialize = async (
|
||||
options?: StockLocationServiceInitializeOptions | ExternalModuleDeclaration,
|
||||
options: StockLocationServiceInitializeOptions | ExternalModuleDeclaration,
|
||||
injectedDependencies?: {
|
||||
eventBusService: IEventBusService
|
||||
}
|
||||
): Promise<IStockLocationService> => {
|
||||
const serviceKey = "stockLocationService"
|
||||
const serviceKey = Modules.STOCK_LOCATION
|
||||
const loaded = await MedusaModule.bootstrap(
|
||||
serviceKey,
|
||||
"@medusajs/stock-location",
|
||||
|
||||
@@ -90,10 +90,10 @@ export type ModuleExports = {
|
||||
models?: Constructor<any>[]
|
||||
runMigrations?(
|
||||
options: LoaderOptions,
|
||||
moduleDeclaration: InternalModuleDeclaration
|
||||
moduleDeclaration?: InternalModuleDeclaration
|
||||
): Promise<void>
|
||||
revertMigration?(
|
||||
options: LoaderOptions,
|
||||
moduleDeclaration: InternalModuleDeclaration
|
||||
moduleDeclaration?: InternalModuleDeclaration
|
||||
): Promise<void>
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user