mirror of
https://github.com/OneUptime/oneuptime
synced 2024-11-21 22:59:07 +00:00
refactor: Update PostgresDatabase.ts to use async/await for createDatabase and dropDatabase methods
This commit is contained in:
parent
95dca8e8c6
commit
99a80ab9b6
@ -1,6 +1,5 @@
|
||||
import logger from "../Utils/Logger";
|
||||
import dataSourceOptions from "./Postgres/DataSourceOptions";
|
||||
import getTestDataSourceOptions from "./Postgres/TestDataSourceOptions";
|
||||
import Sleep from "Common/Types/Sleep";
|
||||
import { DataSource, DataSourceOptions } from "typeorm";
|
||||
import { createDatabase, dropDatabase } from "typeorm-extension";
|
||||
@ -9,16 +8,12 @@ export type DatabaseSourceOptions = DataSourceOptions;
|
||||
export type DatabaseSource = DataSource;
|
||||
|
||||
export default class Database {
|
||||
private dataSource!: DataSource | null;
|
||||
protected dataSource!: DataSource | null;
|
||||
|
||||
public getDatasourceOptions(): DataSourceOptions {
|
||||
return dataSourceOptions;
|
||||
}
|
||||
|
||||
public getTestDatasourceOptions(): DataSourceOptions {
|
||||
return getTestDataSourceOptions();
|
||||
}
|
||||
|
||||
public getDataSource(): DataSource | null {
|
||||
return this.dataSource;
|
||||
}
|
||||
|
@ -26,7 +26,7 @@ describe("probeService", () => {
|
||||
});
|
||||
|
||||
test("create a new probe", async () => {
|
||||
const probeService: ProbeService = new ProbeService(database.getDatabase());
|
||||
const probeService: ProbeService = new ProbeService(database);
|
||||
const name: string = Faker.generateName();
|
||||
const probeVersion: Version = new Version("1.0.1");
|
||||
const key: ObjectID = ObjectID.generate();
|
||||
@ -55,7 +55,7 @@ describe("probeService", () => {
|
||||
});
|
||||
|
||||
test("findOneBy probe by name", async () => {
|
||||
const probeService: ProbeService = new ProbeService(database.getDatabase());
|
||||
const probeService: ProbeService = new ProbeService(database);
|
||||
const name: string = Faker.generateName();
|
||||
const probeVersion: Version = new Version("1.0.1");
|
||||
const key: ObjectID = ObjectID.generate();
|
||||
@ -108,7 +108,7 @@ describe("probeService", () => {
|
||||
});
|
||||
|
||||
test("findOneBy by probeVersion", async () => {
|
||||
const probeService: ProbeService = new ProbeService(database.getDatabase());
|
||||
const probeService: ProbeService = new ProbeService(database);
|
||||
const name: string = Faker.generateName();
|
||||
const probeVersion: Version = new Version("1.0.2");
|
||||
const key: ObjectID = ObjectID.generate();
|
||||
@ -157,7 +157,7 @@ describe("probeService", () => {
|
||||
});
|
||||
|
||||
test("findOneBy by invalid name", async () => {
|
||||
const probeService: ProbeService = new ProbeService(database.getDatabase());
|
||||
const probeService: ProbeService = new ProbeService(database);
|
||||
const name: string = Faker.generateName();
|
||||
const probeVersion: Version = new Version("1.0.2");
|
||||
const key: ObjectID = ObjectID.generate();
|
||||
@ -193,7 +193,7 @@ describe("probeService", () => {
|
||||
});
|
||||
|
||||
test("select columns should work", async () => {
|
||||
const probeService: ProbeService = new ProbeService(database.getDatabase());
|
||||
const probeService: ProbeService = new ProbeService(database);
|
||||
const name: string = Faker.generateName();
|
||||
const probeVersion: Version = new Version("1.0.2");
|
||||
const key: ObjectID = ObjectID.generate();
|
||||
@ -232,7 +232,7 @@ describe("probeService", () => {
|
||||
});
|
||||
|
||||
test("findOneBy by key", async () => {
|
||||
const probeService: ProbeService = new ProbeService(database.getDatabase());
|
||||
const probeService: ProbeService = new ProbeService(database);
|
||||
const name: string = Faker.generateName();
|
||||
const probeVersion: Version = new Version("1.0.2");
|
||||
const key: ObjectID = ObjectID.generate();
|
||||
@ -281,7 +281,7 @@ describe("probeService", () => {
|
||||
});
|
||||
|
||||
test("findBy all entities", async () => {
|
||||
const probeService: ProbeService = new ProbeService(database.getDatabase());
|
||||
const probeService: ProbeService = new ProbeService(database);
|
||||
const name1: string = Faker.generateName();
|
||||
const probeVersion1: Version = new Version("1.0.2");
|
||||
const key1: ObjectID = ObjectID.generate();
|
||||
@ -356,7 +356,7 @@ describe("probeService", () => {
|
||||
});
|
||||
|
||||
test("findBy limit", async () => {
|
||||
const probeService: ProbeService = new ProbeService(database.getDatabase());
|
||||
const probeService: ProbeService = new ProbeService(database);
|
||||
const savedProbes: Array<Probe> = [];
|
||||
|
||||
for (let i: number = 0; i < 20; i++) {
|
||||
@ -421,7 +421,7 @@ describe("probeService", () => {
|
||||
});
|
||||
|
||||
test("findBy skip", async () => {
|
||||
const probeService: ProbeService = new ProbeService(database.getDatabase());
|
||||
const probeService: ProbeService = new ProbeService(database);
|
||||
const savedProbes: Array<Probe> = [];
|
||||
|
||||
for (let i: number = 0; i < 20; i++) {
|
||||
@ -486,7 +486,7 @@ describe("probeService", () => {
|
||||
});
|
||||
|
||||
test("delete probe by query", async () => {
|
||||
const probeService: ProbeService = new ProbeService(database.getDatabase());
|
||||
const probeService: ProbeService = new ProbeService(database);
|
||||
const name: string = Faker.generateName();
|
||||
const probeVersion: Version = new Version("1.0.2");
|
||||
const key: ObjectID = ObjectID.generate();
|
||||
@ -533,7 +533,7 @@ describe("probeService", () => {
|
||||
});
|
||||
|
||||
test("update probe by query", async () => {
|
||||
const probeService: ProbeService = new ProbeService(database.getDatabase());
|
||||
const probeService: ProbeService = new ProbeService(database);
|
||||
const name: string = Faker.generateName();
|
||||
const probeVersion: Version = new Version("1.0.2");
|
||||
const key: ObjectID = ObjectID.generate();
|
||||
@ -584,7 +584,7 @@ describe("probeService", () => {
|
||||
});
|
||||
|
||||
test("update probe by query", async () => {
|
||||
const probeService: ProbeService = new ProbeService(database.getDatabase());
|
||||
const probeService: ProbeService = new ProbeService(database);
|
||||
const name: string = Faker.generateName();
|
||||
const probeVersion: Version = new Version("1.0.2");
|
||||
const key: ObjectID = ObjectID.generate();
|
||||
@ -637,7 +637,7 @@ describe("probeService", () => {
|
||||
});
|
||||
|
||||
test("slugify column", async () => {
|
||||
const probeService: ProbeService = new ProbeService(database.getDatabase());
|
||||
const probeService: ProbeService = new ProbeService(database);
|
||||
const name: string = Faker.generateName();
|
||||
const probeVersion: Version = new Version("1.0.2");
|
||||
const key: ObjectID = ObjectID.generate();
|
||||
@ -660,7 +660,7 @@ describe("probeService", () => {
|
||||
});
|
||||
|
||||
test("add user to createdBy column", async () => {
|
||||
const probeService: ProbeService = new ProbeService(database.getDatabase());
|
||||
const probeService: ProbeService = new ProbeService(database);
|
||||
const user: User = await UserTestService.generateRandomUser().data.save();
|
||||
|
||||
const name: string = Faker.generateName();
|
||||
@ -708,7 +708,7 @@ describe("probeService", () => {
|
||||
});
|
||||
|
||||
test("include user in relation", async () => {
|
||||
const probeService: ProbeService = new ProbeService(database.getDatabase());
|
||||
const probeService: ProbeService = new ProbeService(database);
|
||||
const user: User = await UserTestService.generateRandomUser().data.save();
|
||||
|
||||
const name: string = Faker.generateName();
|
||||
@ -764,7 +764,7 @@ describe("probeService", () => {
|
||||
});
|
||||
|
||||
test("find a probe by user relation", async () => {
|
||||
const probeService: ProbeService = new ProbeService(database.getDatabase());
|
||||
const probeService: ProbeService = new ProbeService(database);
|
||||
const user: User = await UserTestService.generateRandomUser().data.save();
|
||||
|
||||
const name: string = Faker.generateName();
|
||||
|
@ -22,10 +22,10 @@ jest.mock("../../../Server/Infrastructure/PostgresDatabase", () => {
|
||||
default: actualModule.default,
|
||||
PostgresAppInstance: {
|
||||
getDataSource: () => {
|
||||
return testDatabase.getDatabase().getDataSource();
|
||||
return testDatabase.getDataSource();
|
||||
},
|
||||
isConnected: () => {
|
||||
return testDatabase.getDatabase().isConnected();
|
||||
return testDatabase.isConnected();
|
||||
},
|
||||
},
|
||||
};
|
||||
|
@ -38,10 +38,10 @@ jest.mock("../../../Server/Infrastructure/PostgresDatabase", () => {
|
||||
default: actualModule.default,
|
||||
PostgresAppInstance: {
|
||||
getDataSource: () => {
|
||||
return testDatabase.getDatabase().getDataSource();
|
||||
return testDatabase;
|
||||
},
|
||||
isConnected: () => {
|
||||
return testDatabase.getDatabase().isConnected();
|
||||
return testDatabase.isConnected();
|
||||
},
|
||||
},
|
||||
};
|
||||
@ -64,7 +64,7 @@ describe("TeamMemberService", () => {
|
||||
beforeEach(async () => {
|
||||
await testDatabase.createAndConnect();
|
||||
|
||||
teamMemberService = new TeamMemberService(testDatabase.getDatabase());
|
||||
teamMemberService = new TeamMemberService(testDatabase);
|
||||
|
||||
user = UserServiceHelper.generateRandomUser().data;
|
||||
user = await user.save();
|
||||
|
@ -2,50 +2,30 @@ import PostgresDatabase, {
|
||||
DatabaseSource,
|
||||
DatabaseSourceOptions,
|
||||
} from "../../../Server/Infrastructure/PostgresDatabase";
|
||||
import { newDb } from "pg-mem";
|
||||
import logger from "../../../Server/Utils/Logger";
|
||||
import getTestDataSourceOptions from "../../../Server/Infrastructure/Postgres/TestDataSourceOptions";
|
||||
|
||||
export default class DatabaseConnect {
|
||||
private database!: PostgresDatabase;
|
||||
|
||||
public constructor() {
|
||||
this.database = new PostgresDatabase();
|
||||
export default class TestDatabase extends PostgresDatabase {
|
||||
public async createAndConnect(): Promise<void> {
|
||||
const testDatasourceOptions = getTestDataSourceOptions();
|
||||
await this.connect(testDatasourceOptions);
|
||||
}
|
||||
|
||||
public getDatabase(): PostgresDatabase {
|
||||
return this.database;
|
||||
}
|
||||
|
||||
public async createAndConnect(): Promise<DatabaseSource> {
|
||||
const dataSourceOptions: DatabaseSourceOptions =
|
||||
await this.createDatabase();
|
||||
return await this.connectDatabase(dataSourceOptions);
|
||||
public override async connect(
|
||||
dataSourceOptions: DatabaseSourceOptions,
|
||||
): Promise<DatabaseSource> {
|
||||
const db = newDb();
|
||||
const dataSource: DatabaseSource =
|
||||
db.adapters.createTypeormDataSource(dataSourceOptions);
|
||||
logger.debug("Postgres Database Connected");
|
||||
this.dataSource = dataSource;
|
||||
return dataSource;
|
||||
}
|
||||
|
||||
public async disconnectAndDropDatabase(): Promise<void> {
|
||||
await this.disconnectDatabase();
|
||||
await this.dropDatabase();
|
||||
}
|
||||
|
||||
public async createDatabase(): Promise<DatabaseSourceOptions> {
|
||||
const dataSourceOptions: DatabaseSourceOptions =
|
||||
this.database.getTestDatasourceOptions();
|
||||
this.database.createDatabase();
|
||||
|
||||
return dataSourceOptions;
|
||||
}
|
||||
public async connectDatabase(
|
||||
dataSourceOptions: DatabaseSourceOptions,
|
||||
): Promise<DatabaseSource> {
|
||||
const connection: DatabaseSource =
|
||||
await this.database.connect(dataSourceOptions);
|
||||
await connection.synchronize();
|
||||
return connection;
|
||||
}
|
||||
|
||||
public async disconnectDatabase(): Promise<void> {
|
||||
await this.database.disconnect();
|
||||
}
|
||||
|
||||
public async dropDatabase(): Promise<void> {
|
||||
await this.database.dropDatabase();
|
||||
// Drop the database. Since this is the in-mem db, it will be destroyed.
|
||||
}
|
||||
}
|
||||
|
||||
export const PostgresAppInstance: TestDatabase = new TestDatabase();
|
||||
|
Loading…
Reference in New Issue
Block a user