From 5237384d08f2f876ffaf5a73ed10a14686dc3964 Mon Sep 17 00:00:00 2001 From: Simon Larsen Date: Tue, 20 Aug 2024 17:37:45 +0100 Subject: [PATCH] refactor: Update PostgresAppInstance to use static methods and properties --- App/Index.ts | 2 +- .../Server/Infrastructure/PostgresDatabase.ts | 26 +++++++++---------- Common/Server/Infrastructure/Status.ts | 2 +- Common/Server/Services/DatabaseService.ts | 2 +- .../__mocks__/TestDatabase.mock.ts | 3 +-- Ingestor/Index.ts | 2 +- Nginx/Index.ts | 2 +- 7 files changed, 18 insertions(+), 21 deletions(-) diff --git a/App/Index.ts b/App/Index.ts index c46e688c22c..7275f6a8d2f 100755 --- a/App/Index.ts +++ b/App/Index.ts @@ -9,7 +9,7 @@ import Workers from "./FeatureSet/Workers/Index"; import Workflow from "./FeatureSet/Workflow/Index"; import { PromiseVoidFunction } from "Common/Types/FunctionTypes"; import { ClickhouseAppInstance } from "Common/Server/Infrastructure/ClickhouseDatabase"; -import { PostgresAppInstance } from "Common/Server/Infrastructure/PostgresDatabase"; +import PostgresAppInstance from "Common/Server/Infrastructure/PostgresDatabase"; import Redis from "Common/Server/Infrastructure/Redis"; import InfrastructureStatus from "Common/Server/Infrastructure/Status"; import logger from "Common/Server/Utils/Logger"; diff --git a/Common/Server/Infrastructure/PostgresDatabase.ts b/Common/Server/Infrastructure/PostgresDatabase.ts index f7a5c110fbd..80c4646ba0b 100644 --- a/Common/Server/Infrastructure/PostgresDatabase.ts +++ b/Common/Server/Infrastructure/PostgresDatabase.ts @@ -8,23 +8,23 @@ export type DatabaseSourceOptions = DataSourceOptions; export type DatabaseSource = DataSource; export default class Database { - protected dataSourceOptions: DataSourceOptions | null = null; - protected dataSource: DataSource | null = null; + protected static dataSourceOptions: DataSourceOptions | null = null; + protected static dataSource: DataSource | null = null; - public getDatasourceOptions(): DataSourceOptions { + public static getDatasourceOptions(): DataSourceOptions { this.dataSourceOptions = DatabaseDataSourceOptions; return this.dataSourceOptions; } - public getDataSource(): DataSource | null { + public static getDataSource(): DataSource | null { return this.dataSource; } - public isConnected(): boolean { + public static isConnected(): boolean { return Boolean(this.dataSource); } - public async connect(): Promise { + public static async connect(): Promise { let retry: number = 0; const dataSourceOptions: DataSourceOptions = this.getDatasourceOptions(); @@ -67,14 +67,14 @@ export default class Database { } } - public async disconnect(): Promise { + public static async disconnect(): Promise { if (this.dataSource) { await this.dataSource.destroy(); this.dataSource = null; } } - public async checkConnnectionStatus(): Promise { + public static async checkConnnectionStatus(): Promise { // check popstgres connection to see if it is still alive try { @@ -94,7 +94,7 @@ export default class Database { } } - public async dropDatabase(): Promise { + public static async dropDatabase(): Promise { await dropDatabase({ options: this.getDatasourceOptions(), }); @@ -102,23 +102,21 @@ export default class Database { this.dataSourceOptions = null; } - public async createDatabase(): Promise { + public static async createDatabase(): Promise { await createDatabase({ options: this.getDatasourceOptions(), ifNotExist: true, }); } - public async createAndConnect(): Promise { + public static async createAndConnect(): Promise { await this.createDatabase(); await this.connect(); } - public async disconnectAndDropDatabase(): Promise { + public static async disconnectAndDropDatabase(): Promise { // Drop the database. Since this is the in-mem db, it will be destroyed. await this.disconnect(); await this.dropDatabase(); } } - -export const PostgresAppInstance: Database = new Database(); diff --git a/Common/Server/Infrastructure/Status.ts b/Common/Server/Infrastructure/Status.ts index db02744607c..6127f5bcf7a 100644 --- a/Common/Server/Infrastructure/Status.ts +++ b/Common/Server/Infrastructure/Status.ts @@ -1,6 +1,6 @@ // This class checks the status of all the datasources. import { ClickhouseAppInstance } from "./ClickhouseDatabase"; -import { PostgresAppInstance } from "./PostgresDatabase"; +import PostgresAppInstance from "./PostgresDatabase"; import Redis from "./Redis"; import DatabaseNotConnectedException from "Common/Types/Exception/DatabaseNotConnectedException"; diff --git a/Common/Server/Services/DatabaseService.ts b/Common/Server/Services/DatabaseService.ts index 373eeb262c4..913c777b818 100644 --- a/Common/Server/Services/DatabaseService.ts +++ b/Common/Server/Services/DatabaseService.ts @@ -1,5 +1,5 @@ import { AppApiHostname, EncryptionSecret } from "../EnvironmentConfig"; -import { PostgresAppInstance } from "../Infrastructure/PostgresDatabase"; +import PostgresAppInstance from "../Infrastructure/PostgresDatabase"; import ClusterKeyAuthorization from "../Middleware/ClusterKeyAuthorization"; import CountBy from "../Types/Database/CountBy"; import CreateBy from "../Types/Database/CreateBy"; diff --git a/Common/Tests/Server/TestingUtils/__mocks__/TestDatabase.mock.ts b/Common/Tests/Server/TestingUtils/__mocks__/TestDatabase.mock.ts index 276e212f6ac..c1fc1a91fae 100644 --- a/Common/Tests/Server/TestingUtils/__mocks__/TestDatabase.mock.ts +++ b/Common/Tests/Server/TestingUtils/__mocks__/TestDatabase.mock.ts @@ -1,7 +1,6 @@ import getTestDataSourceOptions from "../Postgres/TestDataSourceOptions"; -import { +import PostgresAppInstance, { DatabaseSourceOptions, - PostgresAppInstance, } from "../../../../Server/Infrastructure/PostgresDatabase"; import Redis from "../../../../Server/Infrastructure/Redis"; import getTestRedisConnectionOptions from "../Redis/TestRedisOptions"; diff --git a/Ingestor/Index.ts b/Ingestor/Index.ts index 931418f1538..46408c2d902 100644 --- a/Ingestor/Index.ts +++ b/Ingestor/Index.ts @@ -8,7 +8,7 @@ import RegisterAPI from "./API/Register"; import ServerMonitorAPI from "./API/ServerMonitor"; import { PromiseVoidFunction } from "Common/Types/FunctionTypes"; import { ClickhouseAppInstance } from "Common/Server/Infrastructure/ClickhouseDatabase"; -import { PostgresAppInstance } from "Common/Server/Infrastructure/PostgresDatabase"; +import PostgresAppInstance from "Common/Server/Infrastructure/PostgresDatabase"; import Redis from "Common/Server/Infrastructure/Redis"; import InfrastructureStatus from "Common/Server/Infrastructure/Status"; import Express, { ExpressApplication } from "Common/Server/Utils/Express"; diff --git a/Nginx/Index.ts b/Nginx/Index.ts index eba0b98dc41..0a976bb90ec 100644 --- a/Nginx/Index.ts +++ b/Nginx/Index.ts @@ -1,6 +1,6 @@ import AcmeWriteCertificatesJob from "./Jobs/AcmeWriteCertificates"; import { PromiseVoidFunction } from "Common/Types/FunctionTypes"; -import { PostgresAppInstance } from "Common/Server/Infrastructure/PostgresDatabase"; +import PostgresAppInstance from "Common/Server/Infrastructure/PostgresDatabase"; import InfrastructureStatus from "Common/Server/Infrastructure/Status"; import logger from "Common/Server/Utils/Logger"; import App from "Common/Server/Utils/StartServer";