2
0

Replace updates with updateManys when possible

Easy performance win to avoid triggering SELECT query after an UPDATE
This commit is contained in:
Baptiste Arnaud
2023-07-16 18:52:30 +02:00
parent 12ce4eb01b
commit 3426d6689d
13 changed files with 21 additions and 28 deletions

View File

@@ -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,
},
},
}),

View File

@@ -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 },
})

View File

@@ -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() },
})

View File

@@ -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,

View File

@@ -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,

View File

@@ -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 },
})

View File

@@ -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,
},

View File

@@ -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 },
})

View File

@@ -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 },
})

View File

@@ -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 },
})

View File

@@ -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,
})