diff --git a/packages/create/src/gather-user-responses.ts b/packages/create/src/gather-user-responses.ts index 4481d6c4b1..b264923671 100644 --- a/packages/create/src/gather-user-responses.ts +++ b/packages/create/src/gather-user-responses.ts @@ -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; @@ -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?", @@ -113,6 +127,7 @@ export async function gatherUserResponses( dbSchema, dbUserName, dbPassword, + dbSSL, superadminIdentifier, superadminPassword, populateProducts, diff --git a/packages/create/src/helpers.ts b/packages/create/src/helpers.ts index 84b8f0ec52..c09eb723e0 100644 --- a/packages/create/src/helpers.ts +++ b/packages/create/src/helpers.ts @@ -352,6 +352,7 @@ async function checkPostgresDbExists(options: any, root: string): Promise port: options.port, database: options.database, schema: options.schema, + ssl: options.ssl, }; const client = new Client(connectionOptions); @@ -371,6 +372,8 @@ async function checkPostgresDbExists(options: any, root: string): Promise 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(); @@ -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.`); } diff --git a/packages/create/templates/vendure-config.hbs b/packages/create/templates/vendure-config.hbs index 0ba38b8204..94a08b1ce2 100644 --- a/packages/create/templates/vendure-config.hbs +++ b/packages/create/templates/vendure-config.hbs @@ -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,