feat(utils, module-sdk, medusa): Extract pg connection utils to utils package (#4961)
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
import { asValue, AwilixContainer } from "awilix"
|
||||
import { ConfigModule } from "../types/global"
|
||||
import { ContainerRegistrationKeys } from "@medusajs/utils"
|
||||
import { ClientConfig } from "pg"
|
||||
import knex from "knex"
|
||||
import { ContainerRegistrationKeys, ModulesSdkUtils } from "@medusajs/utils"
|
||||
|
||||
type Options = {
|
||||
configModule: ConfigModule
|
||||
@@ -10,44 +8,32 @@ type Options = {
|
||||
}
|
||||
|
||||
export default async ({ container, configModule }: Options): Promise<void> => {
|
||||
const connectionString = configModule.projectConfig.database_url
|
||||
const database = configModule.projectConfig.database_database
|
||||
const extra: any = configModule.projectConfig.database_extra || {}
|
||||
const schema = configModule.projectConfig.database_schema || "public"
|
||||
if (container.hasRegistration(ContainerRegistrationKeys.PG_CONNECTION)) {
|
||||
return
|
||||
}
|
||||
|
||||
// Share a knex connection to be consumed by the shared modules
|
||||
if (!container.hasRegistration(ContainerRegistrationKeys.PG_CONNECTION)) {
|
||||
const config: ClientConfig = {
|
||||
connectionString,
|
||||
database,
|
||||
ssl: extra?.ssl ?? false,
|
||||
idle_in_transaction_session_timeout:
|
||||
extra.idle_in_transaction_session_timeout ?? undefined, // prevent null to be passed
|
||||
}
|
||||
const connectionString = configModule.projectConfig.database_url
|
||||
const extra: any = configModule.projectConfig.database_extra || {}
|
||||
const schema = configModule.projectConfig.database_schema || "public"
|
||||
const idleTimeoutMillis = extra.idleTimeoutMillis ?? undefined // prevent null to be passed
|
||||
const poolMax = extra.max
|
||||
|
||||
// TODO: see later if it is possible to use the same connection with multiple pool config using this shared connection across the modules
|
||||
// const pgConnection = await new Client(config).connect() // or any other method to create your connection here
|
||||
delete extra.max
|
||||
delete extra.idleTimeoutMillis
|
||||
|
||||
const pgConnection = knex<any, any>({
|
||||
client: "pg",
|
||||
searchPath: schema,
|
||||
connection: {
|
||||
connectionString: connectionString,
|
||||
database,
|
||||
ssl: extra?.ssl ?? false,
|
||||
idle_in_transaction_session_timeout:
|
||||
extra.idle_in_transaction_session_timeout ?? undefined, // prevent null to be passed
|
||||
},
|
||||
pool: {
|
||||
min: 0,
|
||||
max: extra.max,
|
||||
idleTimeoutMillis: extra.idleTimeoutMillis ?? undefined, // prevent null to be passed
|
||||
},
|
||||
})
|
||||
const pgConnection = ModulesSdkUtils.createPgConnection({
|
||||
clientUrl: connectionString,
|
||||
schema,
|
||||
driverOptions: extra,
|
||||
pool: {
|
||||
max: poolMax,
|
||||
idleTimeoutMillis,
|
||||
},
|
||||
})
|
||||
|
||||
container.register(
|
||||
ContainerRegistrationKeys.PG_CONNECTION,
|
||||
asValue(pgConnection)
|
||||
)
|
||||
}
|
||||
container.register(
|
||||
ContainerRegistrationKeys.PG_CONNECTION,
|
||||
asValue(pgConnection)
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user