How to automatically trigger an event by a particular date using Postgres and GraphQL?


#1

I’m looking to create a booking system using GraphQL, Prisma, Postgres, and React that allows a user to rent a property from date A to date B. When the due date, B, arrives, I want the database to do two things:

  1. Trigger an event updating its Overdue field from false to true when the due date is passed. My current workaround solution is to have a query that fetches the due date, compares it to the current date and if the current date is passed the due date, mutate the Overdue field from false to true. But, the problem with this solution is that it only works when the query is performed, which means the user has to necessarily visit the website.
  2. This second thing is tied into the first one, but how do I send a proactive alert from the database to the user with email notifying the overdue date?

#2

Hi Kevv,

I was just looking into this myself. It depends on what server you are using. I’m using Node.js and found node-schedule. What you can do is create a recurring job, let’s say every hour. This job can check if the due date is reached, then mutate the Overdue field, or if the due date is nearly reached, trigger the mutation to send an alert.