Mutations Inside Queries
I am attempting to find a way of executing a mutation of Collection A each time Collection B is queried. The example use case here would be incrementing a value within a document each time a separate collection is queried. While I understand each mutation/query is a separate entity and cannot be nested. What is the suggested approach to this issue?
17 Replies
I'd say use a mutation to do both at once.
There's no way to write from a query, not even to schedule a function.
If you want to have a function that is "access and increment" you can make that a mutation. You lose the ability to subscribe to changes with a mutation, but you can write to the DB (these two are both related to Convex's guarantees around determinism & caching)
The issue is that I'm trying to trigger a mutation upon each query event. So it needs to connect with the activity of my queries somehow.
I tried using actions but React didn't like that
Can you share a bit more about what you're trying to accomplish? Maybe we can get a different angle.
I also highly recommend going through the tour to get the query/mutation/action paradigms: https://docs.convex.dev/get-started
Welcome to Convex | Convex Developer Hub
Convex is a novel, fun, and extremely productive way to make backends for your
So essentially, I would like to capture a count of each time a query is run for an authenticated user. The way I have been attempting to approach this so far has been to find a way to trigger an update mutation from my query definitions. For example, each time a query to gather all documents is executed, a mutation is triggered to my users database that increments the number of calls associated with the user. Perhaps I'm going about this the wrong way? Others out there must require this sort of functionality at some point.
Thanks Ian! I've gone through the tutorial but cound't find anything to indicate a resolution to my issue. Definitely appreciate the Convex documentation though, it's been a big help.
To trigger a query that I'm assuming has a limit to number of times run, does the user have to do something explicit? Or are the queries running through just loading views?
Eg. as a user running one of these queries, do I have to push a button, or just open the app
It's a mix, but some of them run by loading a view. My hope was to have this all set up in the backend to ensure that every query has a count collected.
Wondering if you can get this data from what Convex sends to integrations like Axiom
just checked, doesn't look like it
Query count is also a bit tough with Convex just conceptually, as a user would be getting updated data automatically if underlying data changes
Is this for billing-relevant usage limits or abuse limits?
Yeah, that makes sense
Yep, I'm trying to see where and who is the cause of some significant bandwidth bleed I've experienced lately
Ah okay
Along with implementing some controls for future prevention
However, I'd also like to use a similar functionality for understanding user data allocation for future billing features
It'd be helpful to have expanded data going out to integrations - I wouldn't expect to track this sort of thing directly in my app db. I know there's been talk of exposing more "slightly stale" data for Convex devs to explore with external tooling, but I don't where that's at on the roadmap.
Relevant post here: https://discord.com/channels/1019350475847499849/1191138600474136716/1191138600474136716
1. You can log from your queries and then use logs streaming to understand what's happening
2. You can't rate limit queries that are not changing (they're cached, and we'll make them cheaper soon), but you can rate limit on the write side for anything that's changing (search for rate limit on https://search.convex.dev/)
@Michal Srb could you expand on what you mean by logging from my queries along with the log streaming. Is there some documentation you could point me to possibly?
Ah cancel that, I found the documetation:
https://docs.convex.dev/production/integrations/log-streams
Log Streams | Convex Developer Hub
Configure logging integrations for your Convex deployment
This looks like it might be the key! I'll play around with solutions and get back to this thread if I have some success. Thanks again @Michal Srb, @ian , and @erquhart !