Allow the use of SSL connections to the postgres database. (#1256)

* Allow the use of SSL connections to the postgres database.

* Add default SSL false when no env set

* Add commented out example of DB_SSL env

* Refactor add SSL option into PostgresConnectionOptions

* Refactor the database connection to optionally use a URL string instead of the env variables

* Refactor the database connection based on feedback

* Add dynamic validation around the DB envs

* Remove DB_URL from example

* Fix rebase

* Add back the optional database port in the example

* Formatted file correctly

* change types to a const to fix tests
This commit is contained in:
Hammer
2023-01-20 20:27:01 +00:00
committed by GitHub
parent 652f5cbf20
commit 5340683199
2 changed files with 24 additions and 9 deletions

View File

@@ -1,13 +1,8 @@
import { PostgresConnectionOptions } from 'typeorm/driver/postgres/PostgresConnectionOptions';
import { DataSource } from 'typeorm';
export const databaseConfig: PostgresConnectionOptions = {
const baseDatabaseConfig: PostgresConnectionOptions = {
type: 'postgres',
host: process.env.DB_HOSTNAME || 'immich_postgres',
port: parseInt(process.env.DB_PORT || '5432'),
username: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE_NAME,
entities: [__dirname + '/../**/*.entity.{js,ts}'],
synchronize: false,
migrations: [__dirname + '/../migrations/*.{js,ts}'],
@@ -15,4 +10,17 @@ export const databaseConfig: PostgresConnectionOptions = {
connectTimeoutMS: 10000, // 10 seconds
};
const envBasedDatabaseConfig = {
host: process.env.DB_HOSTNAME || 'immich_postgres',
port: parseInt(process.env.DB_PORT || '5432'),
username: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE_NAME,
};
const url = process.env.DB_URL;
const additionalSSLDatabaseConfig = url ? { url } : envBasedDatabaseConfig;
export const databaseConfig: PostgresConnectionOptions = { ...baseDatabaseConfig, ...additionalSSLDatabaseConfig };
export const dataSource = new DataSource(databaseConfig);