Skip to content

Commit

Permalink
refactor: Update PostgresAppInstance to use static methods and proper…
Browse files Browse the repository at this point in the history
…ties
  • Loading branch information
simlarsen committed Aug 20, 2024
1 parent ee29277 commit 5237384
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 21 deletions.
2 changes: 1 addition & 1 deletion App/Index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
26 changes: 12 additions & 14 deletions Common/Server/Infrastructure/PostgresDatabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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 {
Expand All @@ -94,31 +94,29 @@ export default class Database {
}
}

public async dropDatabase(): Promise<void> {
public static async dropDatabase(): Promise<void> {
await dropDatabase({
options: this.getDatasourceOptions(),
});
this.dataSource = null;
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();
2 changes: 1 addition & 1 deletion Common/Server/Infrastructure/Status.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down
2 changes: 1 addition & 1 deletion Common/Server/Services/DatabaseService.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
2 changes: 1 addition & 1 deletion Ingestor/Index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion Nginx/Index.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down

0 comments on commit 5237384

Please sign in to comment.