Hello, how do I implement something like this: Wh...
# orm-help
i
Hello, how do I implement something like this: When a user send a new message from the client, I need to count them at the end of the month and give user how much messages he sent during this month. I understand that I’ll need to aggregate and count them by date range, but the problem is that how do I trigger such an event. Any advice on how to implement something like that?
j
If this is something you will notify the user about (without them asking for it) it sounds like a CRON job with some way to push the info to the user. If this is a native app, most of them come with some way of scheduling certain tasks. Anyways you can specify filters to the primsa queries. Does that answer your question? I'm not 100% I got what you were asking for.
i
@Jenkins yeah I guess, I forgot that I can run it at the first day of each month instead of at the end of each month. But I guess my problem will be that for example: I have a lot of users and each of them need to receive information on how much messages he’s sent on previous month. Basically I can just do a queue something like
bull
for example and add a task to run that aggregation.
j
You can define any queries you'd like and just run that, yeah. Queries between your GQL server and the Prisma backend does not need to match 1:1 🙂 For example, you could define:
Copy code
query {
  sentMessagesLastMonth(userId: ID!) {
    count
  }
}
or something similar. And behind that you'd be querying the db with something like:
Copy code
const resolver = (parent, args, ctx) => {
  return ctx.db.query.messagesConnection({where: {
    userId: args.userId
  }, filter: {
    // your filters go here
  }}, `{ count }`);
};
this is just me pulling syntax out of my ass, but that's the general gist.
Again, I'm not 100% sure of the syntax - but the Prisma docs for your schema is very useful for figuring out how to structure your query.