From 3426d6689d03ea68116f893c9343c0a1a43e1595 Mon Sep 17 00:00:00 2001 From: Baptiste Arnaud Date: Sun, 16 Jul 2023 18:52:30 +0200 Subject: [PATCH] :zap: Replace updates with updateManys when possible Easy performance win to avoid triggering SELECT query after an UPDATE --- apps/builder/src/features/billing/api/getUsage.ts | 6 ------ apps/builder/src/lib/googleSheets.ts | 2 +- apps/builder/src/pages/api/auth/[...nextauth].ts | 2 +- apps/builder/src/pages/api/stripe/webhook.ts | 2 +- apps/viewer/src/features/chat/api/sendMessage.ts | 14 +++++++------- .../src/features/chat/helpers/continueBotFlow.ts | 4 ++-- .../src/features/variables/updateVariables.ts | 2 +- apps/viewer/src/lib/google-sheets.ts | 2 +- .../[blockId]/steps/[stepId]/unsubscribeWebhook.ts | 2 +- .../blocks/[blockId]/unsubscribeWebhook.ts | 2 +- .../api/typebots/[typebotId]/results/[resultId].ts | 2 +- packages/prisma/mysql/schema.prisma | 3 +-- packages/scripts/sendAlertEmails.ts | 6 +++--- 13 files changed, 21 insertions(+), 28 deletions(-) diff --git a/apps/builder/src/features/billing/api/getUsage.ts b/apps/builder/src/features/billing/api/getUsage.ts index 0b652f4f5b0..ccb50815244 100644 --- a/apps/builder/src/features/billing/api/getUsage.ts +++ b/apps/builder/src/features/billing/api/getUsage.ts @@ -36,11 +36,6 @@ export const getUsage = authenticatedProcedure const now = new Date() const firstDayOfMonth = new Date(now.getFullYear(), now.getMonth(), 1) - const firstDayOfNextMonth = new Date( - now.getFullYear(), - now.getMonth() + 1, - 1 - ) const [ totalChatsUsed, { @@ -62,7 +57,6 @@ export const getUsage = authenticatedProcedure hasStarted: true, createdAt: { gte: firstDayOfMonth, - lt: firstDayOfNextMonth, }, }, }), diff --git a/apps/builder/src/lib/googleSheets.ts b/apps/builder/src/lib/googleSheets.ts index dd569091a8e..3cf64c191bb 100644 --- a/apps/builder/src/lib/googleSheets.ts +++ b/apps/builder/src/lib/googleSheets.ts @@ -48,7 +48,7 @@ const updateTokens = access_token: credentials.access_token, } const { encryptedData, iv } = await encrypt(newCredentials) - await prisma.credentials.update({ + await prisma.credentials.updateMany({ where: { id: credentialsId }, data: { data: encryptedData, iv }, }) diff --git a/apps/builder/src/pages/api/auth/[...nextauth].ts b/apps/builder/src/pages/api/auth/[...nextauth].ts index 50e23c29d6b..8d416a0bac7 100644 --- a/apps/builder/src/pages/api/auth/[...nextauth].ts +++ b/apps/builder/src/pages/api/auth/[...nextauth].ts @@ -216,7 +216,7 @@ const updateLastActivityDate = async (user: User) => { first.getDate() === second.getDate() if (!datesAreOnSameDay(user.lastActivityAt, new Date())) - await prisma.user.update({ + await prisma.user.updateMany({ where: { id: user.id }, data: { lastActivityAt: new Date() }, }) diff --git a/apps/builder/src/pages/api/stripe/webhook.ts b/apps/builder/src/pages/api/stripe/webhook.ts index 21383aaf298..9f0ac4d5648 100644 --- a/apps/builder/src/pages/api/stripe/webhook.ts +++ b/apps/builder/src/pages/api/stripe/webhook.ts @@ -104,7 +104,7 @@ const webhookHandler = async (req: NextApiRequest, res: NextApiResponse) => { data: { claimedAt: new Date() }, }) - await prisma.workspace.update({ + await prisma.workspace.updateMany({ where: { id: workspaceId }, data: { plan: Plan.CUSTOM, diff --git a/apps/viewer/src/features/chat/api/sendMessage.ts b/apps/viewer/src/features/chat/api/sendMessage.ts index 59f7403f4df..daa0dda277c 100644 --- a/apps/viewer/src/features/chat/api/sendMessage.ts +++ b/apps/viewer/src/features/chat/api/sendMessage.ts @@ -95,13 +95,6 @@ export const sendMessage = publicProcedure const { messages, input, clientSideActions, newSessionState, logs } = await continueBotFlow(session.state)(message) - await prisma.chatSession.updateMany({ - where: { id: session.id }, - data: { - state: newSessionState, - }, - }) - const containsSetVariableClientSideAction = clientSideActions?.some( (action) => 'setVariable' in action ) @@ -114,6 +107,13 @@ export const sendMessage = publicProcedure ) await setResultAsCompleted(session.state.result.id) + await prisma.chatSession.updateMany({ + where: { id: session.id }, + data: { + state: newSessionState, + }, + }) + return { messages, input, diff --git a/apps/viewer/src/features/chat/helpers/continueBotFlow.ts b/apps/viewer/src/features/chat/helpers/continueBotFlow.ts index b011565a01b..09e35acd471 100644 --- a/apps/viewer/src/features/chat/helpers/continueBotFlow.ts +++ b/apps/viewer/src/features/chat/helpers/continueBotFlow.ts @@ -162,7 +162,7 @@ const saveVariableValueIfAny = } export const setResultAsCompleted = async (resultId: string) => { - await prisma.result.update({ + await prisma.result.updateMany({ where: { id: resultId }, data: { isCompleted: true }, }) @@ -235,7 +235,7 @@ const saveAnswer = } const setResultAsStarted = async (resultId: string) => { - await prisma.result.update({ + await prisma.result.updateMany({ where: { id: resultId }, data: { hasStarted: true }, }) diff --git a/apps/viewer/src/features/variables/updateVariables.ts b/apps/viewer/src/features/variables/updateVariables.ts index 8b927aebfd1..a4aa74ddb47 100644 --- a/apps/viewer/src/features/variables/updateVariables.ts +++ b/apps/viewer/src/features/variables/updateVariables.ts @@ -44,7 +44,7 @@ const updateResultVariables = ].filter((variable) => isDefined(variable.value)) as VariableWithValue[] if (result.id) - await prisma.result.update({ + await prisma.result.updateMany({ where: { id: result.id, }, diff --git a/apps/viewer/src/lib/google-sheets.ts b/apps/viewer/src/lib/google-sheets.ts index 78b2e330c67..77205e6bbfd 100644 --- a/apps/viewer/src/lib/google-sheets.ts +++ b/apps/viewer/src/lib/google-sheets.ts @@ -44,7 +44,7 @@ const updateTokens = access_token: credentials.access_token, } const { encryptedData, iv } = await encrypt(newCredentials) - await prisma.credentials.update({ + await prisma.credentials.updateMany({ where: { id: credentialsId }, data: { data: encryptedData, iv }, }) diff --git a/apps/viewer/src/pages/api/typebots/[typebotId]/blocks/[blockId]/steps/[stepId]/unsubscribeWebhook.ts b/apps/viewer/src/pages/api/typebots/[typebotId]/blocks/[blockId]/steps/[stepId]/unsubscribeWebhook.ts index 577264014fd..0e7dff4cc67 100644 --- a/apps/viewer/src/pages/api/typebots/[typebotId]/blocks/[blockId]/steps/[stepId]/unsubscribeWebhook.ts +++ b/apps/viewer/src/pages/api/typebots/[typebotId]/blocks/[blockId]/steps/[stepId]/unsubscribeWebhook.ts @@ -23,7 +23,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => { const { webhookId } = typebot.groups .find(byId(groupId)) ?.blocks.find(byId(blockId)) as WebhookBlock - await prisma.webhook.update({ + await prisma.webhook.updateMany({ where: { id: webhookId }, data: { url: null }, }) diff --git a/apps/viewer/src/pages/api/typebots/[typebotId]/blocks/[blockId]/unsubscribeWebhook.ts b/apps/viewer/src/pages/api/typebots/[typebotId]/blocks/[blockId]/unsubscribeWebhook.ts index dee67646e45..3cc12035b53 100644 --- a/apps/viewer/src/pages/api/typebots/[typebotId]/blocks/[blockId]/unsubscribeWebhook.ts +++ b/apps/viewer/src/pages/api/typebots/[typebotId]/blocks/[blockId]/unsubscribeWebhook.ts @@ -22,7 +22,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => { const { webhookId } = typebot.groups .flatMap((g) => g.blocks) .find(byId(blockId)) as WebhookBlock - await prisma.webhook.update({ + await prisma.webhook.updateMany({ where: { id: webhookId }, data: { url: null }, }) diff --git a/apps/viewer/src/pages/api/typebots/[typebotId]/results/[resultId].ts b/apps/viewer/src/pages/api/typebots/[typebotId]/results/[resultId].ts index 2eb40adc260..750aa4508eb 100644 --- a/apps/viewer/src/pages/api/typebots/[typebotId]/results/[resultId].ts +++ b/apps/viewer/src/pages/api/typebots/[typebotId]/results/[resultId].ts @@ -9,7 +9,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => { typeof req.body === 'string' ? JSON.parse(req.body) : req.body ) as Result const resultId = req.query.resultId as string - const result = await prisma.result.update({ + const result = await prisma.result.updateMany({ where: { id: resultId }, data, }) diff --git a/packages/prisma/mysql/schema.prisma b/packages/prisma/mysql/schema.prisma index aee67d04ab3..d33e604b412 100644 --- a/packages/prisma/mysql/schema.prisma +++ b/packages/prisma/mysql/schema.prisma @@ -1,6 +1,5 @@ generator client { - provider = "prisma-client-js" - previewFeatures = ["extendedWhereUnique"] + provider = "prisma-client-js" } datasource db { diff --git a/packages/scripts/sendAlertEmails.ts b/packages/scripts/sendAlertEmails.ts index 2c3ea3f2363..6b923c1654e 100644 --- a/packages/scripts/sendAlertEmails.ts +++ b/packages/scripts/sendAlertEmails.ts @@ -144,7 +144,7 @@ const sendAlertIfLimitReached = async ( chatsLimit, url: `https://app.typebot.io/typebots?workspaceId=${workspace.id}`, }) - await prisma.workspace.update({ + await prisma.workspace.updateMany({ where: { id: workspace.id }, data: { chatsLimitFirstEmailSentAt: new Date() }, }) @@ -168,7 +168,7 @@ const sendAlertIfLimitReached = async ( chatsLimit, url: `https://app.typebot.io/typebots?workspaceId=${workspace.id}`, }) - await prisma.workspace.update({ + await prisma.workspace.updateMany({ where: { id: workspace.id }, data: { chatsLimitSecondEmailSentAt: new Date() }, }) @@ -179,7 +179,7 @@ const sendAlertIfLimitReached = async ( if (totalChatsUsed > chatsLimit * 3 && workspace.plan === Plan.FREE) { console.log(`Automatically quarantine workspace ${workspace.id}...`) - await prisma.workspace.update({ + await prisma.workspace.updateMany({ where: { id: workspace.id }, data: { isQuarantined: true }, })