#Mutations Inside Queries

27 messages · Page 1 of 1 (latest)

tribal veldt
#

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?

little phoenix
#

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.

nova galleon
#

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)

tribal veldt
#

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

little phoenix
#

Can you share a bit more about what you're trying to accomplish? Maybe we can get a different angle.

nova galleon
tribal veldt
# little phoenix Can you share a bit more about what you're trying to accomplish? Maybe we can ge...

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.

tribal veldt
little phoenix
#

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

tribal veldt
#

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.

little phoenix
#

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?

tribal veldt
#

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

little phoenix
#

Ah okay

tribal veldt
#

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

little phoenix
#

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.

lethal ermine
#
  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/)
tribal veldt
#

@lethal ermine 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?