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:
1 parent
6f85a3d366
commit
bd12a95083
17 files changed
+133
-25
No files matched your search
@@ -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 { ModuleExports } from "@medusajs/modules-sdk"
|
||||||
|
|
||||||
import InMemoryCacheService from "./services/inmemory-cache"
|
import InMemoryCacheService from "./services/inmemory-cache"
|
||||||
|
|
||||||
const service = InMemoryCacheService
|
const service = InMemoryCacheService
|
||||||
@@ -9,3 +8,5 @@ const moduleDefinition: ModuleExports = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export default moduleDefinition
|
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 { ModuleExports } from "@medusajs/modules-sdk"
|
||||||
|
|
||||||
import Loader from "./loaders"
|
import Loader from "./loaders"
|
||||||
import { RedisCacheService } from "./services"
|
import { RedisCacheService } from "./services"
|
||||||
|
|
||||||
@@ -12,3 +11,5 @@ const moduleDefinition: ModuleExports = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export default moduleDefinition
|
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 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 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 { InternalModuleDeclaration, Logger } from "@medusajs/modules-sdk"
|
||||||
import { ConfigModule, EmitData } from "@medusajs/types"
|
import { EmitData } from "@medusajs/types"
|
||||||
import { AbstractEventBusModuleService } from "@medusajs/utils"
|
import { AbstractEventBusModuleService } from "@medusajs/utils"
|
||||||
import { BulkJobOptions, JobsOptions, Queue, Worker } from "bullmq"
|
import { BulkJobOptions, JobsOptions, Queue, Worker } from "bullmq"
|
||||||
import { Redis } from "ioredis"
|
import { Redis } from "ioredis"
|
||||||
@@ -7,7 +7,6 @@ import { BullJob, EmitOptions, EventBusRedisModuleOptions } from "../types"
|
|||||||
|
|
||||||
type InjectedDependencies = {
|
type InjectedDependencies = {
|
||||||
logger: Logger
|
logger: Logger
|
||||||
configModule: ConfigModule
|
|
||||||
eventBusRedisConnection: Redis
|
eventBusRedisConnection: Redis
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -17,7 +16,6 @@ type InjectedDependencies = {
|
|||||||
*/
|
*/
|
||||||
// eslint-disable-next-line max-len
|
// eslint-disable-next-line max-len
|
||||||
export default class RedisEventBusService extends AbstractEventBusModuleService {
|
export default class RedisEventBusService extends AbstractEventBusModuleService {
|
||||||
protected readonly config_: ConfigModule
|
|
||||||
protected readonly logger_: Logger
|
protected readonly logger_: Logger
|
||||||
protected readonly moduleOptions_: EventBusRedisModuleOptions
|
protected readonly moduleOptions_: EventBusRedisModuleOptions
|
||||||
// eslint-disable-next-line max-len
|
// eslint-disable-next-line max-len
|
||||||
@@ -26,7 +24,7 @@ export default class RedisEventBusService extends AbstractEventBusModuleService
|
|||||||
protected queue_: Queue
|
protected queue_: Queue
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
{ configModule, logger, eventBusRedisConnection }: InjectedDependencies,
|
{ logger, eventBusRedisConnection }: InjectedDependencies,
|
||||||
moduleOptions: EventBusRedisModuleOptions = {},
|
moduleOptions: EventBusRedisModuleOptions = {},
|
||||||
moduleDeclaration: InternalModuleDeclaration
|
moduleDeclaration: InternalModuleDeclaration
|
||||||
) {
|
) {
|
||||||
@@ -35,7 +33,6 @@ export default class RedisEventBusService extends AbstractEventBusModuleService
|
|||||||
super(...arguments)
|
super(...arguments)
|
||||||
|
|
||||||
this.moduleOptions_ = moduleOptions
|
this.moduleOptions_ = moduleOptions
|
||||||
this.config_ = configModule
|
|
||||||
this.logger_ = logger
|
this.logger_ = logger
|
||||||
|
|
||||||
this.queue_ = new Queue(moduleOptions.queueName ?? `events-queue`, {
|
this.queue_ = new Queue(moduleOptions.queueName ?? `events-queue`, {
|
||||||
|
|||||||
@@ -22,6 +22,6 @@ const moduleDefinition: ModuleExports = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export default moduleDefinition
|
export default moduleDefinition
|
||||||
|
|
||||||
export * from "./initialize"
|
export * from "./initialize"
|
||||||
|
export { revertMigration, runMigrations } from "./migrations/run-migration"
|
||||||
export * from "./types"
|
export * from "./types"
|
||||||
@@ -2,17 +2,18 @@ import {
|
|||||||
ExternalModuleDeclaration,
|
ExternalModuleDeclaration,
|
||||||
InternalModuleDeclaration,
|
InternalModuleDeclaration,
|
||||||
MedusaModule,
|
MedusaModule,
|
||||||
|
Modules,
|
||||||
} from "@medusajs/modules-sdk"
|
} from "@medusajs/modules-sdk"
|
||||||
import { IEventBusService, IInventoryService } from "@medusajs/types"
|
import { IEventBusService, IInventoryService } from "@medusajs/types"
|
||||||
import { InventoryServiceInitializeOptions } from "../types"
|
import { InventoryServiceInitializeOptions } from "../types"
|
||||||
|
|
||||||
export const initialize = async (
|
export const initialize = async (
|
||||||
options?: InventoryServiceInitializeOptions | ExternalModuleDeclaration,
|
options: InventoryServiceInitializeOptions | ExternalModuleDeclaration,
|
||||||
injectedDependencies?: {
|
injectedDependencies?: {
|
||||||
eventBusService: IEventBusService
|
eventBusService: IEventBusService
|
||||||
}
|
}
|
||||||
): Promise<IInventoryService> => {
|
): Promise<IInventoryService> => {
|
||||||
const serviceKey = "inventoryService"
|
const serviceKey = Modules.INVENTORY
|
||||||
const loaded = await MedusaModule.bootstrap(
|
const loaded = await MedusaModule.bootstrap(
|
||||||
serviceKey,
|
serviceKey,
|
||||||
"@medusajs/inventory",
|
"@medusajs/inventory",
|
||||||
|
|||||||
@@ -4,21 +4,29 @@ import {
|
|||||||
MODULE_SCOPE,
|
MODULE_SCOPE,
|
||||||
} from "@medusajs/types"
|
} from "@medusajs/types"
|
||||||
|
|
||||||
export const MODULE_DEFINITIONS: ModuleDefinition[] = [
|
export enum Modules {
|
||||||
{
|
EVENT_BUS = "eventBus",
|
||||||
key: "eventBus",
|
STOCK_LOCATION = "stockLocationService",
|
||||||
|
INVENTORY = "inventoryService",
|
||||||
|
CACHE = "cacheService",
|
||||||
|
}
|
||||||
|
|
||||||
|
export const ModulesDefinition: { [key: string]: ModuleDefinition } = {
|
||||||
|
[Modules.EVENT_BUS]: {
|
||||||
|
key: Modules.EVENT_BUS,
|
||||||
registrationName: "eventBusModuleService",
|
registrationName: "eventBusModuleService",
|
||||||
defaultPackage: "@medusajs/event-bus-local",
|
defaultPackage: "@medusajs/event-bus-local",
|
||||||
label: "EventBusModuleService",
|
label: "EventBusModuleService",
|
||||||
canOverride: true,
|
canOverride: true,
|
||||||
isRequired: true,
|
isRequired: true,
|
||||||
|
dependencies: ["logger"],
|
||||||
defaultModuleDeclaration: {
|
defaultModuleDeclaration: {
|
||||||
scope: MODULE_SCOPE.INTERNAL,
|
scope: MODULE_SCOPE.INTERNAL,
|
||||||
resources: MODULE_RESOURCE_TYPE.SHARED,
|
resources: MODULE_RESOURCE_TYPE.SHARED,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
[Modules.STOCK_LOCATION]: {
|
||||||
key: "stockLocationService",
|
key: Modules.STOCK_LOCATION,
|
||||||
registrationName: "stockLocationService",
|
registrationName: "stockLocationService",
|
||||||
defaultPackage: false,
|
defaultPackage: false,
|
||||||
label: "StockLocationService",
|
label: "StockLocationService",
|
||||||
@@ -30,8 +38,8 @@ export const MODULE_DEFINITIONS: ModuleDefinition[] = [
|
|||||||
resources: MODULE_RESOURCE_TYPE.SHARED,
|
resources: MODULE_RESOURCE_TYPE.SHARED,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
[Modules.INVENTORY]: {
|
||||||
key: "inventoryService",
|
key: Modules.INVENTORY,
|
||||||
registrationName: "inventoryService",
|
registrationName: "inventoryService",
|
||||||
defaultPackage: false,
|
defaultPackage: false,
|
||||||
label: "InventoryService",
|
label: "InventoryService",
|
||||||
@@ -43,8 +51,8 @@ export const MODULE_DEFINITIONS: ModuleDefinition[] = [
|
|||||||
resources: MODULE_RESOURCE_TYPE.SHARED,
|
resources: MODULE_RESOURCE_TYPE.SHARED,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
[Modules.CACHE]: {
|
||||||
key: "cacheService",
|
key: Modules.CACHE,
|
||||||
registrationName: "cacheService",
|
registrationName: "cacheService",
|
||||||
defaultPackage: "@medusajs/cache-inmemory",
|
defaultPackage: "@medusajs/cache-inmemory",
|
||||||
label: "CacheService",
|
label: "CacheService",
|
||||||
@@ -55,6 +63,9 @@ export const MODULE_DEFINITIONS: ModuleDefinition[] = [
|
|||||||
resources: MODULE_RESOURCE_TYPE.SHARED,
|
resources: MODULE_RESOURCE_TYPE.SHARED,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
]
|
}
|
||||||
|
|
||||||
|
export const MODULE_DEFINITIONS: ModuleDefinition[] =
|
||||||
|
Object.values(ModulesDefinition)
|
||||||
|
|
||||||
export default MODULE_DEFINITIONS
|
export default MODULE_DEFINITIONS
|
||||||
@@ -50,9 +50,10 @@ export class MedusaModule {
|
|||||||
const services = {}
|
const services = {}
|
||||||
|
|
||||||
for (const resolution of Object.values(moduleResolutions)) {
|
for (const resolution of Object.values(moduleResolutions)) {
|
||||||
|
const keyName = resolution.definition.key
|
||||||
const registrationName = resolution.definition.registrationName
|
const registrationName = resolution.definition.registrationName
|
||||||
|
|
||||||
services[registrationName] = container.resolve(registrationName)
|
services[keyName] = container.resolve(registrationName)
|
||||||
}
|
}
|
||||||
|
|
||||||
return services
|
return services
|
||||||
|
|||||||
@@ -19,3 +19,6 @@ const moduleDefinition: ModuleExports = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export default moduleDefinition
|
export default moduleDefinition
|
||||||
|
export * from "./initialize"
|
||||||
|
export { revertMigration, runMigrations } from "./migrations/run-migration"
|
||||||
|
export * from "./types"
|
||||||
@@ -2,17 +2,18 @@ import {
|
|||||||
ExternalModuleDeclaration,
|
ExternalModuleDeclaration,
|
||||||
InternalModuleDeclaration,
|
InternalModuleDeclaration,
|
||||||
MedusaModule,
|
MedusaModule,
|
||||||
|
Modules,
|
||||||
} from "@medusajs/modules-sdk"
|
} from "@medusajs/modules-sdk"
|
||||||
import { IEventBusService, IStockLocationService } from "@medusajs/types"
|
import { IEventBusService, IStockLocationService } from "@medusajs/types"
|
||||||
import { StockLocationServiceInitializeOptions } from "../types"
|
import { StockLocationServiceInitializeOptions } from "../types"
|
||||||
|
|
||||||
export const initialize = async (
|
export const initialize = async (
|
||||||
options?: StockLocationServiceInitializeOptions | ExternalModuleDeclaration,
|
options: StockLocationServiceInitializeOptions | ExternalModuleDeclaration,
|
||||||
injectedDependencies?: {
|
injectedDependencies?: {
|
||||||
eventBusService: IEventBusService
|
eventBusService: IEventBusService
|
||||||
}
|
}
|
||||||
): Promise<IStockLocationService> => {
|
): Promise<IStockLocationService> => {
|
||||||
const serviceKey = "stockLocationService"
|
const serviceKey = Modules.STOCK_LOCATION
|
||||||
const loaded = await MedusaModule.bootstrap(
|
const loaded = await MedusaModule.bootstrap(
|
||||||
serviceKey,
|
serviceKey,
|
||||||
"@medusajs/stock-location",
|
"@medusajs/stock-location",
|
||||||
|
|||||||
@@ -90,10 +90,10 @@ export type ModuleExports = {
|
|||||||
models?: Constructor<any>[]
|
models?: Constructor<any>[]
|
||||||
runMigrations?(
|
runMigrations?(
|
||||||
options: LoaderOptions,
|
options: LoaderOptions,
|
||||||
moduleDeclaration: InternalModuleDeclaration
|
moduleDeclaration?: InternalModuleDeclaration
|
||||||
): Promise<void>
|
): Promise<void>
|
||||||
revertMigration?(
|
revertMigration?(
|
||||||
options: LoaderOptions,
|
options: LoaderOptions,
|
||||||
moduleDeclaration: InternalModuleDeclaration
|
moduleDeclaration?: InternalModuleDeclaration
|
||||||
): Promise<void>
|
): Promise<void>
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user