Initial commit
This commit is contained in:
		
							
								
								
									
										21
									
								
								src/db/dataSource.migration.test.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								src/db/dataSource.migration.test.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,21 @@
 | 
			
		||||
import { randomUUID } from "crypto";
 | 
			
		||||
 | 
			
		||||
jest.mock("typeorm");
 | 
			
		||||
 | 
			
		||||
describe("src/db:dataSource.migration", () => {
 | 
			
		||||
    beforeEach(() => {
 | 
			
		||||
        jest.resetModules();
 | 
			
		||||
        jest.clearAllMocks();
 | 
			
		||||
 | 
			
		||||
        process.env["DB_HOST"] = randomUUID();
 | 
			
		||||
        process.env["DB_USER"] = randomUUID();
 | 
			
		||||
        process.env["DB_PASS"] = randomUUID();
 | 
			
		||||
        process.env["DB_NAME"] = randomUUID();
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    it("exports an initialized dataSource", async () => {
 | 
			
		||||
        const dut = (await import("./dataSource.migration")).default;
 | 
			
		||||
 | 
			
		||||
        expect(dut.initialize).toHaveBeenCalled();
 | 
			
		||||
    });
 | 
			
		||||
});
 | 
			
		||||
							
								
								
									
										3
									
								
								src/db/dataSource.migration.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								src/db/dataSource.migration.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,3 @@
 | 
			
		||||
import dataSource from "./dataSource";
 | 
			
		||||
 | 
			
		||||
export default dataSource;
 | 
			
		||||
							
								
								
									
										59
									
								
								src/db/dataSource.test.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										59
									
								
								src/db/dataSource.test.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,59 @@
 | 
			
		||||
import { randomUUID } from "crypto";
 | 
			
		||||
 | 
			
		||||
jest.mock("typeorm");
 | 
			
		||||
jest.mock("dotenv");
 | 
			
		||||
jest.mock("dotenv-expand");
 | 
			
		||||
 | 
			
		||||
describe("src/db:dataSource", () => {    
 | 
			
		||||
    beforeEach(() => {
 | 
			
		||||
        jest.resetModules();
 | 
			
		||||
        jest.clearAllMocks();
 | 
			
		||||
 | 
			
		||||
        process.env["DB_HOST"] = randomUUID();
 | 
			
		||||
        process.env["DB_USER"] = randomUUID();
 | 
			
		||||
        process.env["DB_PASS"] = randomUUID();
 | 
			
		||||
        process.env["DB_NAME"] = randomUUID();
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    it("throws an error if an environment variable is missing", async () => {
 | 
			
		||||
        delete process.env["DB_HOST"];
 | 
			
		||||
        process.env["DB_USER"] = randomUUID();
 | 
			
		||||
        process.env["DB_PASS"] = randomUUID();
 | 
			
		||||
        process.env["DB_NAME"] = randomUUID();
 | 
			
		||||
 | 
			
		||||
        await expect(async () => await import("./dataSource")).rejects.toThrowError(ReferenceError);
 | 
			
		||||
 | 
			
		||||
        jest.resetModules();
 | 
			
		||||
 | 
			
		||||
        process.env["DB_HOST"] = randomUUID();
 | 
			
		||||
        delete process.env["DB_USER"];
 | 
			
		||||
 | 
			
		||||
        await expect(async () => await import("./dataSource")).rejects.toThrowError(ReferenceError);
 | 
			
		||||
 | 
			
		||||
        jest.resetModules();
 | 
			
		||||
 | 
			
		||||
        process.env["DB_USER"] = randomUUID();
 | 
			
		||||
        delete process.env["DB_PASS"];
 | 
			
		||||
 | 
			
		||||
        await expect(async () => await import("./dataSource")).rejects.toThrowError(ReferenceError);
 | 
			
		||||
 | 
			
		||||
        jest.resetModules();
 | 
			
		||||
 | 
			
		||||
        process.env["DB_PASS"] = randomUUID();
 | 
			
		||||
        delete process.env["DB_NAME"];
 | 
			
		||||
 | 
			
		||||
        await expect(async () => await import("./dataSource")).rejects.toThrowError(ReferenceError);
 | 
			
		||||
 | 
			
		||||
        jest.resetModules();
 | 
			
		||||
 | 
			
		||||
        process.env["DB_NAME"] = randomUUID();
 | 
			
		||||
 | 
			
		||||
        expect(async () => await import("./dataSource")).not.toThrow();
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    it("exports an initialized dataSource", async () => {
 | 
			
		||||
        const dut = (await import("./dataSource")).default;
 | 
			
		||||
 | 
			
		||||
        expect(dut.initialize).toHaveBeenCalled();
 | 
			
		||||
    });
 | 
			
		||||
});
 | 
			
		||||
							
								
								
									
										37
									
								
								src/db/dataSource.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								src/db/dataSource.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,37 @@
 | 
			
		||||
import path from "path";
 | 
			
		||||
import { DataSource } from "typeorm";
 | 
			
		||||
import { config } from "dotenv";
 | 
			
		||||
import dotenvExpand from "dotenv-expand";
 | 
			
		||||
 | 
			
		||||
const env = config();
 | 
			
		||||
dotenvExpand.expand(env);
 | 
			
		||||
 | 
			
		||||
const host = process.env["DB_HOST"];
 | 
			
		||||
const username = process.env["DB_USER"];
 | 
			
		||||
const password = process.env["DB_PASS"];
 | 
			
		||||
const database = process.env["DB_NAME"];
 | 
			
		||||
 | 
			
		||||
if (!host) throw new ReferenceError("Environment variable DB_HOST is missing");
 | 
			
		||||
if (!password) throw new ReferenceError("Environment variable DB_PASS is missing");
 | 
			
		||||
if (!username) throw new ReferenceError("Environment variable DB_USER is missing");
 | 
			
		||||
if (!database) throw new ReferenceError("Environment variable DB_NAME is missing");
 | 
			
		||||
 | 
			
		||||
const dataSource = new DataSource({
 | 
			
		||||
    type: "postgres",
 | 
			
		||||
    host: host,
 | 
			
		||||
    port: 5432,
 | 
			
		||||
    username: username,
 | 
			
		||||
    password: password,
 | 
			
		||||
    database: database,
 | 
			
		||||
    migrationsRun: true,
 | 
			
		||||
    migrations: [path.join(__dirname, "/migrations/*")],
 | 
			
		||||
    entities: [],
 | 
			
		||||
    migrationsTransactionMode: "each",
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
const initPromise = dataSource.initialize();
 | 
			
		||||
 | 
			
		||||
export default dataSource;
 | 
			
		||||
export {
 | 
			
		||||
    initPromise
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user