feat(utils, module-sdk, medusa): Extract pg connection utils to utils package (#4961)
This commit is contained in:
committed by
GitHub
parent
fbae7f9654
commit
6273b4b160
@@ -10,8 +10,8 @@ import {
|
||||
} from "@medusajs/types"
|
||||
import {
|
||||
ContainerRegistrationKeys,
|
||||
ModulesSdkUtils,
|
||||
isObject,
|
||||
ModulesSdkUtils,
|
||||
} from "@medusajs/utils"
|
||||
import { MODULE_PACKAGE_NAMES, Modules } from "./definitions"
|
||||
import { MedusaModule } from "./medusa-module"
|
||||
@@ -19,21 +19,12 @@ import { RemoteLink } from "./remote-link"
|
||||
import { RemoteQuery } from "./remote-query"
|
||||
|
||||
export type MedusaModuleConfig = (Partial<ModuleConfig> | Modules)[]
|
||||
export type SharedResources = {
|
||||
database?: ModuleServiceInitializeOptions["database"] & {
|
||||
pool?: {
|
||||
name?: string
|
||||
afterCreate?: Function
|
||||
min?: number
|
||||
max?: number
|
||||
refreshIdle?: boolean
|
||||
idleTimeoutMillis?: number
|
||||
reapIntervalMillis?: number
|
||||
returnToHead?: boolean
|
||||
priorityRange?: number
|
||||
log?: (message: string, logLevel: string) => void
|
||||
}
|
||||
}
|
||||
type SharedResources = {
|
||||
database?: ModuleServiceInitializeOptions["database"]
|
||||
}
|
||||
|
||||
const isModuleConfig = (obj: any): obj is ModuleConfig => {
|
||||
return isObject(obj)
|
||||
}
|
||||
|
||||
export async function MedusaApp({
|
||||
@@ -77,25 +68,16 @@ export async function MedusaApp({
|
||||
sharedResourcesConfig as ModuleServiceInitializeOptions,
|
||||
true
|
||||
)!
|
||||
const { pool } = sharedResourcesConfig?.database ?? {}
|
||||
|
||||
if (dbData?.clientUrl) {
|
||||
const { knex } = await import("knex")
|
||||
const dbConnection = knex({
|
||||
client: "pg",
|
||||
searchPath: dbData.schema || "public",
|
||||
connection: {
|
||||
connectionString: dbData.clientUrl,
|
||||
ssl: (dbData.driverOptions?.connection as any).ssl! ?? false,
|
||||
},
|
||||
pool: {
|
||||
// https://knexjs.org/guide/#pool
|
||||
...(pool ?? {}),
|
||||
min: pool?.min ?? 0,
|
||||
},
|
||||
})
|
||||
|
||||
injectedDependencies[ContainerRegistrationKeys.PG_CONNECTION] = dbConnection
|
||||
if (
|
||||
dbData.clientUrl &&
|
||||
!injectedDependencies[ContainerRegistrationKeys.PG_CONNECTION]
|
||||
) {
|
||||
injectedDependencies[ContainerRegistrationKeys.PG_CONNECTION] =
|
||||
ModulesSdkUtils.createPgConnection({
|
||||
...(sharedResourcesConfig?.database ?? {}),
|
||||
...dbData,
|
||||
})
|
||||
}
|
||||
|
||||
const allModules: Record<string, LoadedModule | LoadedModule[]> = {}
|
||||
@@ -106,7 +88,7 @@ export async function MedusaApp({
|
||||
let path: string
|
||||
let declaration: any = {}
|
||||
|
||||
if (isObject(mod)) {
|
||||
if (isModuleConfig(mod)) {
|
||||
if (!mod.module) {
|
||||
throw new Error(
|
||||
`Module ${JSON.stringify(mod)} is missing module name.`
|
||||
@@ -141,7 +123,7 @@ export async function MedusaApp({
|
||||
declaration,
|
||||
undefined,
|
||||
injectedDependencies,
|
||||
isObject(mod) ? mod.definition : undefined
|
||||
isModuleConfig(mod) ? mod.definition : undefined
|
||||
)) as LoadedModule
|
||||
|
||||
if (allModules[key] && !Array.isArray(allModules[key])) {
|
||||
|
||||
Reference in New Issue
Block a user