How to export security and index rules from Firestore?

FirebaseGoogle Cloud-FirestoreFirebase Security

Firebase Problem Overview


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.

Firebase Solutions


Solution 1 - Firebase

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 - Firebase

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 - Firebase

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.

Solution 4 - Firebase

If the accepted answer isn't working for you (I got a permissions error) for firestore indexes you can go to your firebase console > Cloud firestore > Indexes then open up the network tab in inspector, clear all the requests and refresh the page. Once the page is loaded you can find the JSON formatted response of the indexes (I found mine by searching the word 'indexes' in the search bar of the network tab) in the XHR filter of network requests. It should look something like 'indexes?key=...' you can copy this JSON response.

If you've already initialized firebase in your project with firebase init, you can simply paste it into your project's firestore.indexes.json file. Then change each name property to a collectionGroup property. eg: 'name': 'projects/[your project name]...' to 'collectionGroup': '[name of collection for this index]'

Run firebase deploy --only firestore:indexes to update any changes made in your text editor back to the firestore indexes tab

for firestore security rules, in a less complicated but similar manner, you can copy and paste the rules shown in the firebase console into the firestore.rules file of your project.

sample firestore.indexes.json file

{
  "indexes": [
    {
      "collectionGroup": "faq",
      "queryScope": "COLLECTION",
      "fields": [
        {
          "fieldPath": "searchKeywords",
          "arrayConfig": "CONTAINS"
        },
        {
          "fieldPath": "answered",
          "order": "ASCENDING"
        },
        {
          "fieldPath": "relevanceScore",
          "order": "ASCENDING"
        },
        {
          "fieldPath": "__name__",
          "order": "ASCENDING"
        }
      ]
    }
  ]
}

Solution 5 - Firebase

Any Integrated Development Environment rides on an operating system that supports Node.js (which is required by Firebase CLI). So, if you look at Nodes Filesystem documentation, there are examples of how to manipulate (copy/paste) files programatically (via JavaScript). As far as deploying to Firebase programatically, see child_process.spawn

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionukszView Question on Stackoverflow
Solution 1 - FirebasefyllepoView Answer on Stackoverflow
Solution 2 - FirebasemesqueebView Answer on Stackoverflow
Solution 3 - FirebaseFrank van PuffelenView Answer on Stackoverflow
Solution 4 - FirebaseTraeView Answer on Stackoverflow
Solution 5 - FirebaseRonnie RoystonView Answer on Stackoverflow