Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to synchronize Firestore rules and indexes? [closed]

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.

like image 848
Satoshi Nakajima Avatar asked May 25 '19 18:05

Satoshi Nakajima


People also ask

How long does it take for Firebase rules to update?

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.

How do you update firestore rules?

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.

Why are my firestore reads so high?

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.


1 Answers

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:

  1. Set-up your local project to deploy the Firestore security rules through the CLI and only modify the rules locally before deploying (not super flexible)
  2. Don't define the Security rules locally but only in the Firebase console. For that you should remove the "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.

like image 72
Renaud Tarnec Avatar answered Sep 20 '22 19:09

Renaud Tarnec