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
30
packages/utils/src/modules-sdk/create-pg-connection.ts
Normal file
30
packages/utils/src/modules-sdk/create-pg-connection.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import knex from "knex"
|
||||
import { ModuleServiceInitializeOptions } from "@medusajs/types"
|
||||
|
||||
type Options = ModuleServiceInitializeOptions["database"]
|
||||
|
||||
/**
|
||||
* Create a new knex (pg in the future) connection which can be reused and shared
|
||||
* @param options
|
||||
*/
|
||||
export function createPgConnection(options: Options) {
|
||||
const { pool, schema = "public", clientUrl, driverOptions } = options
|
||||
const ssl = options.driverOptions?.ssl ?? false
|
||||
|
||||
return knex<any, any>({
|
||||
client: "pg",
|
||||
searchPath: schema,
|
||||
connection: {
|
||||
connectionString: clientUrl,
|
||||
ssl,
|
||||
idle_in_transaction_session_timeout:
|
||||
(driverOptions?.idle_in_transaction_session_timeout as number) ??
|
||||
undefined, // prevent null to be passed
|
||||
},
|
||||
pool: {
|
||||
// https://knexjs.org/guide/#pool
|
||||
...(pool ?? {}),
|
||||
min: (pool?.min as number) ?? 0,
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -3,3 +3,4 @@ export * from "./decorators"
|
||||
export * from "./build-query"
|
||||
export * from "./retrieve-entity"
|
||||
export * from "./loaders/mikro-orm-connection-loader"
|
||||
export * from "./create-pg-connection"
|
||||
|
||||
Reference in New Issue
Block a user