From 02643214ae8da64f1c186d963935fbd15ffb91a2 Mon Sep 17 00:00:00 2001 From: Ismail Pelaseyed Date: Tue, 11 Apr 2023 15:42:00 +0200 Subject: [PATCH] Move `Prisma` client to outer scope in API routes --- pages/api/chatbots/index.js | 3 ++- pages/api/v1/chatbots/[chatbotId]/index.js | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/pages/api/chatbots/index.js b/pages/api/chatbots/index.js index 8d49205..fa3846e 100644 --- a/pages/api/chatbots/index.js +++ b/pages/api/chatbots/index.js @@ -2,8 +2,9 @@ import { PrismaClient } from "@prisma/client"; import { getServerSession } from "next-auth/next"; import { authOptions } from "../auth/[...nextauth]"; +const prismaClient = new PrismaClient(); + const chatbotsHandler = async (request, response) => { - const prismaClient = new PrismaClient(); const session = await getServerSession(request, response, authOptions); const user = await prismaClient.user.findUnique({ where: { email: session.user.email }, diff --git a/pages/api/v1/chatbots/[chatbotId]/index.js b/pages/api/v1/chatbots/[chatbotId]/index.js index 6c34fea..516075b 100644 --- a/pages/api/v1/chatbots/[chatbotId]/index.js +++ b/pages/api/v1/chatbots/[chatbotId]/index.js @@ -11,8 +11,9 @@ import { HumanChatMessage, AIChatMessage } from "langchain/schema"; import { PrismaClient } from "@prisma/client"; import { DEFAULT_PROMPT_TEMPLATE } from "@/lib/prompt-template"; +const prismaClient = new PrismaClient(); + const chatbotHandler = async (request, response) => { - const prismaClient = new PrismaClient(); const { chatbotId } = request.query; const { message } = request.body;