summary refs log tree commit diff
diff options
context:
space:
mode:
authorSamuel <34555296+Flam3rboy@users.noreply.github.com>2023-03-17 19:18:31 +0100
committerSamuel <34555296+Flam3rboy@users.noreply.github.com>2023-03-17 19:18:31 +0100
commit197f1aacd2dad264cf0cb415efeb57d1ea08c849 (patch)
treec42916ca4e449c9c85c3109fed3238c7e5f09323
parentperf: custom i18next middleware that only initializes when needed (diff)
downloadserver-197f1aacd2dad264cf0cb415efeb57d1ea08c849.tar.xz
feat: better-sqlite3 support
-rw-r--r--package.json1
-rw-r--r--src/util/util/Database.ts19
2 files changed, 18 insertions, 2 deletions
diff --git a/package.json b/package.json
index 03cc7981..101a5f53 100644
--- a/package.json
+++ b/package.json
@@ -112,6 +112,7 @@
 		"@fosscord/util": "dist/util"
 	},
 	"optionalDependencies": {
+		"better-sqlite3": "^8.2.0",
 		"erlpack": "^0.1.4",
 		"nodemailer-mailgun-transport": "^2.1.5",
 		"nodemailer-mailjet-transport": "github:n0script22/nodemailer-mailjet-transport",
diff --git a/src/util/util/Database.ts b/src/util/util/Database.ts
index 64d7ca14..e625ea9b 100644
--- a/src/util/util/Database.ts
+++ b/src/util/util/Database.ts
@@ -36,9 +36,17 @@ if (!process.env) {
 const dbConnectionString =
 	process.env.DATABASE || path.join(process.cwd(), "database.db");
 
-const DatabaseType = dbConnectionString.includes("://")
+let DatabaseType = dbConnectionString.includes("://")
 	? dbConnectionString.split(":")[0]?.replace("+srv", "")
 	: "sqlite";
+
+if (DatabaseType === "sqlite") {
+	try {
+		require("better-sqlite3");
+		DatabaseType = "better-sqlite3";
+		// eslint-disable-next-line no-empty
+	} catch (error) {}
+}
 const isSqlite = DatabaseType.includes("sqlite");
 
 const DataSourceOptions = new DataSource({
@@ -55,6 +63,7 @@ const DataSourceOptions = new DataSource({
 	supportBigNumbers: true,
 	name: "default",
 	migrations: [path.join(__dirname, "..", "migration", DatabaseType, "*.js")],
+	cache: true,
 });
 
 // Gets the existing database connection
@@ -77,7 +86,13 @@ export async function initDatabase(): Promise<DataSource> {
 	}
 
 	if (!process.env.DB_SYNC) {
-		const supported = ["mysql", "mariadb", "postgres", "sqlite"];
+		const supported = [
+			"mysql",
+			"mariadb",
+			"postgres",
+			"sqlite",
+			"better-sqlite3",
+		];
 		if (!supported.includes(DatabaseType)) {
 			console.log(
 				"[Database]" +