Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions apps/dokploy/__test__/db/app-db-module.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
41 changes: 3 additions & 38 deletions apps/dokploy/server/db/index.ts
Original file line number Diff line number Diff line change
@@ -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<typeof schema>;
/**
* 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";