How to export security and index rules from Firestore?
Solution 1
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
.
Solution 2
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.
Solution 3
I don't think there is currently an API for getting the Firestore security rules from a project. You can deploy rules through the CLI, which can also be embedded in custom Node scripts, and invoked from CI processes. But as far as I know there is no API to read the rules from a project.
It sounds like a good reason to file a feature request.
Comments
-
uksz about 2 years
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. -
Frank van Puffelen over 5 yearsThat's a great find @fyllepo! I'd completely missed that you can use
firestore:indexes
to read the indexes. I'm struggling to get it to work, but it seems that's just on me. Did you get it to work? If so, I assume the JSON file contains only the indexes, and not the security rules. Is that correct? Can you maybe update your answer to show what the JSON contains? -
fyllepo over 5 years@FrankvanPuffelen added an example, thank you for the tip :)
-
Troy Michael about 5 yearsThis no longer works. Results come back in a different format: "indexes": [ { "collectionGroup": "posts", "queryScope": "COLLECTION", "fields": [ { "fieldPath": "createdBy", "order": "ASCENDING" }, { "fieldPath": "createdAt", "order": "DESCENDING" } ] },
-
fyllepo about 5 years@TroyMichael I haven't used Firebase for a little while, can you mention your firebase version and I'll investigate shortly?
-
Birch almost 4 yearsThis worked for me using firebase-tools (firebase-cli) 8.7.0.
-
Wolf87 about 3 yearsYup, still works on version 9.6.0 too. Thanks
-
VHanded about 3 yearsUse it like this : firebase firestore:indexes > firestore.indexes.json