diff --git a/apps/dokploy/__test__/db/app-db-module.test.ts b/apps/dokploy/__test__/db/app-db-module.test.ts new file mode 100644 index 00000000000..6de2696dfb2 --- /dev/null +++ b/apps/dokploy/__test__/db/app-db-module.test.ts @@ -0,0 +1,9 @@ +import { db as sharedDb } from "@dokploy/server/db"; +import { describe, expect, it } from "vitest"; +import { db } from "@/server/db"; + +describe("@/server/db", () => { + it("reuses the shared client instead of opening a second pool", () => { + expect(db).toBe(sharedDb); + }); +}); diff --git a/apps/dokploy/server/db/index.ts b/apps/dokploy/server/db/index.ts index afc10bedf97..01848593548 100644 --- a/apps/dokploy/server/db/index.ts +++ b/apps/dokploy/server/db/index.ts @@ -1,38 +1,3 @@ -import { dbUrl } from "@dokploy/server/db/constants"; -import * as schema from "@dokploy/server/db/schema"; -import { and, eq } from "drizzle-orm"; -import { drizzle, type PostgresJsDatabase } from "drizzle-orm/postgres-js"; -import postgres from "postgres"; - -export { and, eq }; - -type Database = PostgresJsDatabase; -/** - * Evita problemas de redeclaración global en monorepos. - * No usamos `declare global`. - */ -const globalForDb = globalThis as unknown as { - db?: Database; -}; - -let dbConnection: Database; - -if (process.env.NODE_ENV === "production") { - // En producción no usamos global cache - dbConnection = drizzle(postgres(dbUrl), { - schema, - }); -} else { - // En desarrollo reutilizamos conexión para evitar múltiples conexiones - if (!globalForDb.db) { - globalForDb.db = drizzle(postgres(dbUrl), { - schema, - }); - } - - dbConnection = globalForDb.db; -} - -export const db: Database = dbConnection; - -export { dbUrl }; +// A pool of its own here gave the panel a second set of up to 10 Postgres +// connections; the shared module keeps one pool per process. +export { and, db, dbUrl, eq } from "@dokploy/server/db";