I have a question regarding the triggering of Cloud Functions in Firebase.
I've seen the ones triggered by Creating, Delete or Update in Firestore, but what about if I have a Collection in Firestore in which documents have a Specific Date Time Field, and when that date comes the Function can auto-trigger.
Like if in the Collection Games, I have a Document in which the Date field is April 20, 2022, 18:30:00 UTC when that time comes a function will auto-trigger and run.
Is this possible to do, and if it is can someone show me a tutorial or something?
Kind Regards
You have to setup a scheduler.
Eg. Every minute your function starts and queries Firestore for documents with your_date_field value in the past.
https://firebase.google.com/docs/functions/schedule-functions
Related
I need to back up my prod server Firestore DB hourly. I know about exportDocuments but it incurs one read operation per document exported. I have more than 3 million and these are increasing day by day.
Is it possible to export docs that are added/updated in a given period like the last 1 hour?
I already have Cloud Scheduler + Cloud Pub/Sub + function-based backup system. It is backing up all the docs. It is costing too much.
If you need to schedule some operations in Firestore, you can consider using Cloud Scheduler, which allows you to schedule HTTP requests or Cloud Pub/Sub messages to Cloud Functions for Firebase that you deploy.
If you need to get the documents that are added/updated in a given period of time, like the last 1 hour, then don't forget to add a timestamp field to your documents. In this way, you can query based on that timestamp field.
To get docs that are added/updated in a given period like in the last 1 hour, add a field to the document, say lastUpdated, and keep its value current with every insert/update.
Then query for incremental documents like, where("lastUpdated", ">", "lastExportTimestamp") from the backup function, lastExportTimestamp being the time of last export (and may be stored in a separate collection).
See an example here.
Hope this clarifies, else leave a comment.
P.S. Please be advised that this approach may still need a full periodic backup (say daily), for ease of restore process, if/when required.
I am building an application that must react when the timestamp for a certain Firestore document becomes older than the current time. Is there a way to setup this type of query listener as a Cloud Function, or otherwise achieve the desired goal of reacting to a document when its timestamp crosses the current time?
From what I can tell reading the Firestore and Cloud Function documentation, query listeners may not be possible to setup as Cloud Functions. Furthermore, this is not just a regular query listener - the query criteria (time) is dynamic, so it isn't the typical query structure ("is A < 5") but a dynamic one ("is T < now" where "now" is changing every moment).
If it's true this is not possible as a query listener, I'd certainly appreciate any suggestions on how to achieve this goal through another means. One idea I had was to create a Cloud Function that triggers every 60 seconds and runs the queries based on the time at that moment, but this would not allow constant listening (and 60 seconds is unfortunately too long for our usage). Thank you so much in advance
Firestore queries can only filter on literal values that are explicitly stored in the documents they return. There's no way to perform a calculation in a query, so any time you need a now in the query - that timestamp will be calculated at the moment the query is created.
There are two common ways to implement the time-to-live type functionality that you describe:
Set up a process that periodically runs (e.g. a time-based Cloud Function), and every time the process runs perform a query to determine what documents have expired.
As a variant of this, you could start a permanent listener for updates each time the Cloud Function triggers and keep that active for slightly less than the interval until the next trigger.
Create a Cloud Task for each document that expires/triggers when the document needs to be processed. While this may seem more complex, it actually ends up being simpler due to the fact that your callbacks now trigger on individual documents.
Also see: Is there any TTL (Time To Live ) for Documents in Firebase Firestore, which includes a link to Doug's excellent article on How to schedule a Cloud Function to run in the future with Cloud Tasks (to build a Firestore document TTL).
This question already has an answer here:
Between StartDate and EndDate Firestore
(1 answer)
Closed 3 years ago.
I have a collection of events in my Firestore database where each one has a startDate and an endDate. On client side I would like to query for events that are happening right now. Normally, you would just check if currentDate is inside the time interval, however this can't be done with Firestore since it is disallowed to have more than one comparison query.
Has anyone encountered this problem and how did you overcome it?
I you are storing the startDate and endDate as milliseconds, you could just take your client time in milliseconds and make a query against your events to know if it has already started, is ongoing or is done.
Personally I usually store dates in millis to make queries and also in a human readable format when I check the documents using the Console.
however this can't be done with Firestore since it is disallowed to have more than one comparison query
No, you can query a Firestore collection using multiple comparison functions but only on the same property and not on multiple properties as you intend to do. According to the official documentation regarding query limitations:
Query limitations
Cloud Firestore does not support the following types of queries:
Queries with range filters on different fields, as described in the previous section.
So as you have probably noticed, Cloud Firestore allows a range filter on a single field. The most reasonable explanation is that Firestore cannot guarantee its performance in such a case.
To solve this, you'll have to query your collection twice and combine the results of those queries on the client. It's not perfect since you need to query twice, but I think it will do the trick.
I am trying to implement full text search of firestore DB by using Elasticsearch.
In order to sync the firestore data to elasticsearch, I used cloud functions with Firestore update events, such that whenever a firestore document is updated, a cloud function is triggered which updates the elasticsearch.
This works as expected. However, this is not cost effective.
I would like to update the elasticsearch periodically every hour by querying the firestore DB for documents that changed in the last one hour and updating elasticsearch.
Is there a way to query firestore for only those documents that got updated in the last hour?
You will need to have a field in the database that reflects when the document was last updated. Without that, you can't build a query to get what you want.
I know this is an old question but in order to achieve this you could add a field to your documents in firestore like "indexed": boolean and run a cron on cloud functions that query docs with indexed == false and then after indexing change them to true.
JS / Node.js solution:
How to use Firestore's rules.duration and/or rules.timestamp or other Firestore rules to ensure that a document could be created daily?
Put another way, a user would create, for example a comment/remark/tweet, at most once daily? So how to enforce using Firestore security rules?
For instance, Monday (24 Dec 2018) I could write a new comment. Tuesday (25 Dec 2018) I could write another new comment. But if I were to write the 2nd new comments on Tuesday (25 Dec 2018) it would NOT allow.
The solution should be able to work for daily, weekly, monthly, or quarterly.
Security rules don't have a sense of time, other than the current moment in time that some access occurred, and other timestamps in other documents. So you will have to use timestamps in other documents to gate access.
The only way I can think of to achieve this is in conjunction with Cloud Functions. You could have a single document per user that acts as a write location for new post data. Rules on that document would check that the user is doing two things:
Writing the current time (servervalue timestamp) into a known field.
The current time is also not less than the allowed time since the last write of that field.
When the write is successful, a Cloud Function could trigger on that write, then copy the post data from other fields in that document into the final document where the post must live.
Or you could simplify things a bit, skip the security rules, and just have a Cloud Function that deletes incoming documents that don't satisfy your post frequency rules by querying for the most two recent posts from that user, and checking their timestamps.