How to export security and index rules from Firestore?
Asked Answered
L

7

89

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.

Landlordism answered 12/10, 2018 at 11:25 Comment(1)
For all of the below solutions, firebase.json needs to be properly configured to be able to deploy firestore rules and indexes. You can run firebase init again and select Firestore to add configuration and files needed. Then, follow solutions explained below.Mcnair
A
77

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.

Aura answered 30/8, 2021 at 7:52 Comment(2)
then how do you deploy specific json file to different db?Fuhrman
firebase deploy --only firestore requires and uses ./firestore.indees.json This is not even clear in the official document, but that's how it's implemented. Be sure to switch to the target project before you do this (firebase use <target>). So it may not be a great idea to script just the deploy in your package.json.Chandelier
P
199

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.

Edit: It's still working as of 9.6.0.

Papacy answered 13/10, 2018 at 0:4 Comment(7)
That'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?Strabismus
@FrankvanPuffelen added an example, thank you for the tip :)Papacy
This no longer works. Results come back in a different format: "indexes": [ { "collectionGroup": "posts", "queryScope": "COLLECTION", "fields": [ { "fieldPath": "createdBy", "order": "ASCENDING" }, { "fieldPath": "createdAt", "order": "DESCENDING" } ] },Oculo
@TroyMichael I haven't used Firebase for a little while, can you mention your firebase version and I'll investigate shortly?Papacy
This worked for me using firebase-tools (firebase-cli) 8.7.0.Catcall
Yup, still works on version 9.6.0 too. ThanksBerkeleian
Use it like this : firebase firestore:indexes > firestore.indexes.jsonHaggai
A
77

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.

Aura answered 30/8, 2021 at 7:52 Comment(2)
then how do you deploy specific json file to different db?Fuhrman
firebase deploy --only firestore requires and uses ./firestore.indees.json This is not even clear in the official document, but that's how it's implemented. Be sure to switch to the target project before you do this (firebase use <target>). So it may not be a great idea to script just the deploy in your package.json.Chandelier
V
15

This is how my project files are laid out

myProjectFolder

  • .firebaserc
  • firebase.json
  • firestore.indexes.json
  • functions

  • Run the commands firebase use myApp-dev then firebase firestore:indexes > firestore.indexes.json to export your current dev project's indexes to a file

    • myApp-dev and myApp-prod is the "Project ID". To find it in Firebase, click the cog wheel next to "Project Overview" --> Project settings --> General tab (you should see it below)

In file firebase.json make sure it is pointing to the exported firestore.indexes.json for its indexes:

{
  "functions": [
    {
      "source": "functions",
      "codebase": "default",
      "ignore": [
        "node_modules",
        ".git",
        "firebase-debug.log",
        "firebase-debug.*.log"
      ],
      "predeploy": [
        "npm --prefix \"$RESOURCE_DIR\" run lint",
        "npm --prefix \"$RESOURCE_DIR\" run build"
      ]
    }
  ],
  "firestore": {
    "indexes": "firestore.indexes.json"
  }
}
  • Run the commands firebase use myApp-prod and firebase deploy --only firestore:indexes
Vernonvernor answered 7/11, 2022 at 16:43 Comment(1)
Why isn't this the accepted answer?Floor
S
2

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.

Strabismus answered 12/10, 2018 at 13:33 Comment(0)
H
1

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"
        }
      ]
    }
  ]
}
Herbartian answered 7/7, 2021 at 15:46 Comment(0)
V
1

To export composite indexes in json format using gcloud console use

gcloud firestore indexes composite list --format json

https://cloud.google.com/sdk/gcloud/reference/firestore/indexes

Vella answered 6/10, 2023 at 1:33 Comment(0)
L
-17

The Cloud Firestore Index Definition Reference page shows how.

You can export indexes with the CLI using firebase firestore:indexes.

Limicolous answered 13/10, 2018 at 1:48 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.