feat(db): support postgresql database (#3643)

Co-authored-by: Meier Lukas <meierschlumpf@gmail.com>
This commit is contained in:
Yuichi Nakai
2025-08-30 03:30:03 +09:00
committed by GitHub
parent a4aa2aea90
commit 5168cba8e4
22 changed files with 3603 additions and 32 deletions

View File

@@ -2,7 +2,7 @@ import type { InferInsertModel } from "drizzle-orm";
import { objectEntries } from "@homarr/common";
import type { HomarrDatabase, HomarrDatabaseMysql } from "./driver";
import type { HomarrDatabase, HomarrDatabaseMysql, HomarrDatabasePostgresql } from "./driver";
import { env } from "./env";
import * as schema from "./schema";
@@ -10,6 +10,14 @@ type TableKey = {
[K in keyof typeof schema]: (typeof schema)[K] extends { _: { brand: "Table" } } ? K : never;
}[keyof typeof schema];
export function isMysql(): boolean {
return env.DB_DRIVER === "mysql2";
}
export function isPostgresql(): boolean {
return env.DB_DRIVER === "node-postgres";
}
export const createDbInsertCollectionForTransaction = <TTableKey extends TableKey>(
tablesInInsertOrder: TTableKey[],
) => {
@@ -35,8 +43,10 @@ export const createDbInsertCollectionForTransaction = <TTableKey extends TableKe
}
});
},
insertAllAsync: async (db: HomarrDatabaseMysql) => {
await db.transaction(async (transaction) => {
// We allow any database that supports async passed here but then fallback to mysql to prevent typescript errors
insertAllAsync: async (db: HomarrDatabaseMysql | HomarrDatabasePostgresql) => {
const innerDb = db as HomarrDatabaseMysql;
await innerDb.transaction(async (transaction) => {
for (const [key, values] of objectEntries(context)) {
if (values.length >= 1) {
// Below is actually the mysqlSchema when the driver is mysql
@@ -56,12 +66,18 @@ export const createDbInsertCollectionWithoutTransaction = <TTableKey extends Tab
return {
...collection,
insertAllAsync: async (db: HomarrDatabase) => {
if (env.DB_DRIVER !== "mysql2") {
insertAll(db);
return;
switch (env.DB_DRIVER) {
case "mysql2":
case "node-postgres":
// For mysql2 and node-postgres, we can use the async insertAllAsync method
await insertAllAsync(db as unknown as HomarrDatabaseMysql | HomarrDatabasePostgresql);
return;
default:
// For better-sqlite3, we need to use the synchronous insertAll method
// default assumes better-sqlite3. It's original implementation.
insertAll(db);
break;
}
await insertAllAsync(db as unknown as HomarrDatabaseMysql);
},
};
};