P
Prisma4mo ago
fotoflo

How to get the type of a compound query?

how would i get the type of this?
ts
const commentsPromise = prisma.urlComments.findMany({
where: {
urlId,
},
include: {
User: {
select: {
name: true,
email: true,
image: true,
},
},
},
orderBy: {
createdAt: "desc",
},
});

const statusCheckPromise = prisma.statusChecks.findMany({
where: {
urlId,
httpCode: {
notIn: [200, 301, 302, 303, 307, 308],
},
},
orderBy: {
createdAt: "desc",
},
});

const [comments, statusChecks] = await prisma.$transaction([
commentsPromise,
statusCheckPromise,
]);
ts
const commentsPromise = prisma.urlComments.findMany({
where: {
urlId,
},
include: {
User: {
select: {
name: true,
email: true,
image: true,
},
},
},
orderBy: {
createdAt: "desc",
},
});

const statusCheckPromise = prisma.statusChecks.findMany({
where: {
urlId,
httpCode: {
notIn: [200, 301, 302, 303, 307, 308],
},
},
orderBy: {
createdAt: "desc",
},
});

const [comments, statusChecks] = await prisma.$transaction([
commentsPromise,
statusCheckPromise,
]);
4 Replies
David
David4mo ago
What type exactly are you looking to get? The return of both statusCheckPromise as well as commentsPromise?
Enzonaki
Enzonaki4mo ago
In situations like that isnt just easier to infer types from the code? Or use typeof
fotoflo
fotoflo3mo ago
@Enzonaki how do you infer the types from the code? or use type of?
David
David3mo ago
type Comments = typeof comments but that only works if you define the type inside the function. If you want it outside its best to abstract the query to its own function.
async function getComments() {
return await prisma.statusChecks.findMany({
where: {
urlId,
httpCode: {
notIn: [200, 301, 302, 303, 307, 308],
},
},
orderBy: {
createdAt: "desc",
},
});
}

type Comments = Awaited<ReturnType<typeof getComments>>
async function getComments() {
return await prisma.statusChecks.findMany({
where: {
urlId,
httpCode: {
notIn: [200, 301, 302, 303, 307, 308],
},
},
orderBy: {
createdAt: "desc",
},
});
}

type Comments = Awaited<ReturnType<typeof getComments>>