Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to export security and index rules from Firestore?

I've set up multiple different indexes on my Firestore development database. Now, I would like to export them into the firestore.indexes.json so that the process of setting up prod environment would be easier. Is there a way to export those indexes using Firebase CLI? The same applies to security rules, although I know that I can copy paste them.

like image 402
uksz Avatar asked Oct 12 '18 11:10

uksz


People also ask

How do I export firestore index?

It's possible! Run from CLI firebase firestore:indexes inside your firebase project folder. Providing you have indexes already setup and logged into Firebase via the CLI too, you'll get a formatted JSON output for you to copy. Exported indexes can be re imported using firebase deploy --only firestore:indexes .

How to get Firestore rules?

Use the Firebase console To set up and deploy your first set of rules, open the Rules tab in the Cloud Firestore section of the Firebase console. Write your rules in the online editor, then click Publish.

How do you deploy firestore security rules?

Open 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.

What are rules in Firestore?

Cloud Firestore Security Rules allow you to control access to documents and collections in your database. The flexible rules syntax allows you to create rules that match anything, from all writes to the entire database to operations on a specific document.


2 Answers

It's possible!

Run from CLI firebase firestore:indexes inside your firebase project folder.

Providing you have indexes already setup and logged into Firebase via the CLI too, you'll get a formatted JSON output for you to copy.

Example:

{   "indexes": [     {       "collectionId": "teslaData",       "fields": [         {           "fieldPath": "Model",           "mode": "ASCENDING"         },         {           "fieldPath": "Price",           "mode": "ASCENDING"         }       ]     }   ] } 

Exported indexes can be re imported using firebase deploy --only firestore:indexes. Check following doc extract.

https://firebase.google.com/docs/firestore/query-data/indexing

You can also deploy indexes with the Firebase CLI. To get started, run firebase init firestore in your project directory. During setup, the Firebase CLI generates a JSON file with the default indexes in the correct format. Edit the file to add more indexes and deploy it with the firebase deploy command. If you only want to deploy indexes, add the --only firestore:indexes flag. If you make edits to the indexes using the Firebase console, make sure you also update your local indexes file.

I'm using Firebase CLI 4.2.1 if that helps, good luck :)

Edit: It's still working as of 9.6.0.

like image 153
fyllepo Avatar answered Oct 21 '22 16:10

fyllepo


In your Firebase project folder execute this in the terminal:

firebase firestore:indexes > firestore.indexes.json 

And it will save a file called firestore.indexes.json with your indexes.

You can then upload that file onto other Firebase projects.

like image 32
mesqueeb Avatar answered Oct 21 '22 17:10

mesqueeb