P
Prisma•2w ago
Brokenwind

Prisma.skip on deeply nested objects

Hello, is there a way to Prisma.skip deeply nested objects? In this case:
const {
_sum: { netSales = 0 },
} = await prisma.returnsItem.aggregate({
where: {
inventory: {
product: {
categoryId: productCategoryId ?? Prisma.skip,
},
},
returns: {
transactionDate: {
gte: start.toDate(),
lte: end.toDate(),
},
salesChannelId: salesChannelId ?? Prisma.skip,
territory: {
id: territoryId ?? Prisma.skip,
district: {
id: districtId ?? Prisma.skip,
region: {
id: regionId ?? Prisma.skip,
},
},
},
},
},
_sum: {
netSales: true,
},
});
const {
_sum: { netSales = 0 },
} = await prisma.returnsItem.aggregate({
where: {
inventory: {
product: {
categoryId: productCategoryId ?? Prisma.skip,
},
},
returns: {
transactionDate: {
gte: start.toDate(),
lte: end.toDate(),
},
salesChannelId: salesChannelId ?? Prisma.skip,
territory: {
id: territoryId ?? Prisma.skip,
district: {
id: districtId ?? Prisma.skip,
region: {
id: regionId ?? Prisma.skip,
},
},
},
},
},
_sum: {
netSales: true,
},
});
When territoryId, districtId, and regionId are all undefined, it gives out this as a result:
territory: { id: dr {}, district: { id: dr {}, region: { id: dr {}, } } }
territory: { id: dr {}, district: { id: dr {}, region: { id: dr {}, } } }
Normally I would expect it to not consider the territory clause at all since all of it are skipped. For some reason, prisma doesn't skip this and returns null.
3 Replies
Prisma AI Help
Prisma AI Help•2w ago
You selected the carefully hand-crafted route. A dev artisan will respond soon. Meanwhile, the #ask-ai channel awaits if you're curious!
Nurul
Nurul•7d ago
Hey 👋 Can you please elaborate? What output are you currently getting and what output were you expecting?
Brokenwind
BrokenwindOP•7d ago
Hey, thanks for responding. In my specific example:
territory: {
id: territoryId ?? Prisma.skip,
district: {
id: districtId ?? Prisma.skip,
region: {
id: regionId ?? Prisma.skip,
},
},
},
territory: {
id: territoryId ?? Prisma.skip,
district: {
id: districtId ?? Prisma.skip,
region: {
id: regionId ?? Prisma.skip,
},
},
},
I have this structure in place so I can supply the territoryId, districtId, regionId when filtering. This allows me to have dynamic filtering and I want to supply these as needed. Just giving them territoryId, districtId, or regionId works in itself and filters through that. My issue is when I have those 3 undefined. It will use Prisma.skip as intended but it doesn't give me the result I want. The result that I want is to not filter any by territory at all. Instead, prisma (I believe) is filtering where territories are undefined (which shouldn't because I have strictUndefinedChecks) and I get no result. When I log the filters, this is what it looks like: territory: { id: dr {}, district: { id: dr {}, region: { id: dr {}, } } } Which is what prisma does when all three are undefined. Currently, this works for me:
territory:
territoryId || districtId || regionId
? {
id: territoryId ?? Prisma.skip,
district: {
id: districtId ?? Prisma.skip,
region: {
id: regionId ?? Prisma.skip,
},
},
}
: Prisma.skip,
territory:
territoryId || districtId || regionId
? {
id: territoryId ?? Prisma.skip,
district: {
id: districtId ?? Prisma.skip,
region: {
id: regionId ?? Prisma.skip,
},
},
}
: Prisma.skip,
In my opinion, if this is the structure:
territory: {
id: Prisma.skip,
district: {
id: Prisma.skip,
region: {
id: Prisma.skip,
},
},
}
territory: {
id: Prisma.skip,
district: {
id: Prisma.skip,
region: {
id: Prisma.skip,
},
},
}
It should just skip it. My workaround is ugly code and I wish I didn't need that.

Did you find this page helpful?