prisma-redis-middleware
prisma-redis-middleware copied to clipboard
Nested relations cache (invalidate or exclude model)
I can't propose a solution for it right now, but I think it is a valuable issue to map over here once...
With a pseudo schema like
model User {
id String
nodes Node[]
}
model Note {
id String
text String
}
if I try to get something like
const getUserNotes = async (userId: string) => {
const { notes } = await prisma.findUnique({ where: { id: userId }, include: { notes: true } });
return notes
};
it would produce the key:
{
params: {
action: "findUnique",
args: { select: { notes: true }, where: { id: "ckyen2nkj0579g171l47kfz93" } },
dataPath: ["select", "notes"],
model: "User",
runInTransaction: false
}
}
then if I happen to delete a note, there would be no way for the cache to be able to know it was invalidated.
and excluding Note
model from cache wouldn't fix it as this query would be cached on User
model
I know it isn't so simple to fix this... but may be worth noticing it here
I've run into this recently, too, and is a bummer since it effectively means I need to exclude models that have nested relations from caching :/ open to helping where possible!
Can’t we invalidate all quereres that include the updated one? It’s a slash hammer approach but it would solve the underlying problem for now. It could be improved later on.
One workaround I've found for this in the meantime is running and update
on the parent resource with a blank update. This should be a DB no-op, but gets processed by the middlewhere and invalidates the nested resources due to how the keys are constructed. Tradeoff is an additional DB op, of course, but it does the trick until things are sorted w/ this use-case.
await prisma.thing.update({
where: {
id: myID // the id of the 'parent' resource,
},
data: {},
include: {
NestedThing: true,
},
});
This PR should help with that: https://github.com/Asjas/prisma-redis-middleware/pull/322