summary refs log tree commit diff
path: root/src/util/util/Database.ts
blob: 87ee212f9cc92d9185ae917f41cb2be2d2db56b4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import path from "path";
import "reflect-metadata";
import { DataSource } from "typeorm";
import { yellow, green, red } from "picocolors";

// UUID extension option is only supported with postgres
// We want to generate all id's with Snowflakes that's why we have our own BaseEntity class

var dbConnection: DataSource | undefined;
let dbConnectionString =
	process.env.DATABASE || path.join(process.cwd(), "database.db");

// Gets the existing database connection
export function getDatabase(): DataSource | null {
	// if (!dbConnection) throw new Error("Tried to get database before it was initialised");
	if (!dbConnection) return null;
	return dbConnection;
}

// Called once on server start
export async function initDatabase(): Promise<DataSource> {
	if (dbConnection) return dbConnection;

	const type = dbConnectionString.includes("://")
		? dbConnectionString.split(":")[0]?.replace("+srv", "")
		: "sqlite";
	const isSqlite = type.includes("sqlite");

	console.log(`[Database] ${yellow(`connecting to ${type} db`)}`);
	if (isSqlite) {
		console.log(
			`[Database] ${red(
				`You are running sqlite! Please keep in mind that we recommend setting up a dedicated database!`,
			)}`,
		);
	}

	const dataSource = new DataSource({
		//@ts-ignore type 'string' is not 'mysql' | 'sqlite' | 'mariadb' | etc etc
		type,
		charset: "utf8mb4",
		url: isSqlite ? undefined : dbConnectionString,
		database: isSqlite ? dbConnectionString : undefined,
		entities: ["dist/util/entities/*.js"],
		synchronize: false,
		logging: false,
		bigNumberStrings: false,
		supportBigNumbers: true,
		name: "default",
		migrations: ["dist/util/migrations/*.js"],
	});

	dbConnection = await dataSource.initialize();

	await dbConnection.runMigrations();
	console.log(`[Database] ${green("connected")}`);

	return dbConnection;
}

export { dbConnection };

export function closeDatabase() {
	dbConnection?.destroy();
}