From cd5256da7fa01d6fa88be0dc509f946029caa053 Mon Sep 17 00:00:00 2001 From: David Mehren Date: Mon, 18 Apr 2022 15:21:49 +0200 Subject: [PATCH] refactor(db-config): Use typeorm-style options TypeORM does not use a separate config option for the path to the SQLite file. Additionally, the "dialect" is called "type." This commit adjusts our config to follow the upstream convention to reduce confusion. Signed-off-by: David Mehren --- docs/content/config/index.md | 17 ++++--- ...-dialect.enum.ts => database-type.enum.ts} | 2 +- src/config/database.config.ts | 44 ++++++++----------- src/config/mock/database.config.mock.ts | 6 +-- src/utils/session.ts | 4 +- test/app.e2e-spec.ts | 4 +- 6 files changed, 34 insertions(+), 43 deletions(-) rename src/config/{database-dialect.enum.ts => database-type.enum.ts} (87%) diff --git a/docs/content/config/index.md b/docs/content/config/index.md index a0ab8863f..51d19cf49 100644 --- a/docs/content/config/index.md +++ b/docs/content/config/index.md @@ -55,15 +55,14 @@ We officially support and test these databases: - PostgreSQL - MariaDB -| environment variable | default | example | description | -|------------------------|---------|---------------------|-----------------------------------------------------------------------------------------------| -| `HD_DATABASE_DIALECT` | - | `postgres` | The database dialect you want to use. This can be `postgres`, `mysql`, `mariadb` or `sqlite`. | -| `HD_DATABASE_HOST` | - | `db.example.com` | The host, where the database runs. *Only if you're **not** using `sqlite`.* | -| `HD_DATABASE_PORT` | - | `5432` | The port, where the database runs. *Only if you're **not** using `sqlite`.* | -| `HD_DATABASE_NAME` | - | `hedgedoc` | The name of the database to use. *Only if you're **not** using `sqlite`.* | -| `HD_DATABASE_USER` | - | `hedgedoc` | The user that logs in the database. *Only if you're **not** using `sqlite`.* | -| `HD_DATABASE_PASS` | - | `password` | The password to log into the database. *Only if you're **not** using `sqlite`.* | -| `HD_DATABASE_STORAGE` | - | `./hedgedoc.sqlite` | The location of the database file. *Only if you're using `sqlite`.* | +| environment variable | default | example | description | +|-----------------------|---------|---------------------|--------------------------------------------------------------------------------------------| +| `HD_DATABASE_TYPE` | - | `postgres` | The database type you want to use. This can be `postgres`, `mysql`, `mariadb` or `sqlite`. | +| `HD_DATABASE_NAME` | - | `hedgedoc` | The name of the database to use. When using SQLite, this is the path to the database file. | +| `HD_DATABASE_HOST` | - | `db.example.com` | The host, where the database runs. *Only if you're **not** using `sqlite`.* | +| `HD_DATABASE_PORT` | - | `5432` | The port, where the database runs. *Only if you're **not** using `sqlite`.* | +| `HD_DATABASE_USER` | - | `hedgedoc` | The user that logs in the database. *Only if you're **not** using `sqlite`.* | +| `HD_DATABASE_PASS` | - | `password` | The password to log into the database. *Only if you're **not** using `sqlite`.* | ## External services diff --git a/src/config/database-dialect.enum.ts b/src/config/database-type.enum.ts similarity index 87% rename from src/config/database-dialect.enum.ts rename to src/config/database-type.enum.ts index a629edad7..9912332c5 100644 --- a/src/config/database-dialect.enum.ts +++ b/src/config/database-type.enum.ts @@ -4,7 +4,7 @@ * SPDX-License-Identifier: AGPL-3.0-only */ -export enum DatabaseDialect { +export enum DatabaseType { POSTGRES = 'postgres', MYSQL = 'mysql', MARIADB = 'mariadb', diff --git a/src/config/database.config.ts b/src/config/database.config.ts index 0aab632df..6286c686c 100644 --- a/src/config/database.config.ts +++ b/src/config/database.config.ts @@ -6,7 +6,7 @@ import { registerAs } from '@nestjs/config'; import * as Joi from 'joi'; -import { DatabaseDialect } from './database-dialect.enum'; +import { DatabaseType } from './database-type.enum'; import { buildErrorMessage, parseOptionalNumber } from './utils'; export interface DatabaseConfig { @@ -15,56 +15,48 @@ export interface DatabaseConfig { database: string; host: string; port: number; - storage: string; - dialect: DatabaseDialect; + type: DatabaseType; } const databaseSchema = Joi.object({ - username: Joi.when('dialect', { - is: Joi.invalid(DatabaseDialect.SQLITE), + type: Joi.string() + .valid(...Object.values(DatabaseType)) + .label('HD_DATABASE_TYPE'), + + // This is the database name, except for SQLite, + // where it is the path to the database file. + database: Joi.string().label('HD_DATABASE_NAME'), + username: Joi.when('type', { + is: Joi.invalid(DatabaseType.SQLITE), then: Joi.string(), otherwise: Joi.optional(), }).label('HD_DATABASE_USER'), - password: Joi.when('dialect', { - is: Joi.invalid(DatabaseDialect.SQLITE), + password: Joi.when('type', { + is: Joi.invalid(DatabaseType.SQLITE), then: Joi.string(), otherwise: Joi.optional(), }).label('HD_DATABASE_PASS'), - database: Joi.when('dialect', { - is: Joi.invalid(DatabaseDialect.SQLITE), - then: Joi.string(), - otherwise: Joi.optional(), - }).label('HD_DATABASE_NAME'), - host: Joi.when('dialect', { - is: Joi.invalid(DatabaseDialect.SQLITE), + host: Joi.when('type', { + is: Joi.invalid(DatabaseType.SQLITE), then: Joi.string(), otherwise: Joi.optional(), }).label('HD_DATABASE_HOST'), - port: Joi.when('dialect', { - is: Joi.invalid(DatabaseDialect.SQLITE), + port: Joi.when('type', { + is: Joi.invalid(DatabaseType.SQLITE), then: Joi.number(), otherwise: Joi.optional(), }).label('HD_DATABASE_PORT'), - storage: Joi.when('dialect', { - is: Joi.valid(DatabaseDialect.SQLITE), - then: Joi.string(), - otherwise: Joi.optional(), - }).label('HD_DATABASE_STORAGE'), - dialect: Joi.string() - .valid(...Object.values(DatabaseDialect)) - .label('HD_DATABASE_DIALECT'), }); export default registerAs('databaseConfig', () => { const databaseConfig = databaseSchema.validate( { + type: process.env.HD_DATABASE_TYPE, username: process.env.HD_DATABASE_USER, password: process.env.HD_DATABASE_PASS, database: process.env.HD_DATABASE_NAME, host: process.env.HD_DATABASE_HOST, port: parseOptionalNumber(process.env.HD_DATABASE_PORT), - storage: process.env.HD_DATABASE_STORAGE, - dialect: process.env.HD_DATABASE_DIALECT, }, { abortEarly: false, diff --git a/src/config/mock/database.config.mock.ts b/src/config/mock/database.config.mock.ts index ae3e12d9c..cb2038f7f 100644 --- a/src/config/mock/database.config.mock.ts +++ b/src/config/mock/database.config.mock.ts @@ -5,18 +5,18 @@ */ import { registerAs } from '@nestjs/config'; -import { DatabaseDialect } from '../database-dialect.enum'; +import { DatabaseType } from '../database-type.enum'; import { DatabaseConfig } from '../database.config'; export default registerAs( 'databaseConfig', (): DatabaseConfig => ({ - dialect: (process.env.HEDGEDOC_TEST_DB_TYPE || 'sqlite') as DatabaseDialect, + type: (process.env.HEDGEDOC_TEST_DB_TYPE || + DatabaseType.SQLITE) as DatabaseType, database: 'hedgedoc', password: 'hedgedoc', host: 'localhost', port: 0, - storage: '', username: 'hedgedoc', }), ); diff --git a/src/utils/session.ts b/src/utils/session.ts index 095f35731..f3c359270 100644 --- a/src/utils/session.ts +++ b/src/utils/session.ts @@ -10,7 +10,7 @@ import session from 'express-session'; import { Repository } from 'typeorm'; import { AuthConfig } from '../config/auth.config'; -import { DatabaseDialect } from '../config/database-dialect.enum'; +import { DatabaseType } from '../config/database-type.enum'; import { DatabaseConfig } from '../config/database.config'; import { Session } from '../users/session.entity'; @@ -36,7 +36,7 @@ export function setupSessionMiddleware( saveUninitialized: false, store: new TypeormStore({ cleanupLimit: 2, - limitSubquery: dbConfig.dialect !== DatabaseDialect.MARIADB, + limitSubquery: dbConfig.type !== DatabaseType.MARIADB, }).connect(app.get>(getRepositoryToken(Session))), }), ); diff --git a/test/app.e2e-spec.ts b/test/app.e2e-spec.ts index add0e1561..da71d8e18 100644 --- a/test/app.e2e-spec.ts +++ b/test/app.e2e-spec.ts @@ -33,8 +33,8 @@ describe('App', () => { }) .overrideProvider(getConfigToken('databaseConfig')) .useValue({ - storage: ':memory:', - dialect: 'sqlite', + database: ':memory:', + type: 'sqlite', }) .overrideProvider(getConfigToken('authConfig')) .useValue({