Skip to content

Commit ee06675

Browse files
🚀 feat: implement Prisma Client singleton pattern and enhance utils
This update introduces a singleton pattern for the Prisma Client in the utilities module, improving application performance by ensuring only one instance of the client exists. Additional enhancements include code cleanup and the introduction of global type declarations for better type safety and project maintainability. - Implemented a singleton pattern for Prisma Client to prevent multiple instances, which can lead to performance issues. - Introduced a `global.d.ts` file for declaring global types, such as the Prisma Client, enhancing type safety across the project. - Adjusted spacing and formatting in various utility functions for better readability and consistency.
1 parent f6df0d9 commit ee06675

2 files changed

Lines changed: 21 additions & 9 deletions

File tree

‎apps/web/app/utils.ts‎

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
import { Metadata } from "next";
22
import { getServerSession } from "next-auth";
3-
// import { getSession } from "next-auth/react";
3+
import { PostHog } from 'posthog-node';
4+
import { authOptions } from "./api/auth/[...nextauth]/AuthOptions";
5+
import { PrismaClient } from "@prisma/client";
6+
47
type OpenGraphType = "article" | "website" | "book" | "profile" | "music.song" | "music.album" | "music.playlist" | "music.radio_station" | "video.movie" | "video.episode" | "video.tv_show" | "video.other";
58

6-
import { PrismaClient } from '@prisma/client'
7-
export const prisma = new PrismaClient()
9+
10+
// Singleton pattern for Prisma Client
11+
let prisma: PrismaClient;
12+
if (!global.prisma) {
13+
global.prisma = new PrismaClient();
14+
}
15+
prisma = global.prisma;
16+
export { prisma };
817

918
import LoopsClient from "loops";
1019
export const loops = new LoopsClient(process.env.LOOPS_API_KEY!);
@@ -48,9 +57,6 @@ export function constructMetadata({
4857

4958
}
5059

51-
import { PostHog } from 'posthog-node';
52-
import { authOptions } from "./api/auth/[...nextauth]/AuthOptions";
53-
5460
export const posthog_serverside = new PostHog(
5561
process.env.NEXT_PUBLIC_POSTHOG_KEY!,
5662
{ host: process.env.NEXT_PUBLIC_POSTHOG_HOST },
@@ -92,7 +98,7 @@ interface WithSessionHandler {
9298

9399
export const getSession = async () => {
94100
const session = await getServerSession(authOptions) as Session;
95-
if(!session?.user) return { user: null } as unknown as Session;
101+
if (!session?.user) return { user: null } as unknown as Session;
96102
return session;
97103
};
98104

@@ -137,7 +143,7 @@ export const toUpperCamelCase = (input: string): string => {
137143
export const getAvatarFallbackInitials = (name: string): string => {
138144
const nameParts = name.split(' ');
139145
if (nameParts.length > 1) {
140-
return `${nameParts[0][0]}${nameParts[nameParts.length - 1][0]}`.toUpperCase();
146+
return `${nameParts[0][0]}${nameParts[nameParts.length - 1][0]}`.toUpperCase();
141147
}
142148
return nameParts[0][0].toUpperCase();
143-
}
149+
}

‎apps/web/global.d.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// global.d.ts
2+
import { PrismaClient } from '@prisma/client';
3+
4+
declare global {
5+
var prisma: PrismaClient | undefined;
6+
}

0 commit comments

Comments
 (0)