Copy Composite Indexes on GCP [duplicate] - firebase

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.

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.

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.

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.

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

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"
}
]
}
]
}

The Cloud Firestore Index Definition Reference page shows how.
You can export indexes with the CLI using firebase
firestore:indexes.

Related

firebase firestore index building [duplicate]

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.
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.
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.
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.
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
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"
}
]
}
]
}
The Cloud Firestore Index Definition Reference page shows how.
You can export indexes with the CLI using firebase
firestore:indexes.

Nuxt3 with Firebase Hosting using Cloud Functions: Where to see the other non-hosting cloud functions?

I have set up my Nuxt app (Nuxt3) to use Firebase Hosting with Cloud Functions. This works fine and able to deploy. However, now I wanted to use a cloud function to handle events triggered in Firestore database. I set up a sample "hello world" cloud function by following the Firebase Cloud Functions tutorial , but now when deploying this new hello world function via the local emulator, the function is no where to be found!
Here is the Emulator log when I run firebase emulators:start:
I see the function output is the server folder, but that is for the app hosting.
Where is the helloWorld cloud function I made?
Directory structure:
functions/:
And the app's root contains a firebase.json like so:
{
"functions": {
"source": ".output/server",
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run lint"
]
},
"hosting": [
{
"site": "<removed for this post>",
"public": ".output/public",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"cleanUrls": true,
"rewrites": [
{
"source": "**",
"function": "server"
}
]
}
]
}
My helloWorld.js function is correctly transpiled into the build folder:
Where/how do I access my Hello World function I made? The emulator ONLY seems to show the hosting function.
There is nothing in your post that makes the Firebase Functions Emulator look in the functions folder. It gets completely ignored.
You’ve configured it as:
functions.source: .output/Server
The default for this field is “functions”. Thus, in many samples, it seems like there would be a special meaning for that folder name. It’s just a default.
Thus: it looks into .output/Server/index.js (I think) and executes that.
If you want multiple Functions entry points, have a look at the “codepaces” feature recently released: https://firebase.google.com/docs/functions
Note: I haven’t used codespaces myself

Target Deploy for Firebase Functions

I'm trying to set a target deploy for Firebase Functions.
https://firebase.google.com/docs/cli/targets
This is the firebase.json file
{
"functions": [{
"target": "production",
"source": "src/prod/functions",
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run lint",
"npm --prefix \"$RESOURCE_DIR\" run build"
]
}]
}
The predeploy runs normally but when deploying gets an error
functions[prod]: Finished running predeploy script.
Error: An unexpected error has occurred.
If I go to firebase-debug.log it shows:
TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received type undefined
Putting the Functions folder on the project root it will deploy normally.
What is missing here?
Try running firebase init and set the options as best you can so it doesn't overwrite your existing project. I had to run it from the parent directory of /functions otherwise it will create a new directory but maybe there is some option to set a different directory name to suit your case.
From the link that you posted:
Firebase supports deploy targets for:
Firebase Hosting sites
Cloud Storage for Firebase storage buckets
Firebase Realtime Database instances
I was also looking for the same answer, but it seems that it is still not supported in the end of 2022

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.
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.
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.
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.
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
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"
}
]
}
]
}
The Cloud Firestore Index Definition Reference page shows how.
You can export indexes with the CLI using firebase
firestore:indexes.

How do I deploy Firebase Database Security rules using the command line?

In older versions of Firebase, we could add a rules section to our firebase.json file, and upload new security rules on every deploy.
How do we use the firebase-tools v3 command-line tools to deploy database security rules?
This page says that it's possible: "Rules for Firebase Storage"
This page hints that the command line tools can do it, but firebase --help and firebase deploy --help don't seem to hint at how to do it? (Apologies if I missed it...)
(related: where is the canonical doc for everything that can go into firebase.json? I found it on the old Firebase site, but can't find it via search on the new docs.)
Thanks!
You can use firebase deploy --only database if you only want to update database rules. It will overwrite your existing rules.
You can check out Firebase CLI Reference for more info
You can use firebase deploy or firebase deploy --only database from the command line, BUT most important:
Please note hereunder firebase.json format: The "rules" entry is under "database" entry.
It was taken from Firebase Sample code.
{
"database": {
"rules": "database-rules.json"
},
"hosting": {
"public": "./",
"ignore": [
"firebase.json",
"database-rules.json",
]
}
}
To deploy a new set of security rules with firebase cli
firebase deploy --only firestore:rules
To deploy a new set of security rules, add a rules top-level key to your firebase.json.
Example firebase.json file:
{
"rules": "firebase_rules.json",
"hosting": {
"public": "doc",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
]
}
}
The firebase_rules.json is a JSON file that contains the security rules. Here's an example:
{
"rules": {
".read": false,
".write": false
}
}
When you run firebase deploy, it will send the contents of firebase_rules.json to the server, replacing/updating any rules configurations.

Resources