All files / src/lib prisma.ts

100% Statements 9/9
100% Branches 6/6
100% Functions 2/2
100% Lines 9/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29          4x                 23x 6x 1x   5x 3x   5x 4x   5x        
import { Prisma, PrismaClient } from "../prisma/client";
import { withAccelerate } from "@prisma/extension-accelerate";
 
// Helper to instantiate a PrismaClient extended with accelerate
function createDbClient(url: string) {
  return new PrismaClient({ accelerateUrl: url }).$extends(withAccelerate());
}
export type DbClient = ReturnType<typeof createDbClient>;
 
// Cache Prisma clients by their connection URL on the global object
declare global {
  var prismaClients: Record<string, DbClient> | undefined;
}
 
export const getDb = (DATABASE_URL: string): DbClient => {
  if (!DATABASE_URL) {
    throw new Error("Missing DATABASE_URL configuration");
  }
  if (!globalThis.prismaClients) {
    globalThis.prismaClients = {};
  }
  if (!globalThis.prismaClients[DATABASE_URL]) {
    globalThis.prismaClients[DATABASE_URL] = createDbClient(DATABASE_URL);
  }
  return globalThis.prismaClients[DATABASE_URL]!;
};
 
export { Prisma };