I have an prisma(postgresql)/trpc/next.js that uses referenced tables. So for example I have models that look like:
model Class {
id String @id
name String
lectures Lectures[]
}
model Lectures {
id String @id
text String
classId String
class Class @relation(fields: [classId], references: [id])
}
I've been trying to simply my next.js code, by just calling a "general" update on Class whenever any Class attribute is changed.
So something like:
const classUpdate = trpc.class.update.useMutation();
...
classUpdate.mutate(...myClass, name: newName);
I think i'd like to do something similar, where whenever there is a change to a Lecture for the Class (create/update/delete), i can simply call my "top-level" classUpdate.mutate(..)
and pass in the new complete JSON tree structure that represents the Class, Lectures (and any other references/nested references that i might add later).
So something like:
const classUpdate = trpc.class.update.useMutation();
...
classUpdate.mutate(...myClass, lectures: [ ...myClass.lectures, newLecture ]);
...
classUpdate.mutate(...myClass, lectures: [ ...myClass.lectures, changedExistingLecture ]);
Im unclear if I can do this - and if so, what would the trpc procedure definitions look like... Or if i should be doing this at all; Should I be making specific trpc routers for each model (so a lectureCreate.mutate(..)
, lectureUpdate.mutate(..)and
lectureDelete.mutate(..)`)
I might be approach this from a GQL mindset where I think of my models as 1 big JSON tree of fields and sub-objects (tables in my case), and want to just mutate that complete abstraction in one go, versus breaking each action up by model/relationship.