Skip to content

Commit

Permalink
feat(create): Support SSL enforced PostgreSQL databases (#2905)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomh4 authored Jun 20, 2024
1 parent 36be89f commit 65b4f3c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
15 changes: 15 additions & 0 deletions packages/create/src/gather-user-responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ interface PromptAnswers {
dbSchema?: string | symbol;
dbUserName: string | symbol;
dbPassword: string | symbol;
dbSSL?: boolean | symbol;
superadminIdentifier: string | symbol;
superadminPassword: string | symbol;
populateProducts: boolean | symbol;
Expand Down Expand Up @@ -72,6 +73,19 @@ export async function gatherUserResponses(
})
: '';
checkCancel(dbSchema);
const dbSSL =
dbType === 'postgres'
? await select({
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 },
],
initialValue: false,
})
: false;
checkCancel(dbSSL);
const dbUserName = hasConnection
? await text({
message: "What's the database user name?",
Expand Down Expand Up @@ -113,6 +127,7 @@ export async function gatherUserResponses(
dbSchema,
dbUserName,
dbPassword,
dbSSL,
superadminIdentifier,
superadminPassword,
populateProducts,
Expand Down
15 changes: 15 additions & 0 deletions packages/create/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ async function checkPostgresDbExists(options: any, root: string): Promise<true>
port: options.port,
database: options.database,
schema: options.schema,
ssl: options.ssl,
};
const client = new Client(connectionOptions);

Expand All @@ -371,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 @@ -389,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
3 changes: 3 additions & 0 deletions packages/create/templates/vendure-config.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ export const config: VendureConfig = {
{{#if dbSchema}}
schema: process.env.DB_SCHEMA,
{{/if}}
{{#if dbSSL}}
ssl: true,
{{/if}}
{{#if isSQLjs}}
location: path.join(__dirname, 'vendure.sqlite'),
autoSave: true,
Expand Down

0 comments on commit 65b4f3c

Please sign in to comment.