What is the best approach to call query when they depend on scheduled functions?
I am calling
useMutation from client where it inserts data in the db for that particular chat Id, and then run certain scheduled functions based on the data, and I am returning the chat Id to the client when the data has been inserted to the db.
My application is working fine, but I feel like I am making unnecessary query requests, when all the scheduled functions have not yet been completed.
I am currently showing a loading state to the user till the embeddingId is undeifned, since the last scheduled function will populate the embeddingId.
Is this okay? Or is there a better way to achieve this?
Here is my code for inserting data to the db and calling the scheuled functions
The scheduled function is dependant on other scheduled functions.8 Replies
If you don't need to show the chat until the embedding is there you can return
null on the backend:
The query will rerun when the chatbook document changes, but it won't be pushed to the client if the result is the same (null in my example).
Does that help?Yeah I get that, but won't the query still be called multiple times till it has an embeddingId?
Or is it okay for it to be like that?
I would say this is OK. You could avoid it if your scheduled functions didn't touch the data
getEmbeddingId relies on.
You could have a separate document on which the scheduled functions "work" (ie write progress to), and then when they are done they write to the "chatbook" document only.Ah, yes. That would work. I'll look into that or just use this. Thanks.
@Rayy for the approach:
Say
getEmbeddingId reads from "chatbook" table
You could have scheduled functions which work on a document in "chatbookInProgress" table
The "chatbookInProgress" document can include v.id("chatbook"). The scheduled functions can read from "chatbook", and write to "chatbookInProgress". When your offline processing is done, your final scheduled function can write into "chatbook". Only then getEmbeddingId will recompute.I was able to implement it, and now my
getEmbeddingId query will be called only after it has all the embeddings. Thanks about that @Michal Srb .
I just had one more query, I don't want to call a query, everytime a component re-renders. I do not want to use One-off queries, since I would want to listen to the changes in the query and if any, then update the document accordingly.
Whenever my Chatbot re-renders, my query is being called which I want to avoid. I do not want to add memoization to the Chatbot component either. Is there any other way to achieve this?
Whenever my Chatbot re-renders, my query is being called which I want to avoid.This is not the case. The query on the server only runs when the data it depends on changes. The React client caches the result.
useQuery doesn't force the query to run again.I might be doing something wrong then, I’ll look into it.
Hey @Michal Srb ,
I figured the error, I was updating the conversation in the "chatbook" table itself, making it call the
embeddingId every time a message is patched to the database.
So, in order to fix that, I created a new "conversations" table which has the messages filed, along with the chatId.
When I insert the conversationId in the respective chatId in the "chatbook" table, the embeddingId is still called everytime there is a change in the "conversations" table. But without inserting the conversationId, the embeddingId is not called every time there is a change in the "conversations" table.
But I would like that conversationId in the "chatbook" table.
One way I found this to work is to pass the conversationId as a string.
I was wondering if there are any better ways?
It was becoming a real pain, so I just refactored my schema. Now the “chatbook” table only depends on userId.
And everything looks good. 🙂