I have a Firebase project, which uses Firestore. I often manually modify security rules and indexes on the Firebase console, but I also have local copies on my dev machine (firestore.rules
and firestore.indexes.json
), which become out of sync when I do so.
Is there a way to automatically sync them? For example, if I create an index in the console, how do I then persist that index in my firestore.indexes.json
?
So far I am manually applying those changes to local copies, but that is obviously an error prone process, and I'm trying to avoid accidentally overriding with out-of-date indexes or rules.
You can deploy rules in the Firebase console or using the Firebase CLI. Updates to Cloud Firestore Security Rules can take up to a minute to affect new queries and listeners. However, it can take up to 10 minutes to fully propagate the changes and affect any active listeners.
Edit and update your rulesOpen the Firebase console and select your project. Then, select Realtime Database, Cloud Firestore or Storage from the product navigation, then click Rules to navigate to the Rules editor. Edit your rules directly in the editor.
Use of the Firebase console will incur reads. If you leave the console open on a collection or document with busy write activity then the Firebase console will automatically read the changes that update the console's display. Most of the time this is the reason for unexpected high reads. You can go through this answer.
There is no automatic mechanism to handle conflicts between the Security rules declared in the Firebase console and the rules declared locally that get deployed when you deploy with the CLI.
If your local project is set-up to deploy the Firestore security rules they will overwrite the rules in the console when you deploy with the CLI. (See this documentation item: https://firebase.google.com/docs/rules/manage-deploy#edit_and_update_your_rules)
There are two possible approaches:
"rules": "firestore.rules"
line from the firebase.json
file and remove the firestore.rules
file from your project.Note that "using the CLI allows you to keep your rules under version control with your application code".
With indexes, things are a bit different: if there are differences between the two versions (the local firestore.indexes.json
file and the index in the console), you will get a warning in the terminal when deploying through the CLI:
i firestore: there are some indexes defined in your project that are not present in your firestore indexes file. Run firebase firestore:indexes and save the result to correct the discrepancy.
i firestore: there are some field overrides defined in your project that are not present in your firestore indexes file. Run firebase firestore:indexes and save the result to correct the discrepancy.
By running firebase firestore:indexes
, the indexes will be printed in the terminal and you will be able to copy them in order to manually update the firestore.indexes.json
file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With