Skip to content

Commit 63d00d0

Browse files
authored
Merge pull request #190 from techulus/main
Reuse DB connection
2 parents 266c38a + ab39fa9 commit 63d00d0

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

lib/utils/useDatabase.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
11
import { sql } from "drizzle-orm";
22
import { drizzle } from "drizzle-orm/node-postgres";
33
import { err, ok, type Result } from "neverthrow";
4-
import { cache } from "react";
54
import type { Database } from "@/drizzle/types";
65
import * as schema from "../../drizzle/schema";
76
import { getOwner } from "./useOwner";
87

8+
const dbInstances = new Map<string, Database>();
9+
910
export function getDatabaseName(ownerId: string): Result<string, string> {
1011
if (!ownerId.startsWith("org_") && !ownerId.startsWith("user_")) {
1112
return err("Invalid owner ID");
1213
}
1314
return ok(ownerId.toLowerCase().replace(/ /g, "_"));
1415
}
1516

16-
export const database = cache(async (): Promise<Database> => {
17+
export async function database(): Promise<Database> {
1718
const { ownerId } = await getOwner();
1819
if (!ownerId) {
1920
throw new Error("Owner ID not found");
2021
}
2122

2223
return getDatabaseForOwner(ownerId);
23-
});
24+
}
2425

2526
export async function getDatabaseForOwner(ownerId: string): Promise<Database> {
2627
const databaseName = getDatabaseName(ownerId).match(
@@ -31,10 +32,18 @@ export async function getDatabaseForOwner(ownerId: string): Promise<Database> {
3132
);
3233

3334
const sslMode = process.env.DATABASE_SSL === "true" ? "?sslmode=require" : "";
35+
const connectionString = `${process.env.DATABASE_URL}/${databaseName}${sslMode}`;
3436

35-
return drizzle(`${process.env.DATABASE_URL}/${databaseName}${sslMode}`, {
36-
schema,
37-
});
37+
if (!dbInstances.has(ownerId)) {
38+
dbInstances.set(
39+
ownerId,
40+
drizzle(connectionString, {
41+
schema,
42+
}),
43+
);
44+
}
45+
46+
return dbInstances.get(ownerId)!;
3847
}
3948

4049
export async function deleteDatabase(ownerId: string) {

trpc/init.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import { auth } from "@clerk/nextjs/server";
22
import { initTRPC, TRPCError } from "@trpc/server";
3-
import { cache } from "react";
43
import superjson from "superjson";
54
import { ZodError } from "zod";
65
import { SearchService } from "@/lib/search";
76
import { database } from "@/lib/utils/useDatabase";
87
import { getOwner, getTimezone } from "@/lib/utils/useOwner";
98

10-
export const createTRPCContext = cache(async () => {
9+
export const createTRPCContext = async () => {
1110
const timezone = await getTimezone();
1211
const { orgSlug, ownerId } = await getOwner();
1312
const db = await database();
@@ -18,7 +17,7 @@ export const createTRPCContext = cache(async () => {
1817
ownerId,
1918
orgSlug,
2019
};
21-
});
20+
};
2221

2322
export type Context = Awaited<ReturnType<typeof createTRPCContext>>;
2423
// Avoid exporting the entire t-object

0 commit comments

Comments
 (0)