🗃️ Write faster prisma queries

This commit is contained in:
Baptiste Arnaud
2022-12-08 11:02:52 +01:00
parent fe8a531715
commit 7eac2c7110
23 changed files with 235 additions and 134 deletions

View File

@@ -1,6 +1,6 @@
import { getLinkedTypebots } from '@/features/blocks/logic/typebotLink/api'
import prisma from '@/lib/prisma'
import { canReadTypebot } from '@/utils/api/dbRules'
import { canReadTypebots } from '@/utils/api/dbRules'
import { authenticatedProcedure } from '@/utils/server/trpc'
import { TRPCError } from '@trpc/server'
import { Typebot, Webhook } from 'models'
@@ -40,7 +40,7 @@ export const getResultExampleProcedure = authenticatedProcedure
)
.query(async ({ input: { typebotId, blockId }, ctx: { user } }) => {
const typebot = (await prisma.typebot.findFirst({
where: canReadTypebot(typebotId, user),
where: canReadTypebots(typebotId, user),
select: {
groups: true,
edges: true,

View File

@@ -1,5 +1,5 @@
import prisma from '@/lib/prisma'
import { canReadTypebot } from '@/utils/api/dbRules'
import { canReadTypebots } from '@/utils/api/dbRules'
import { authenticatedProcedure } from '@/utils/server/trpc'
import { TRPCError } from '@trpc/server'
import { Group, Typebot, Webhook, WebhookBlock } from 'models'
@@ -36,7 +36,7 @@ export const listWebhookBlocksProcedure = authenticatedProcedure
)
.query(async ({ input: { typebotId }, ctx: { user } }) => {
const typebot = (await prisma.typebot.findFirst({
where: canReadTypebot(typebotId, user),
where: canReadTypebots(typebotId, user),
select: {
groups: true,
webhooks: true,

View File

@@ -1,5 +1,5 @@
import prisma from '@/lib/prisma'
import { canWriteTypebot } from '@/utils/api/dbRules'
import { canWriteTypebots } from '@/utils/api/dbRules'
import { authenticatedProcedure } from '@/utils/server/trpc'
import { TRPCError } from '@trpc/server'
import { Typebot, Webhook, WebhookBlock } from 'models'
@@ -31,7 +31,7 @@ export const subscribeWebhookProcedure = authenticatedProcedure
)
.query(async ({ input: { typebotId, blockId, url }, ctx: { user } }) => {
const typebot = (await prisma.typebot.findFirst({
where: canWriteTypebot(typebotId, user),
where: canWriteTypebots(typebotId, user),
select: {
groups: true,
webhooks: true,

View File

@@ -1,5 +1,5 @@
import prisma from '@/lib/prisma'
import { canWriteTypebot } from '@/utils/api/dbRules'
import { canWriteTypebots } from '@/utils/api/dbRules'
import { authenticatedProcedure } from '@/utils/server/trpc'
import { TRPCError } from '@trpc/server'
import { Typebot, Webhook, WebhookBlock } from 'models'
@@ -30,7 +30,7 @@ export const unsubscribeWebhookProcedure = authenticatedProcedure
)
.query(async ({ input: { typebotId, blockId }, ctx: { user } }) => {
const typebot = (await prisma.typebot.findFirst({
where: canWriteTypebot(typebotId, user),
where: canWriteTypebots(typebotId, user),
select: {
groups: true,
webhooks: true,

View File

@@ -1,5 +1,5 @@
import prisma from '@/lib/prisma'
import { canWriteTypebot } from '@/utils/api/dbRules'
import { canWriteTypebots } from '@/utils/api/dbRules'
import { deleteFiles } from '@/utils/api/storage'
import { User, Prisma } from 'db'
import { InputBlockType, Typebot } from 'models'
@@ -14,7 +14,7 @@ export const archiveResults = async ({
resultsFilter?: Prisma.ResultWhereInput
}) => {
const typebot = await prisma.typebot.findFirst({
where: canWriteTypebot(typebotId, user),
where: canWriteTypebots(typebotId, user),
select: { groups: true },
})
if (!typebot) return { success: false }

View File

@@ -1,4 +1,4 @@
import { canWriteTypebot } from '@/utils/api/dbRules'
import { canWriteTypebots } from '@/utils/api/dbRules'
import { authenticatedProcedure } from '@/utils/server/trpc'
import { TRPCError } from '@trpc/server'
import { z } from 'zod'
@@ -34,7 +34,7 @@ export const deleteResultsProcedure = authenticatedProcedure
user,
resultsFilter: {
id: (idsArray?.length ?? 0) > 0 ? { in: idsArray } : undefined,
typebot: canWriteTypebot(typebotId, user),
typebot: canWriteTypebots(typebotId, user),
},
})

View File

@@ -1,5 +1,5 @@
import prisma from '@/lib/prisma'
import { canReadTypebot } from '@/utils/api/dbRules'
import { canReadTypebots } from '@/utils/api/dbRules'
import { authenticatedProcedure } from '@/utils/server/trpc'
import { logSchema } from 'models'
import { z } from 'zod'
@@ -22,9 +22,14 @@ export const getResultLogsProcedure = authenticatedProcedure
)
.output(z.object({ logs: z.array(logSchema) }))
.query(async ({ input: { typebotId, resultId }, ctx: { user } }) => {
const typebot = await prisma.typebot.findFirst({
where: canReadTypebots(typebotId, user),
select: { id: true },
})
if (!typebot) throw new Error('Typebot not found')
const logs = await prisma.log.findMany({
where: {
result: { id: resultId, typebot: canReadTypebot(typebotId, user) },
result: { id: resultId, typebotId: typebot.id },
},
})

View File

@@ -1,5 +1,5 @@
import prisma from '@/lib/prisma'
import { canReadTypebot } from '@/utils/api/dbRules'
import { canReadTypebots } from '@/utils/api/dbRules'
import { authenticatedProcedure } from '@/utils/server/trpc'
import { TRPCError } from '@trpc/server'
import { ResultWithAnswers, resultWithAnswersSchema } from 'models'
@@ -38,11 +38,17 @@ export const getResultsProcedure = authenticatedProcedure
message: 'limit must be between 1 and 200',
})
const { cursor } = input
const typebot = await prisma.typebot.findFirst({
where: canReadTypebots(input.typebotId, user),
select: { id: true },
})
if (!typebot)
throw new TRPCError({ code: 'NOT_FOUND', message: 'Typebot not found' })
const results = (await prisma.result.findMany({
take: limit + 1,
cursor: cursor ? { id: cursor } : undefined,
where: {
typebot: canReadTypebot(input.typebotId, user),
typebotId: typebot.id,
answers: { some: {} },
},
orderBy: {