Skip to content

Commit

Permalink
feat(link-modules, pricing, product, utils): Custom database config i…
Browse files Browse the repository at this point in the history
…n shared mode (#5755)

* feat(link-modules, pricing, product, utils): Should be able to set some custom database config even in shared mode

* Create wet-hounds-retire.md
  • Loading branch information
adrien2p authored Nov 29, 2023
1 parent c4deeee commit 8f25ed8
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 3 deletions.
8 changes: 8 additions & 0 deletions .changeset/wet-hounds-retire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@medusajs/link-modules": patch
"@medusajs/pricing": patch
"@medusajs/product": patch
"@medusajs/utils": patch
---

feat(link-modules, pricing, product, utils): Should be able to set some custom database config even in shared mode
1 change: 1 addition & 0 deletions packages/link-modules/src/loaders/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export function connectionLoader(entity: EntitySchema) {
const pathToMigrations = __dirname + "/../migrations"

await ModulesSdkUtils.mikroOrmConnectionLoader({
moduleName: "link_module",
entities: [entity],
container,
options,
Expand Down
1 change: 1 addition & 0 deletions packages/pricing/src/loaders/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export default async (
const pathToMigrations = __dirname + "/../migrations"

await ModulesSdkUtils.mikroOrmConnectionLoader({
moduleName: "pricing",
entities,
container,
options,
Expand Down
1 change: 1 addition & 0 deletions packages/product/src/loaders/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export default async (
const pathToMigrations = __dirname + "/../migrations"

await ModulesSdkUtils.mikroOrmConnectionLoader({
moduleName: "product",
entities,
container,
options,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ import { mikroOrmCreateConnection } from "../../dal"
import { loadDatabaseConfig } from "../load-module-database-config"

export async function mikroOrmConnectionLoader({
moduleName,
container,
options,
moduleDeclaration,
entities,
pathToMigrations,
}: {
moduleName: string
entities: any[]
container: MedusaContainer
options?:
Expand All @@ -43,7 +45,18 @@ export async function mikroOrmConnectionLoader({
moduleDeclaration?.scope === MODULE_SCOPE.INTERNAL &&
moduleDeclaration.resources === MODULE_RESOURCE_TYPE.SHARED
) {
return await loadShared({ container, entities, pathToMigrations })
const shouldSwallowError = true
const dbConfig = loadDatabaseConfig(
moduleName,
(options ?? {}) as ModulesSdkTypes.ModuleServiceInitializeOptions,
shouldSwallowError
)
return await loadShared({
database: dbConfig,
container,
entities,
pathToMigrations,
})
}

/**
Expand All @@ -55,7 +68,7 @@ export async function mikroOrmConnectionLoader({
)?.database?.connection
dbConfig = {
...loadDatabaseConfig(
"product",
moduleName,
(options ?? {}) as ModulesSdkTypes.ModuleServiceInitializeOptions,
shouldSwallowError
),
Expand Down Expand Up @@ -95,7 +108,7 @@ async function loadDefault({
return orm.em.fork()
}

async function loadShared({ container, entities, pathToMigrations }) {
async function loadShared({ database, container, entities, pathToMigrations }) {
const sharedConnection = container.resolve(
ContainerRegistrationKeys.PG_CONNECTION,
{
Expand All @@ -111,6 +124,7 @@ async function loadShared({ container, entities, pathToMigrations }) {
const manager = await loadDefault({
entities,
database: {
...database,
connection: sharedConnection,
},
pathToMigrations,
Expand Down

0 comments on commit 8f25ed8

Please sign in to comment.