fix: define ssl options when specified in the connection string (#9992)
This commit is contained in:
1 parent
03f4b66b90
commit
307f1015cd
1 file changed
+53
-11
@@ -10,6 +10,15 @@ import {
|
|||||||
parseConnectionString,
|
parseConnectionString,
|
||||||
} from "@medusajs/framework/utils"
|
} from "@medusajs/framework/utils"
|
||||||
|
|
||||||
|
async function connectClient(client: ReturnType<typeof createClient>) {
|
||||||
|
try {
|
||||||
|
await client.connect()
|
||||||
|
return { connected: true, error: null }
|
||||||
|
} catch (error) {
|
||||||
|
return { connected: false, error }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A low-level utility to create the database. This util should
|
* A low-level utility to create the database. This util should
|
||||||
* never exit the process implicitly.
|
* never exit the process implicitly.
|
||||||
@@ -69,30 +78,63 @@ export async function dbCreate({
|
|||||||
/**
|
/**
|
||||||
* Parse connection string specified as "DATABASE_URL" inside the
|
* Parse connection string specified as "DATABASE_URL" inside the
|
||||||
* .env file and create a client instance from it.
|
* .env file and create a client instance from it.
|
||||||
*
|
|
||||||
* Remember we should not specify the database name from the
|
|
||||||
* connection string, because this database might not
|
|
||||||
* exist
|
|
||||||
*/
|
*/
|
||||||
const connectionOptions = parseConnectionString(dbConnectionString)
|
const connectionOptions = parseConnectionString(dbConnectionString)
|
||||||
const client = createClient({
|
|
||||||
|
/**
|
||||||
|
* The following client config is without any database name. This is because
|
||||||
|
* we want to connect to the default database (whatever it is) and create
|
||||||
|
* a new database that we expect not to exist.
|
||||||
|
*/
|
||||||
|
const clientConfig = {
|
||||||
host: connectionOptions.host!,
|
host: connectionOptions.host!,
|
||||||
port: connectionOptions.port ? Number(connectionOptions.port) : undefined,
|
port: connectionOptions.port ? Number(connectionOptions.port) : undefined,
|
||||||
user: connectionOptions.user,
|
user: connectionOptions.user,
|
||||||
password: connectionOptions.password,
|
password: connectionOptions.password,
|
||||||
})
|
...(connectionOptions.ssl ? { ssl: connectionOptions.ssl as any } : {}),
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
/**
|
||||||
await client.connect()
|
* In some case the default database (which is same as the username) does
|
||||||
logger.info(`Connection established with the database "${dbName}"`)
|
* not exist. For example: With Neon, there is no database name after
|
||||||
} catch (error) {
|
* the connection username. Hence, we will have to connect with the
|
||||||
|
* postgres database.
|
||||||
|
*/
|
||||||
|
const clientConfigWithPostgresDB = {
|
||||||
|
host: connectionOptions.host!,
|
||||||
|
port: connectionOptions.port ? Number(connectionOptions.port) : undefined,
|
||||||
|
user: connectionOptions.user,
|
||||||
|
database: "postgres",
|
||||||
|
password: connectionOptions.password,
|
||||||
|
...(connectionOptions.ssl ? { ssl: connectionOptions.ssl as any } : {}),
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* First connect with the default DB
|
||||||
|
*/
|
||||||
|
let client = createClient(clientConfig)
|
||||||
|
let connectionState = await connectClient(client)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* In case of an error, connect with the postgres DB
|
||||||
|
*/
|
||||||
|
if (!connectionState.connected) {
|
||||||
|
client = createClient(clientConfigWithPostgresDB)
|
||||||
|
connectionState = await connectClient(client)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Notify user about the connection state
|
||||||
|
*/
|
||||||
|
if (!connectionState.connected) {
|
||||||
logger.error(
|
logger.error(
|
||||||
"Unable to establish database connection because of the following error"
|
"Unable to establish database connection because of the following error"
|
||||||
)
|
)
|
||||||
logger.error(error)
|
logger.error(connectionState.error)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logger.info(`Connection established with the database "${dbName}"`)
|
||||||
if (await dbExists(client, dbName)) {
|
if (await dbExists(client, dbName)) {
|
||||||
logger.info(`Database "${dbName}" already exists`)
|
logger.info(`Database "${dbName}" already exists`)
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user