Reputation: 3049
I have an app where all data shown to the user is received from firestore documents. These documents consists of either user generated content or data that's been imported from 3rd party APIs. The imported documents contains an expiration timestamp. Each screen in the app is mapped to one or more firestore document paths.
If the app requests and subscribes to a document at a specific path, and that document is not found, or its expiration has been reached, I want a cloud function to (re)import the document from the external API, so I can be sure that the updated data will appear in the app shortly.
However, I cannot find a trigger for document read or not found operations, which would have been the obvious choice.
Without being able to trigger functions on reads, what are the alternative solutions here?
I'm using react/redux/firestoreConnect for getting the data.
Upvotes: 0
Views: 901
Reputation: 83048
I understand that somehow you query, from your app, the Firestore database to find one or more document(s) and that in case
you want to trigger a Cloud Function that would "(re)import the document from the external API".
One way to do that would be to trigger a Callable Cloud Function, from your app, when your query to the Firestore document returns no document or returns a document with an expired date.
Then in the Cloud Function, you execute your API call, update the document and return a "success" flag to the app to indicate it can re-fetch the Firestore doc, since this one has been (re)imported.
In other words, you trigger the (re)import from the app and not based on a Cloud Function background trigger.
I hope this will help, don't hesitate to ask for more detail/direction if needed!
Upvotes: 1