refactor: Update PostgresAppInstance to use static methods and properties

This commit is contained in:
Simon Larsen
2024-08-20 17:37:45 +01:00
parent ee29277cb9
commit 5237384d08
7 changed files with 18 additions and 21 deletions

View File

@@ -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";

View File

@@ -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<DataSource> {
public static async connect(): Promise<DataSource> {
let retry: number = 0;
const dataSourceOptions: DataSourceOptions = this.getDatasourceOptions();
@@ -67,14 +67,14 @@ export default class Database {
}
}
public async disconnect(): Promise<void> {
public static async disconnect(): Promise<void> {
if (this.dataSource) {
await this.dataSource.destroy();
this.dataSource = null;
}
}
public async checkConnnectionStatus(): Promise<boolean> {
public static async checkConnnectionStatus(): Promise<boolean> {
// check popstgres connection to see if it is still alive
try {
@@ -94,7 +94,7 @@ export default class Database {
}
}
public async dropDatabase(): Promise<void> {
public static async dropDatabase(): Promise<void> {
await dropDatabase({
options: this.getDatasourceOptions(),
});
@@ -102,23 +102,21 @@ export default class Database {
this.dataSourceOptions = null;
}
public async createDatabase(): Promise<void> {
public static async createDatabase(): Promise<void> {
await createDatabase({
options: this.getDatasourceOptions(),
ifNotExist: true,
});
}
public async createAndConnect(): Promise<void> {
public static async createAndConnect(): Promise<void> {
await this.createDatabase();
await this.connect();
}
public async disconnectAndDropDatabase(): Promise<void> {
public static async disconnectAndDropDatabase(): Promise<void> {
// 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();

View File

@@ -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";

View File

@@ -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";

View File

@@ -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";

View File

@@ -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";

View File

@@ -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";