⚡ Replace updates with updateManys when possible
Easy performance win to avoid triggering SELECT query after an UPDATE
This commit is contained in:
@@ -36,11 +36,6 @@ export const getUsage = authenticatedProcedure
|
|||||||
|
|
||||||
const now = new Date()
|
const now = new Date()
|
||||||
const firstDayOfMonth = new Date(now.getFullYear(), now.getMonth(), 1)
|
const firstDayOfMonth = new Date(now.getFullYear(), now.getMonth(), 1)
|
||||||
const firstDayOfNextMonth = new Date(
|
|
||||||
now.getFullYear(),
|
|
||||||
now.getMonth() + 1,
|
|
||||||
1
|
|
||||||
)
|
|
||||||
const [
|
const [
|
||||||
totalChatsUsed,
|
totalChatsUsed,
|
||||||
{
|
{
|
||||||
@@ -62,7 +57,6 @@ export const getUsage = authenticatedProcedure
|
|||||||
hasStarted: true,
|
hasStarted: true,
|
||||||
createdAt: {
|
createdAt: {
|
||||||
gte: firstDayOfMonth,
|
gte: firstDayOfMonth,
|
||||||
lt: firstDayOfNextMonth,
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ const updateTokens =
|
|||||||
access_token: credentials.access_token,
|
access_token: credentials.access_token,
|
||||||
}
|
}
|
||||||
const { encryptedData, iv } = await encrypt(newCredentials)
|
const { encryptedData, iv } = await encrypt(newCredentials)
|
||||||
await prisma.credentials.update({
|
await prisma.credentials.updateMany({
|
||||||
where: { id: credentialsId },
|
where: { id: credentialsId },
|
||||||
data: { data: encryptedData, iv },
|
data: { data: encryptedData, iv },
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -216,7 +216,7 @@ const updateLastActivityDate = async (user: User) => {
|
|||||||
first.getDate() === second.getDate()
|
first.getDate() === second.getDate()
|
||||||
|
|
||||||
if (!datesAreOnSameDay(user.lastActivityAt, new Date()))
|
if (!datesAreOnSameDay(user.lastActivityAt, new Date()))
|
||||||
await prisma.user.update({
|
await prisma.user.updateMany({
|
||||||
where: { id: user.id },
|
where: { id: user.id },
|
||||||
data: { lastActivityAt: new Date() },
|
data: { lastActivityAt: new Date() },
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -104,7 +104,7 @@ const webhookHandler = async (req: NextApiRequest, res: NextApiResponse) => {
|
|||||||
data: { claimedAt: new Date() },
|
data: { claimedAt: new Date() },
|
||||||
})
|
})
|
||||||
|
|
||||||
await prisma.workspace.update({
|
await prisma.workspace.updateMany({
|
||||||
where: { id: workspaceId },
|
where: { id: workspaceId },
|
||||||
data: {
|
data: {
|
||||||
plan: Plan.CUSTOM,
|
plan: Plan.CUSTOM,
|
||||||
|
|||||||
@@ -95,13 +95,6 @@ export const sendMessage = publicProcedure
|
|||||||
const { messages, input, clientSideActions, newSessionState, logs } =
|
const { messages, input, clientSideActions, newSessionState, logs } =
|
||||||
await continueBotFlow(session.state)(message)
|
await continueBotFlow(session.state)(message)
|
||||||
|
|
||||||
await prisma.chatSession.updateMany({
|
|
||||||
where: { id: session.id },
|
|
||||||
data: {
|
|
||||||
state: newSessionState,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
const containsSetVariableClientSideAction = clientSideActions?.some(
|
const containsSetVariableClientSideAction = clientSideActions?.some(
|
||||||
(action) => 'setVariable' in action
|
(action) => 'setVariable' in action
|
||||||
)
|
)
|
||||||
@@ -114,6 +107,13 @@ export const sendMessage = publicProcedure
|
|||||||
)
|
)
|
||||||
await setResultAsCompleted(session.state.result.id)
|
await setResultAsCompleted(session.state.result.id)
|
||||||
|
|
||||||
|
await prisma.chatSession.updateMany({
|
||||||
|
where: { id: session.id },
|
||||||
|
data: {
|
||||||
|
state: newSessionState,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
return {
|
return {
|
||||||
messages,
|
messages,
|
||||||
input,
|
input,
|
||||||
|
|||||||
@@ -162,7 +162,7 @@ const saveVariableValueIfAny =
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const setResultAsCompleted = async (resultId: string) => {
|
export const setResultAsCompleted = async (resultId: string) => {
|
||||||
await prisma.result.update({
|
await prisma.result.updateMany({
|
||||||
where: { id: resultId },
|
where: { id: resultId },
|
||||||
data: { isCompleted: true },
|
data: { isCompleted: true },
|
||||||
})
|
})
|
||||||
@@ -235,7 +235,7 @@ const saveAnswer =
|
|||||||
}
|
}
|
||||||
|
|
||||||
const setResultAsStarted = async (resultId: string) => {
|
const setResultAsStarted = async (resultId: string) => {
|
||||||
await prisma.result.update({
|
await prisma.result.updateMany({
|
||||||
where: { id: resultId },
|
where: { id: resultId },
|
||||||
data: { hasStarted: true },
|
data: { hasStarted: true },
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ const updateResultVariables =
|
|||||||
].filter((variable) => isDefined(variable.value)) as VariableWithValue[]
|
].filter((variable) => isDefined(variable.value)) as VariableWithValue[]
|
||||||
|
|
||||||
if (result.id)
|
if (result.id)
|
||||||
await prisma.result.update({
|
await prisma.result.updateMany({
|
||||||
where: {
|
where: {
|
||||||
id: result.id,
|
id: result.id,
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ const updateTokens =
|
|||||||
access_token: credentials.access_token,
|
access_token: credentials.access_token,
|
||||||
}
|
}
|
||||||
const { encryptedData, iv } = await encrypt(newCredentials)
|
const { encryptedData, iv } = await encrypt(newCredentials)
|
||||||
await prisma.credentials.update({
|
await prisma.credentials.updateMany({
|
||||||
where: { id: credentialsId },
|
where: { id: credentialsId },
|
||||||
data: { data: encryptedData, iv },
|
data: { data: encryptedData, iv },
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
|||||||
const { webhookId } = typebot.groups
|
const { webhookId } = typebot.groups
|
||||||
.find(byId(groupId))
|
.find(byId(groupId))
|
||||||
?.blocks.find(byId(blockId)) as WebhookBlock
|
?.blocks.find(byId(blockId)) as WebhookBlock
|
||||||
await prisma.webhook.update({
|
await prisma.webhook.updateMany({
|
||||||
where: { id: webhookId },
|
where: { id: webhookId },
|
||||||
data: { url: null },
|
data: { url: null },
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
|||||||
const { webhookId } = typebot.groups
|
const { webhookId } = typebot.groups
|
||||||
.flatMap((g) => g.blocks)
|
.flatMap((g) => g.blocks)
|
||||||
.find(byId(blockId)) as WebhookBlock
|
.find(byId(blockId)) as WebhookBlock
|
||||||
await prisma.webhook.update({
|
await prisma.webhook.updateMany({
|
||||||
where: { id: webhookId },
|
where: { id: webhookId },
|
||||||
data: { url: null },
|
data: { url: null },
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
|||||||
typeof req.body === 'string' ? JSON.parse(req.body) : req.body
|
typeof req.body === 'string' ? JSON.parse(req.body) : req.body
|
||||||
) as Result
|
) as Result
|
||||||
const resultId = req.query.resultId as string
|
const resultId = req.query.resultId as string
|
||||||
const result = await prisma.result.update({
|
const result = await prisma.result.updateMany({
|
||||||
where: { id: resultId },
|
where: { id: resultId },
|
||||||
data,
|
data,
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
generator client {
|
generator client {
|
||||||
provider = "prisma-client-js"
|
provider = "prisma-client-js"
|
||||||
previewFeatures = ["extendedWhereUnique"]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
datasource db {
|
datasource db {
|
||||||
|
|||||||
@@ -144,7 +144,7 @@ const sendAlertIfLimitReached = async (
|
|||||||
chatsLimit,
|
chatsLimit,
|
||||||
url: `https://app.typebot.io/typebots?workspaceId=${workspace.id}`,
|
url: `https://app.typebot.io/typebots?workspaceId=${workspace.id}`,
|
||||||
})
|
})
|
||||||
await prisma.workspace.update({
|
await prisma.workspace.updateMany({
|
||||||
where: { id: workspace.id },
|
where: { id: workspace.id },
|
||||||
data: { chatsLimitFirstEmailSentAt: new Date() },
|
data: { chatsLimitFirstEmailSentAt: new Date() },
|
||||||
})
|
})
|
||||||
@@ -168,7 +168,7 @@ const sendAlertIfLimitReached = async (
|
|||||||
chatsLimit,
|
chatsLimit,
|
||||||
url: `https://app.typebot.io/typebots?workspaceId=${workspace.id}`,
|
url: `https://app.typebot.io/typebots?workspaceId=${workspace.id}`,
|
||||||
})
|
})
|
||||||
await prisma.workspace.update({
|
await prisma.workspace.updateMany({
|
||||||
where: { id: workspace.id },
|
where: { id: workspace.id },
|
||||||
data: { chatsLimitSecondEmailSentAt: new Date() },
|
data: { chatsLimitSecondEmailSentAt: new Date() },
|
||||||
})
|
})
|
||||||
@@ -179,7 +179,7 @@ const sendAlertIfLimitReached = async (
|
|||||||
|
|
||||||
if (totalChatsUsed > chatsLimit * 3 && workspace.plan === Plan.FREE) {
|
if (totalChatsUsed > chatsLimit * 3 && workspace.plan === Plan.FREE) {
|
||||||
console.log(`Automatically quarantine workspace ${workspace.id}...`)
|
console.log(`Automatically quarantine workspace ${workspace.id}...`)
|
||||||
await prisma.workspace.update({
|
await prisma.workspace.updateMany({
|
||||||
where: { id: workspace.id },
|
where: { id: workspace.id },
|
||||||
data: { isQuarantined: true },
|
data: { isQuarantined: true },
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user