feat(medusa, stock-location, inventory): Allow modules to integrate with core (#2997)
* feat: module shared resources
This commit is contained in:
@@ -1,3 +1,6 @@
|
||||
import { EntityManager } from "typeorm"
|
||||
|
||||
export interface IEventBusService {
|
||||
emit(event: string, data: any): Promise<void>
|
||||
withTransaction(transactionManager?: EntityManager): this
|
||||
}
|
||||
|
||||
@@ -2,6 +2,12 @@ const loader = ({}) => {
|
||||
throw new Error("loader")
|
||||
}
|
||||
|
||||
export const service = class TestService {}
|
||||
export const migrations = []
|
||||
export const loaders = [loader]
|
||||
const service = class TestService {}
|
||||
const migrations = []
|
||||
const loaders = [loader]
|
||||
|
||||
export default {
|
||||
service,
|
||||
migrations,
|
||||
loaders,
|
||||
}
|
||||
|
||||
@@ -1,3 +1,11 @@
|
||||
export const service = class TestService {}
|
||||
export const migrations = []
|
||||
export const loaders = []
|
||||
const service = class TestService {}
|
||||
const migrations = []
|
||||
const loaders = []
|
||||
const models = []
|
||||
|
||||
export default {
|
||||
service,
|
||||
migrations,
|
||||
loaders,
|
||||
models,
|
||||
}
|
||||
|
||||
@@ -1,2 +1,7 @@
|
||||
export const migrations = []
|
||||
export const loaders = []
|
||||
const migrations = []
|
||||
const loaders = []
|
||||
|
||||
export default {
|
||||
migrations,
|
||||
loaders,
|
||||
}
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
// import resolveCwd from "resolve-cwd"
|
||||
import { ConfigModule } from "../../types/global"
|
||||
import {
|
||||
ConfigModule,
|
||||
ModuleDefinition,
|
||||
MODULE_RESOURCE_TYPE,
|
||||
MODULE_SCOPE,
|
||||
} from "../../types/global"
|
||||
import ModuleDefinitionLoader from "../module-definitions"
|
||||
import MODULE_DEFINITIONS from "../module-definitions/definitions"
|
||||
|
||||
@@ -7,13 +11,17 @@ const RESOLVED_PACKAGE = "@medusajs/test-service-resolved"
|
||||
jest.mock("resolve-cwd", () => jest.fn(() => RESOLVED_PACKAGE))
|
||||
|
||||
describe("module definitions loader", () => {
|
||||
const defaultDefinition = {
|
||||
const defaultDefinition: ModuleDefinition = {
|
||||
key: "testService",
|
||||
registrationName: "testService",
|
||||
defaultPackage: "@medusajs/test-service",
|
||||
label: "TestService",
|
||||
isRequired: false,
|
||||
canOverride: true,
|
||||
defaultModuleDeclaration: {
|
||||
scope: MODULE_SCOPE.INTERNAL,
|
||||
resources: MODULE_RESOURCE_TYPE.SHARED,
|
||||
},
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
@@ -33,6 +41,10 @@ describe("module definitions loader", () => {
|
||||
resolutionPath: defaultDefinition.defaultPackage,
|
||||
definition: defaultDefinition,
|
||||
options: {},
|
||||
moduleDeclaration: {
|
||||
scope: "internal",
|
||||
resources: "shared",
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
@@ -82,6 +94,10 @@ describe("module definitions loader", () => {
|
||||
resolutionPath: false,
|
||||
definition: definition,
|
||||
options: {},
|
||||
moduleDeclaration: {
|
||||
scope: "internal",
|
||||
resources: "shared",
|
||||
},
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -100,6 +116,10 @@ describe("module definitions loader", () => {
|
||||
resolutionPath: RESOLVED_PACKAGE,
|
||||
definition: defaultDefinition,
|
||||
options: {},
|
||||
moduleDeclaration: {
|
||||
scope: "internal",
|
||||
resources: "shared",
|
||||
},
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -112,6 +132,7 @@ describe("module definitions loader", () => {
|
||||
modules: {
|
||||
[defaultDefinition.key]: {
|
||||
resolve: defaultDefinition.defaultPackage,
|
||||
resources: MODULE_RESOURCE_TYPE.ISOLATED,
|
||||
},
|
||||
},
|
||||
} as ConfigModule)
|
||||
@@ -120,6 +141,11 @@ describe("module definitions loader", () => {
|
||||
resolutionPath: RESOLVED_PACKAGE,
|
||||
definition: defaultDefinition,
|
||||
options: {},
|
||||
moduleDeclaration: {
|
||||
scope: "internal",
|
||||
resources: "isolated",
|
||||
resolve: defaultDefinition.defaultPackage,
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
@@ -138,6 +164,11 @@ describe("module definitions loader", () => {
|
||||
resolutionPath: defaultDefinition.defaultPackage,
|
||||
definition: defaultDefinition,
|
||||
options: { test: 123 },
|
||||
moduleDeclaration: {
|
||||
scope: "internal",
|
||||
resources: "shared",
|
||||
options: { test: 123 },
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
@@ -149,6 +180,8 @@ describe("module definitions loader", () => {
|
||||
[defaultDefinition.key]: {
|
||||
resolve: defaultDefinition.defaultPackage,
|
||||
options: { test: 123 },
|
||||
scope: "internal",
|
||||
resources: "isolated",
|
||||
},
|
||||
},
|
||||
} as unknown as ConfigModule)
|
||||
@@ -157,6 +190,12 @@ describe("module definitions loader", () => {
|
||||
resolutionPath: RESOLVED_PACKAGE,
|
||||
definition: defaultDefinition,
|
||||
options: { test: 123 },
|
||||
moduleDeclaration: {
|
||||
scope: "internal",
|
||||
resources: "isolated",
|
||||
resolve: defaultDefinition.defaultPackage,
|
||||
options: { test: 123 },
|
||||
},
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -6,13 +6,13 @@ import {
|
||||
createContainer,
|
||||
Resolver,
|
||||
} from "awilix"
|
||||
import { mkdirSync, rmSync, writeFileSync } from "fs"
|
||||
import Logger from "../logger"
|
||||
import { resolve } from "path"
|
||||
import {
|
||||
ConfigModule,
|
||||
MedusaContainer,
|
||||
ModuleResolution,
|
||||
MODULE_RESOURCE_TYPE,
|
||||
MODULE_SCOPE,
|
||||
} from "../../types/global"
|
||||
import registerModules from "../module"
|
||||
import { trackInstallation } from "../__mocks__/medusa-telemetry"
|
||||
@@ -90,6 +90,10 @@ describe("modules loader", () => {
|
||||
key: "testService",
|
||||
defaultPackage: "testService",
|
||||
label: "TestService",
|
||||
defaultModuleDeclaration: {
|
||||
scope: MODULE_SCOPE.INTERNAL,
|
||||
resources: MODULE_RESOURCE_TYPE.SHARED,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
@@ -114,6 +118,10 @@ describe("modules loader", () => {
|
||||
key: "testService",
|
||||
defaultPackage: "testService",
|
||||
label: "TestService",
|
||||
defaultModuleDeclaration: {
|
||||
scope: MODULE_SCOPE.INTERNAL,
|
||||
resources: MODULE_RESOURCE_TYPE.SHARED,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
@@ -149,6 +157,10 @@ describe("modules loader", () => {
|
||||
key: "testService",
|
||||
defaultPackage: "testService",
|
||||
label: "TestService",
|
||||
defaultModuleDeclaration: {
|
||||
scope: MODULE_SCOPE.INTERNAL,
|
||||
resources: MODULE_RESOURCE_TYPE.SHARED,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
@@ -177,6 +189,10 @@ describe("modules loader", () => {
|
||||
key: "testService",
|
||||
defaultPackage: "testService",
|
||||
label: "TestService",
|
||||
defaultModuleDeclaration: {
|
||||
scope: MODULE_SCOPE.INTERNAL,
|
||||
resources: MODULE_RESOURCE_TYPE.SHARED,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
@@ -207,6 +223,10 @@ describe("modules loader", () => {
|
||||
defaultPackage: "testService",
|
||||
label: "TestService",
|
||||
isRequired: true,
|
||||
defaultModuleDeclaration: {
|
||||
scope: MODULE_SCOPE.INTERNAL,
|
||||
resources: MODULE_RESOURCE_TYPE.SHARED,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
@@ -115,17 +115,6 @@ export default async ({
|
||||
const rAct = Logger.success(repoActivity, "Repositories initialized") || {}
|
||||
track("REPOSITORIES_INIT_COMPLETED", { duration: rAct.duration })
|
||||
|
||||
const dbActivity = Logger.activity(`Initializing database${EOL}`)
|
||||
track("DATABASE_INIT_STARTED")
|
||||
const dbConnection = await databaseLoader({
|
||||
container,
|
||||
configModule,
|
||||
})
|
||||
const dbAct = Logger.success(dbActivity, "Database initialized") || {}
|
||||
track("DATABASE_INIT_COMPLETED", { duration: dbAct.duration })
|
||||
|
||||
container.register({ manager: asValue(dbConnection.manager) })
|
||||
|
||||
const stratActivity = Logger.activity(`Initializing strategies${EOL}`)
|
||||
track("STRATEGIES_INIT_STARTED")
|
||||
strategiesLoader({ container, configModule, isTest })
|
||||
@@ -138,6 +127,17 @@ export default async ({
|
||||
const modAct = Logger.success(modulesActivity, "Modules initialized") || {}
|
||||
track("MODULES_INIT_COMPLETED", { duration: modAct.duration })
|
||||
|
||||
const dbActivity = Logger.activity(`Initializing database${EOL}`)
|
||||
track("DATABASE_INIT_STARTED")
|
||||
const dbConnection = await databaseLoader({
|
||||
container,
|
||||
configModule,
|
||||
})
|
||||
const dbAct = Logger.success(dbActivity, "Database initialized") || {}
|
||||
track("DATABASE_INIT_COMPLETED", { duration: dbAct.duration })
|
||||
|
||||
container.register({ manager: asValue(dbConnection.manager) })
|
||||
|
||||
const servicesActivity = Logger.activity(`Initializing services${EOL}`)
|
||||
track("SERVICES_INIT_STARTED")
|
||||
servicesLoader({ container, configModule, isTest })
|
||||
|
||||
@@ -1,4 +1,8 @@
|
||||
import { ModuleDefinition } from "../../types/global"
|
||||
import {
|
||||
ModuleDefinition,
|
||||
MODULE_RESOURCE_TYPE,
|
||||
MODULE_SCOPE,
|
||||
} from "../../types/global"
|
||||
|
||||
export const MODULE_DEFINITIONS: ModuleDefinition[] = [
|
||||
{
|
||||
@@ -8,6 +12,10 @@ export const MODULE_DEFINITIONS: ModuleDefinition[] = [
|
||||
label: "StockLocationService",
|
||||
isRequired: false,
|
||||
canOverride: true,
|
||||
defaultModuleDeclaration: {
|
||||
scope: MODULE_SCOPE.INTERNAL,
|
||||
resources: MODULE_RESOURCE_TYPE.SHARED,
|
||||
},
|
||||
},
|
||||
{
|
||||
key: "inventoryService",
|
||||
@@ -16,6 +24,10 @@ export const MODULE_DEFINITIONS: ModuleDefinition[] = [
|
||||
label: "InventoryService",
|
||||
isRequired: false,
|
||||
canOverride: true,
|
||||
defaultModuleDeclaration: {
|
||||
scope: MODULE_SCOPE.INTERNAL,
|
||||
resources: MODULE_RESOURCE_TYPE.SHARED,
|
||||
},
|
||||
},
|
||||
]
|
||||
|
||||
|
||||
@@ -40,9 +40,16 @@ export default ({ modules }: ConfigModule) => {
|
||||
)
|
||||
}
|
||||
|
||||
const moduleDeclaration =
|
||||
typeof moduleConfiguration === "object" ? moduleConfiguration : {}
|
||||
|
||||
moduleResolutions[definition.key] = {
|
||||
resolutionPath,
|
||||
definition,
|
||||
moduleDeclaration: {
|
||||
...definition.defaultModuleDeclaration,
|
||||
...moduleDeclaration,
|
||||
},
|
||||
options:
|
||||
typeof moduleConfiguration === "object"
|
||||
? moduleConfiguration.options ?? {}
|
||||
|
||||
@@ -1,21 +1,26 @@
|
||||
import { asFunction, asValue } from "awilix"
|
||||
import { asClass, asFunction, asValue } from "awilix"
|
||||
import { trackInstallation } from "medusa-telemetry"
|
||||
import { ConfigModule, Logger, MedusaContainer } from "../types/global"
|
||||
import { EntitySchema } from "typeorm"
|
||||
import {
|
||||
ClassConstructor,
|
||||
ConfigModule,
|
||||
LoaderOptions,
|
||||
Logger,
|
||||
MedusaContainer,
|
||||
ModuleExports,
|
||||
ModuleResolution,
|
||||
MODULE_RESOURCE_TYPE,
|
||||
MODULE_SCOPE,
|
||||
} from "../types/global"
|
||||
import { ModulesHelper } from "../utils/module-helper"
|
||||
|
||||
type Options = {
|
||||
container: MedusaContainer
|
||||
configModule: ConfigModule
|
||||
logger: Logger
|
||||
}
|
||||
|
||||
export const moduleHelper = new ModulesHelper()
|
||||
|
||||
const registerModule = async (
|
||||
container,
|
||||
resolution,
|
||||
configModule,
|
||||
logger
|
||||
container: MedusaContainer,
|
||||
resolution: ModuleResolution,
|
||||
configModule: ConfigModule,
|
||||
logger: Logger
|
||||
): Promise<{ error?: Error } | void> => {
|
||||
if (!resolution.resolutionPath) {
|
||||
container.register({
|
||||
@@ -25,9 +30,9 @@ const registerModule = async (
|
||||
return
|
||||
}
|
||||
|
||||
let loadedModule
|
||||
let loadedModule: ModuleExports
|
||||
try {
|
||||
loadedModule = await import(resolution.resolutionPath!)
|
||||
loadedModule = (await import(resolution.resolutionPath!)).default
|
||||
} catch (error) {
|
||||
return { error }
|
||||
}
|
||||
@@ -42,15 +47,41 @@ const registerModule = async (
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
resolution.moduleDeclaration?.scope === MODULE_SCOPE.INTERNAL &&
|
||||
resolution.moduleDeclaration?.resources === MODULE_RESOURCE_TYPE.SHARED
|
||||
) {
|
||||
const moduleModels = loadedModule?.models || null
|
||||
if (moduleModels) {
|
||||
moduleModels.map((val: ClassConstructor<unknown>) => {
|
||||
container.registerAdd("db_entities", asValue(val))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: "cradle" should only contain dependent Modules and the EntityManager if module scope is shared
|
||||
container.register({
|
||||
[resolution.definition.registrationName]: asFunction((cradle) => {
|
||||
return new moduleService(
|
||||
cradle,
|
||||
resolution.options,
|
||||
resolution.moduleDeclaration
|
||||
)
|
||||
}).singleton(),
|
||||
})
|
||||
|
||||
const moduleLoaders = loadedModule?.loaders || []
|
||||
try {
|
||||
for (const loader of moduleLoaders) {
|
||||
await loader({
|
||||
container,
|
||||
configModule,
|
||||
logger,
|
||||
options: resolution.options,
|
||||
})
|
||||
await loader(
|
||||
{
|
||||
container,
|
||||
configModule,
|
||||
logger,
|
||||
options: resolution.options,
|
||||
},
|
||||
resolution.moduleDeclaration
|
||||
)
|
||||
}
|
||||
} catch (err) {
|
||||
return {
|
||||
@@ -60,12 +91,6 @@ const registerModule = async (
|
||||
}
|
||||
}
|
||||
|
||||
container.register({
|
||||
[resolution.definition.registrationName]: asFunction(
|
||||
(cradle) => new moduleService(cradle, resolution.options)
|
||||
).singleton(),
|
||||
})
|
||||
|
||||
trackInstallation(
|
||||
{
|
||||
module: resolution.definition.key,
|
||||
@@ -79,7 +104,7 @@ export default async ({
|
||||
container,
|
||||
configModule,
|
||||
logger,
|
||||
}: Options): Promise<void> => {
|
||||
}: LoaderOptions): Promise<void> => {
|
||||
const moduleResolutions = configModule?.moduleResolutions ?? {}
|
||||
|
||||
for (const resolution of Object.values(moduleResolutions)) {
|
||||
@@ -87,18 +112,18 @@ export default async ({
|
||||
container,
|
||||
resolution,
|
||||
configModule,
|
||||
logger
|
||||
logger!
|
||||
)
|
||||
if (registrationResult?.error) {
|
||||
const { error } = registrationResult
|
||||
if (resolution.definition.isRequired) {
|
||||
logger.warn(
|
||||
logger?.warn(
|
||||
`Could not resolve required module: ${resolution.definition.label}. Error: ${error.message}`
|
||||
)
|
||||
throw error
|
||||
}
|
||||
|
||||
logger.warn(
|
||||
logger?.warn(
|
||||
`Could not resolve module: ${resolution.definition.label}. Error: ${error.message}`
|
||||
)
|
||||
}
|
||||
|
||||
@@ -37,10 +37,38 @@ export type Logger = _Logger & {
|
||||
warn: (msg: string) => void
|
||||
}
|
||||
|
||||
export enum MODULE_SCOPE {
|
||||
INTERNAL = "internal",
|
||||
EXTERNAL = "external",
|
||||
}
|
||||
|
||||
export enum MODULE_RESOURCE_TYPE {
|
||||
SHARED = "shared",
|
||||
ISOLATED = "isolated",
|
||||
}
|
||||
|
||||
export type ConfigurableModuleDeclaration = {
|
||||
scope: MODULE_SCOPE.INTERNAL
|
||||
resources: MODULE_RESOURCE_TYPE
|
||||
resolve?: string
|
||||
options?: Record<string, unknown>
|
||||
}
|
||||
/*
|
||||
| {
|
||||
scope: MODULE_SCOPE.external
|
||||
server: {
|
||||
type: "built-in" | "rest" | "tsrpc" | "grpc" | "gql"
|
||||
url: string
|
||||
options?: Record<string, unknown>
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
export type ModuleResolution = {
|
||||
resolutionPath: string | false
|
||||
definition: ModuleDefinition
|
||||
options?: Record<string, unknown>
|
||||
moduleDeclaration?: ConfigurableModuleDeclaration
|
||||
}
|
||||
|
||||
export type ModuleDefinition = {
|
||||
@@ -50,11 +78,26 @@ export type ModuleDefinition = {
|
||||
label: string
|
||||
canOverride?: boolean
|
||||
isRequired?: boolean
|
||||
defaultModuleDeclaration: ConfigurableModuleDeclaration
|
||||
}
|
||||
|
||||
export type ConfigurableModuleDeclaration = {
|
||||
resolve?: string
|
||||
export type LoaderOptions = {
|
||||
container: MedusaContainer
|
||||
configModule: ConfigModule
|
||||
options?: Record<string, unknown>
|
||||
logger?: Logger
|
||||
}
|
||||
|
||||
export type Constructor<T> = new (...args: any[]) => T
|
||||
|
||||
export type ModuleExports = {
|
||||
loaders: ((
|
||||
options: LoaderOptions,
|
||||
moduleDeclaration?: ConfigurableModuleDeclaration
|
||||
) => Promise<void>)[]
|
||||
service: Constructor<any>
|
||||
migrations?: any[] // TODO: revisit migrations type
|
||||
models?: Constructor<any>[]
|
||||
}
|
||||
|
||||
export type ConfigModule = {
|
||||
@@ -77,7 +120,10 @@ export type ConfigModule = {
|
||||
admin_cors?: string
|
||||
}
|
||||
featureFlags: Record<string, boolean | string>
|
||||
modules?: Record<string, false | string | ConfigurableModuleDeclaration>
|
||||
modules?: Record<
|
||||
string,
|
||||
false | string | Partial<ConfigurableModuleDeclaration>
|
||||
>
|
||||
moduleResolutions?: Record<string, ModuleResolution>
|
||||
plugins: (
|
||||
| {
|
||||
|
||||
Reference in New Issue
Block a user