Skip to content

Commit

Permalink
fix(modules-sdk): default db options
Browse files Browse the repository at this point in the history
  • Loading branch information
carlos-r-l-rodrigues committed Sep 25, 2024
1 parent 3533aa8 commit 3505033
Showing 1 changed file with 49 additions and 18 deletions.
67 changes: 49 additions & 18 deletions packages/core/modules-sdk/src/medusa-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ import {
} from "@medusajs/types"
import {
ContainerRegistrationKeys,
createMedusaContainer,
dynamicImport,
GraphQLUtils,
isObject,
isString,
MedusaError,
Modules,
ModulesSdkUtils,
createMedusaContainer,
dynamicImport,
isObject,
isString,
promiseAll,
} from "@medusajs/utils"
import type { Knex } from "@mikro-orm/knex"
Expand All @@ -37,7 +37,7 @@ import {
RegisterModuleJoinerConfig,
} from "./medusa-module"
import { RemoteLink } from "./remote-link"
import { createQuery, RemoteQuery } from "./remote-query"
import { RemoteQuery, createQuery } from "./remote-query"
import { MODULE_RESOURCE_TYPE, MODULE_SCOPE } from "./types"

const LinkModulePackage = MODULE_PACKAGE_NAMES[Modules.LINK]
Expand Down Expand Up @@ -85,13 +85,23 @@ export type SharedResources = {
}
}

export async function loadModules(
modulesConfig,
sharedContainer,
migrationOnly = false,
loaderOnly = false,
workerMode: "shared" | "worker" | "server" = "server"
) {
export async function loadModules(args: {
modulesConfig: MedusaModuleConfig
sharedContainer: MedusaContainer
sharedResourcesConfig?: SharedResources
migrationOnly?: boolean
loaderOnly?: boolean
workerMode?: "shared" | "worker" | "server"
}) {
const {
modulesConfig,
sharedContainer,
sharedResourcesConfig,
migrationOnly = false,
loaderOnly = false,
workerMode = "server",
} = args

const allModules = {}

await Promise.all(
Expand Down Expand Up @@ -128,6 +138,18 @@ export async function loadModules(
declaration.resources = MODULE_RESOURCE_TYPE.SHARED
}

if (
declaration.scope === MODULE_SCOPE.INTERNAL &&
declaration.resources === MODULE_RESOURCE_TYPE.SHARED
) {
declaration.options ??= {}
declaration.options.database ??= {
...sharedResourcesConfig?.database,
}
declaration.options.database.debug ??=
sharedResourcesConfig?.database?.debug
}

const loaded = (await MedusaModule.bootstrap({
moduleKey: moduleName,
defaultPath: path,
Expand Down Expand Up @@ -348,13 +370,14 @@ async function MedusaApp_({
})
}

const allModules = await loadModules(
modules,
sharedContainer_,
const allModules = await loadModules({
modulesConfig: modules,
sharedContainer: sharedContainer_,
sharedResourcesConfig,
migrationOnly,
loaderOnly,
workerMode
)
workerMode,
})

if (loaderOnly) {
async function query(...args: any[]) {
Expand Down Expand Up @@ -457,11 +480,18 @@ async function MedusaApp_({
}

for (const { resolution: moduleResolution } of moduleResolutions) {
if (!moduleResolution.options?.database) {
if (
!moduleResolution.options?.database &&
moduleResolution.moduleDeclaration?.scope === MODULE_SCOPE.INTERNAL &&
moduleResolution.moduleDeclaration?.resources ===
MODULE_RESOURCE_TYPE.SHARED
) {
moduleResolution.options ??= {}
moduleResolution.options.database = {
...(sharedResourcesConfig?.database ?? {}),
}
;(moduleResolution as any).options.database.debug ??=
sharedResourcesConfig?.database?.debug
}

const migrationOptions: MigrationOptions = {
Expand Down Expand Up @@ -517,6 +547,7 @@ async function MedusaApp_({
options.database ??= {
...sharedResourcesConfig?.database,
}
options.database.debug ??= sharedResourcesConfig?.database?.debug

return getMigrationPlanner(options, linkModules)
}
Expand Down

0 comments on commit 3505033

Please sign in to comment.