Skip to content

Commit

Permalink
fix(create): Supporting SSL enforced PostgreSQL databases
Browse files Browse the repository at this point in the history
If a db has mandatory ssl when connecting the current @vendure/create fails.
Implemented SSL Option for PostgreSQL
Added SSL option to populate function
Implemented error handling for error 28000 interpreted as SSL error
  • Loading branch information
tomh4 committed Jun 19, 2024
1 parent 043b6b9 commit 42788d8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/create/src/gather-user-responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ export async function gatherUserResponses(
const dbSSL =
dbType === 'postgres'
? await select({
message: 'Use SSL to connect to the database?',
message:
'Use SSL to connect to the database? (only enable if you database provider supports SSL)',
options: [
{ label: 'no', value: false },
{ label: 'yes', value: true },
Expand Down
14 changes: 14 additions & 0 deletions packages/create/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,8 @@ async function checkPostgresDbExists(options: any, root: string): Promise<true>
throwDatabaseDoesNotExist(options.database);
} else if (e.message === 'NO_SCHEMA') {
throwDatabaseSchemaDoesNotExist(options.database, options.schema);
} else if (e.code === '28000') {
throwSSLConnectionError(e, options.ssl);
}
throwConnectionError(e);
await client.end();
Expand All @@ -390,6 +392,18 @@ function throwConnectionError(err: any) {
);
}

function throwSSLConnectionError(err: any, sslEnabled?: any) {
throw new Error(
'Could not connect to the database. ' +
(sslEnabled === undefined
? 'Is your server requiring an SSL connection?'
: 'Are you sure your server supports SSL?') +
`Please check the connection settings in your Vendure config.\n[${
(err.message || err.toString()) as string
}]`,
);
}

function throwDatabaseDoesNotExist(name: string) {
throw new Error(`Database "${name}" does not exist. Please create the database and then try again.`);
}
Expand Down

0 comments on commit 42788d8

Please sign in to comment.