Greetings everyone! I am relatively new to prisma ...
# random
a
Greetings everyone! I am relatively new to prisma and wondering if I could get some highlight on the proper usage of prisma to kinda get match my expectations to the reality 🙂 Stack: apollo graphql + prisma + typegraphql (generating resolvers). schema.prisma: model SomeModel {… id… value Decimal…} model SomeAggregateModel {… id… aggregateValue Decimal…} business logic: (for example) - when SomeModel.value changes - do set SomeAggregateModel.aggregateValue=sum(all values from SomeModel.value) Use case • user in the frontend updates a value which gets updated in the backend through generated resolver UpdateSomeValueResolver • somehow the business logic gets triggered • upon update to SomeAggregateModel.aggregateValue the frontent gets notified and refreshes the display of aggregateValue to the user Questions: 1. Is it correct to expect the existence of ‘trigger’ mechanism which will initiate the business logic to run upon update to the SomeModel.value? 2. If such mechanism exists - would mind to point to some reading/manual/help/documentation reference as to how to enable this mechanism? 3. If such mechanism does NOT exist - what would be the most efficient way to implement the use case given the constraint of creating as less code as possible (i.e. in that case I guess there will have to be custom resolver I guess which front end will need to call to trigger the business logic of calculating the aggregatevalue) I would really appreciate advice on the use case above as I feel like the triggering must be there but so far not lucky enough to find it 🙈
v
Hello @Alexei Snisarenko thank you for posting your question and I glad that you're experimenting with Prisma. From your description this sounds like a use case for Prisma Middleware: https://www.prisma.io/docs/concepts/components/prisma-client/middleware @Joël , @Ryan might have more input for you!
a
Thank you @Vladi Stevanovic. It would really be helpful for me as I am trying to wrap my head around and grasp on this use case
r
@Alexei Snisarenko 👋 In this case, you can go for GraphQL subscriptions. This will make sure that any value that changes in your
SomeModel
for that you could perform an update in
SomeAggregateModel
. Another option is to use database triggers for the same. You can write a trigger in the Migration
.sql
file. This will be persisted in the database and would automatically run based on the condition that you provide in the trigger 🙂
a
Thank you @Ryan That’s where I was looking - subscriptions. Just wanted to make sure I’m digging in right direction. Still what would be your thought for is the prisma middleware useful for my case though?
r
Middleware’s would work as well in this case. You just would need to add a condition if the model being updated it the one you want.
a
Thanks for all the information!
🙌 1
r
Let me know how it goes and if you face any issues 🙂
🤝 1