Skip to content

Commit

Permalink
⚡ Replace updates with updateManys when possible
Browse files Browse the repository at this point in the history
Easy performance win to avoid triggering SELECT query after an UPDATE
  • Loading branch information
baptisteArno committed Jul 16, 2023
1 parent 12ce4eb commit 3426d66
Show file tree
Hide file tree
Showing 13 changed files with 21 additions and 28 deletions.
6 changes: 0 additions & 6 deletions apps/builder/src/features/billing/api/getUsage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
{
Expand All @@ -62,7 +57,6 @@ export const getUsage = authenticatedProcedure
hasStarted: true,
createdAt: {
gte: firstDayOfMonth,
lt: firstDayOfNextMonth,
},
},
}),
Expand Down
2 changes: 1 addition & 1 deletion apps/builder/src/lib/googleSheets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
})
Expand Down
2 changes: 1 addition & 1 deletion apps/builder/src/pages/api/auth/[...nextauth].ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() },
})
Expand Down
2 changes: 1 addition & 1 deletion apps/builder/src/pages/api/stripe/webhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
14 changes: 7 additions & 7 deletions apps/viewer/src/features/chat/api/sendMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions apps/viewer/src/features/chat/helpers/continueBotFlow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
})
Expand Down Expand Up @@ -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 },
})
Expand Down
2 changes: 1 addition & 1 deletion apps/viewer/src/features/variables/updateVariables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down
2 changes: 1 addition & 1 deletion apps/viewer/src/lib/google-sheets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
Expand Down
3 changes: 1 addition & 2 deletions packages/prisma/mysql/schema.prisma
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
generator client {
provider = "prisma-client-js"
previewFeatures = ["extendedWhereUnique"]
provider = "prisma-client-js"
}

datasource db {
Expand Down
6 changes: 3 additions & 3 deletions packages/scripts/sendAlertEmails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() },
})
Expand All @@ -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() },
})
Expand All @@ -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 },
})
Expand Down

0 comments on commit 3426d66

Please sign in to comment.