Invoking actions whenever a document is created/edit in a table

Yes. I am currently following your suggested approach. Will convex add this feature in future? Firebase has this feature to call functions when a doc in firestore is added/changed.
9 Replies
ballingt
ballingt2y ago
(Starting a thread) Possibly yes, it helps to hear that it's what folks want! Could you share more about how this is a useful feature? Convex works well as a Firebase replacement, but an additional feature of Convex that it is relational: you can use multiple tables to represent an entity and combine these records from multiple tables in queries (aka joins) which is more awkward in Firebase. If a User's data lives in multiple tables, instead of reacting to changes in one table to you want to react to changes in a query result instead — so in Convex these could be query listeners instead of table listeners. A reason to schedule the action in the mutation where the record is inserted or modified might be that that's when you know the most about the intention of the change, so you know just which actions should run.
ashuvssut (ashu)
ashuvssut (ashu)OP2y ago
I have 2 mutations and 1 query that does some CRUD on a table. Each time they are called, I need to execute a function that calls an API and return something to the caller mutation/query after receiving this returned value from that function, the query mutation will perform the CRUD. --------------------------------- The ISSUE is... I have to write the same logic for every mutation/query. What if we have a function/ action which listens to that table's changes and UPDATEs that new doc entry/ doc change By this, I am not repeating that UPDATE code logic in those 2 mutation and that 1 query .
If a User's data lives in multiple tables, instead of reacting to changes in one table to you want to react to changes in a query result instead — so in Convex these could be query listeners instead of table listener
Hm I get your point now
ballingt
ballingt2y ago
Should this action run regardless of what is modifed in the CRUD mutation, or only if certain things change about it? E.g. if the content of a message updates you might want to reindex it or fetch new vector embeddings for it or update another datasource with it, but if this name changes maybe you don't need to refetch? so would you want to know what changed in this table/query listener? Or is it enough to know that something changed.
ashuvssut (ashu)
ashuvssut (ashu)OP2y ago
my action should run "only if a certain thing changes about it" In my case, I would like to run that function when the "url" field changes of the document It would be nice to know what changed in the query
ballingt
ballingt2y ago
Thank you, I appreciate you walking through your use case!
ashuvssut (ashu)
ashuvssut (ashu)OP2y ago
welcome! Thank you for giving such detailed insightful replies. I am loving the DX in my project Hi @Tom I just got the error that "I cannot use fetch() inside queries or mutation" But the function that I mentioned that it calls some API needs to use fetch (https://discord.com/channels/1019350475847499849/1155248114647703582/1155251052610863176) This means I have to convert query and mutation to action in order to use that function
ballingt
ballingt2y ago
That's right, the mutation can only really write to the database, store files, and schedule functions. These are your transactions, they need to run quickly. You can either write the partial record and schedule the action to fill in the content (this is what I'd do) or you can run an action from the client which itself runs a mutation with ctx.runMutation(...)
ashuvssut (ashu)
ashuvssut (ashu)OP2y ago
Yes. understood
ballingt
ballingt2y ago
Actions can take longer to run and do fetch calls (like normal backend endpoints) while mutations and queries can access the database and must run quickly and cannot make fetch calls

Did you find this page helpful?