Change query typing to respect where null, or where not null

Hello! I am wondering if there is a solution for my current case that would greatly improve my code. I have several tables that have 2 states: in reservation, or not in reservation. When a row is in reservation, it does not have the links (or relationships) to other tables as it does when not in reservation. For this reason, multiple relationship must be nullable, see screenshot for an example. I would love to be able to do something like this:
const payments = await ctx.prisma.$primary().payment.findMany({
where: {
relation: {
isNot: null,
},
}
});
const payments = await ctx.prisma.$primary().payment.findMany({
where: {
relation: {
isNot: null,
},
}
});
and have the typing of payments to have its key relation not nullable because I have validated in the where that it cannot be null. So the typing would end up to be :
const payments: {
id: number;
relation: Relation;
}[]
const payments: {
id: number;
relation: Relation;
}[]
No description
2 Replies
jonfanz
jonfanz4mo ago
Sorry, I'm a bit confused. You mention that the relation key is not nullable, but it is in your schema above. Regardless, could you do this?
const payments = await
ctx.prisma.$primary().payment.findMany({
where: {
relationId: {
not: null
},
}
});
const payments = await
ctx.prisma.$primary().payment.findMany({
where: {
relationId: {
not: null
},
}
});
FabulouSebas
FabulouSebas4mo ago
Hello sir, very sorry for the late response, I will ensure fast responses for the rest of the week! Let me clarify, I do not think I was clear enough! My issue is really just about the typing returned by a find (many or single). In my database, the relation is nullable. Thus, fetching the payments from my database, the typing of a single payment's relationId property payment.relationId would be number | null because it is nullable. This means I need to make sure payment.relationId is not null before using it. In my case, I know for a fact that after the payments are successful, the payments have a relationId. I would want to be able to do where: { relationId: { not: null }, } and have the typing of payment.relationId to be number. Since in the where, I ensure relationId is not null, I would want the typing to reflect that as well
Want results from more Discord servers?
Add your server