feat(medusa): Modules initializer (#3352)
This commit is contained in:
@@ -1,11 +1,10 @@
|
||||
import { ModuleExports } from "@medusajs/modules-sdk"
|
||||
import loadConnection from "./loaders/connection"
|
||||
|
||||
import migrations from "./migrations"
|
||||
import { revertMigration, runMigrations } from "./migrations/run-migration"
|
||||
import * as StockLocationModels from "./models"
|
||||
import StockLocationService from "./services/stock-location"
|
||||
|
||||
import { ModuleExports } from "@medusajs/modules-sdk"
|
||||
|
||||
const service = StockLocationService
|
||||
const loaders = [loadConnection]
|
||||
const models = Object.values(StockLocationModels)
|
||||
@@ -15,6 +14,8 @@ const moduleDefinition: ModuleExports = {
|
||||
migrations,
|
||||
loaders,
|
||||
models,
|
||||
runMigrations,
|
||||
revertMigration,
|
||||
}
|
||||
|
||||
export default moduleDefinition
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
import { IEventBusService, IStockLocationService } from "@medusajs/medusa"
|
||||
import {
|
||||
ExternalModuleDeclaration,
|
||||
InternalModuleDeclaration,
|
||||
MedusaModule,
|
||||
} from "@medusajs/modules-sdk"
|
||||
import { StockLocationServiceInitializeOptions } from "../types"
|
||||
|
||||
export const initialize = async (
|
||||
options?: StockLocationServiceInitializeOptions | ExternalModuleDeclaration,
|
||||
injectedDependencies?: {
|
||||
eventBusService: IEventBusService
|
||||
}
|
||||
): Promise<IStockLocationService> => {
|
||||
const serviceKey = "stockLocationService"
|
||||
const loaded = await MedusaModule.bootstrap(
|
||||
serviceKey,
|
||||
"@medusajs/stock-location",
|
||||
options as InternalModuleDeclaration | ExternalModuleDeclaration,
|
||||
injectedDependencies
|
||||
)
|
||||
|
||||
return loaded[serviceKey] as IStockLocationService
|
||||
}
|
||||
@@ -2,13 +2,13 @@ import {
|
||||
InternalModuleDeclaration,
|
||||
LoaderOptions,
|
||||
MODULE_RESOURCE_TYPE,
|
||||
MODULE_SCOPE,
|
||||
MODULE_SCOPE
|
||||
} from "@medusajs/modules-sdk"
|
||||
import { DataSource, DataSourceOptions } from "typeorm"
|
||||
|
||||
import * as StockLocationModels from "../models"
|
||||
import { MedusaError } from "medusa-core-utils"
|
||||
import { asValue } from "awilix"
|
||||
import { MedusaError } from "medusa-core-utils"
|
||||
import { DataSource, DataSourceOptions } from "typeorm"
|
||||
import * as StockLocationModels from "../models"
|
||||
import { StockLocationServiceInitializeOptions } from "../types"
|
||||
|
||||
export default async (
|
||||
{ options, container }: LoaderOptions,
|
||||
@@ -21,7 +21,8 @@ export default async (
|
||||
return
|
||||
}
|
||||
|
||||
const dbData = options?.database as Record<string, string>
|
||||
const dbData =
|
||||
options?.database as StockLocationServiceInitializeOptions["database"]
|
||||
|
||||
if (!dbData) {
|
||||
throw new MedusaError(
|
||||
@@ -32,13 +33,13 @@ export default async (
|
||||
|
||||
const entities = Object.values(StockLocationModels)
|
||||
const dataSource = new DataSource({
|
||||
type: dbData.database_type,
|
||||
url: dbData.database_url,
|
||||
database: dbData.database_database,
|
||||
extra: dbData.database_extra || {},
|
||||
schema: dbData.database_schema,
|
||||
type: dbData.type,
|
||||
url: dbData.url,
|
||||
database: dbData.database,
|
||||
extra: dbData.extra || {},
|
||||
schema: dbData.schema,
|
||||
entities,
|
||||
logging: dbData.database_logging,
|
||||
logging: dbData.logging,
|
||||
} as DataSourceOptions)
|
||||
|
||||
await dataSource.initialize()
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
import { InternalModuleDeclaration, LoaderOptions } from "@medusajs/modules-sdk"
|
||||
import { DataSource, DataSourceOptions } from "typeorm"
|
||||
import { StockLocationServiceInitializeOptions } from "../types"
|
||||
|
||||
import migrations from "./index"
|
||||
|
||||
function getDataSource(
|
||||
dbData: StockLocationServiceInitializeOptions["database"]
|
||||
): DataSource {
|
||||
return new DataSource({
|
||||
type: dbData!.type,
|
||||
url: dbData!.url,
|
||||
database: dbData!.database,
|
||||
extra: dbData!.extra || {},
|
||||
migrations: migrations
|
||||
.map((migration: any): Function[] => {
|
||||
return Object.values(migration).filter(
|
||||
(fn) => typeof fn === "function"
|
||||
) as Function[]
|
||||
})
|
||||
.flat(),
|
||||
schema: dbData!.schema,
|
||||
logging: dbData!.logging,
|
||||
} as DataSourceOptions)
|
||||
}
|
||||
|
||||
export async function runMigrations(
|
||||
{ options, logger }: Omit<LoaderOptions, "container">,
|
||||
moduleDeclaration?: InternalModuleDeclaration
|
||||
) {
|
||||
const dbData =
|
||||
options?.database as StockLocationServiceInitializeOptions["database"]
|
||||
|
||||
try {
|
||||
const dataSource = getDataSource(dbData)
|
||||
await dataSource.initialize()
|
||||
await dataSource.runMigrations()
|
||||
|
||||
logger?.info("Stock Location module migration executed")
|
||||
} catch (error) {
|
||||
logger?.error(
|
||||
`Stock Location module migration failed to run - Error: ${error}`
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export async function revertMigration(
|
||||
{ options, logger }: Omit<LoaderOptions, "container">,
|
||||
moduleDeclaration?: InternalModuleDeclaration
|
||||
) {
|
||||
const dbData =
|
||||
options?.database as StockLocationServiceInitializeOptions["database"]
|
||||
|
||||
try {
|
||||
const dataSource = getDataSource(dbData)
|
||||
await dataSource.initialize()
|
||||
await dataSource.undoLastMigration()
|
||||
|
||||
logger?.info("Stock Location module migration reverted")
|
||||
} catch (error) {
|
||||
logger?.error(
|
||||
`Stock Location module migration failed to revert - Error: ${error}`
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -153,11 +153,9 @@ export default class StockLocationService {
|
||||
}
|
||||
const result = await locationRepo.save(loc)
|
||||
|
||||
await this.eventBusService_
|
||||
.withTransaction(manager)
|
||||
.emit(StockLocationService.Events.CREATED, {
|
||||
id: result.id,
|
||||
})
|
||||
await this.eventBusService_?.emit?.(StockLocationService.Events.CREATED, {
|
||||
id: result.id,
|
||||
})
|
||||
|
||||
return result
|
||||
}
|
||||
@@ -201,11 +199,9 @@ export default class StockLocationService {
|
||||
|
||||
await locationRepo.save(toSave)
|
||||
|
||||
await this.eventBusService_
|
||||
.withTransaction(manager)
|
||||
.emit(StockLocationService.Events.UPDATED, {
|
||||
id: stockLocationId,
|
||||
})
|
||||
await this.eventBusService_?.emit?.(StockLocationService.Events.UPDATED, {
|
||||
id: stockLocationId,
|
||||
})
|
||||
|
||||
return item
|
||||
}
|
||||
@@ -267,10 +263,8 @@ export default class StockLocationService {
|
||||
|
||||
await locationRepo.softRemove({ id })
|
||||
|
||||
await this.eventBusService_
|
||||
.withTransaction(manager)
|
||||
.emit(StockLocationService.Events.DELETED, {
|
||||
id,
|
||||
})
|
||||
await this.eventBusService_?.emit?.(StockLocationService.Events.DELETED, {
|
||||
id,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
import { DatabaseType } from "typeorm"
|
||||
|
||||
export type StockLocationServiceInitializeOptions = {
|
||||
database?: {
|
||||
type?: DatabaseType | string
|
||||
url?: string
|
||||
database?: string
|
||||
extra?: Record<string, any>
|
||||
schema?: string
|
||||
logging?: boolean
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user