Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(modules-sdk): default db options #9313

Merged
merged 1 commit into from
Sep 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading