feat(utils, module-sdk, medusa): Extract pg connection utils to utils package (#4961)

This commit is contained in:
Adrien de Peretti
2023-09-11 12:10:40 +02:00
committed by GitHub
parent fbae7f9654
commit 6273b4b160
10 changed files with 85 additions and 76 deletions

View 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,
},
})
}

View File

@@ -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"