I'm writing a script for setting up a Google Cloud project and I'd like to use Firestore. A new project, however, is by default in Datastore mode.
Switching to Firestore can easily be done using the web interface in the cloud console but I'd like to automate it. Is there any API or command line tool (gcloud? firebase?) for enabling it?
Unfortunately, as of today (30 Apr 2019), there is no API or command line to set the mode for Cloud Firestore.
Things seem to have changed by now (03 Jan 2022). I managed to set up a new Firestore with gcloud using the following commands.
Native mode is chosen by default:
gcloud app create --region=us-central
gcloud firestore databases create --region=us-central
also see the documentation for
app create and firestore database create.
Related
I am using gcloud + firebase cli to explore a reproducible way to create and configure a GCP + Firebase project.
I have created the GCP project using gcloud cli tool. I have then used firebase cli to run the command firebase init firestore.
Ulimately it ended up outputting...
Error: It looks like you haven't used Cloud Firestore in this project before. Go to https://console.firebase.google.com/project/my-project/firestore to create your Cloud Firestore database.
Is there a way I can "create my firestore database" using a cli tool or scripting api, instead of having to navigate to a web GUI tool and manually execute steps?
Unless Firebase do more behind the scenes, creating a Firestore instance could be as simple as this command from the GCloud CLI reference:
gcloud firestore databases create --region=us-central --project=my-project
The documentation for the entire create-provision cycle (from GCP's perspective) is here - including Terraform details (some commands may have been released since the docs were written).
I am trying to set up a Firebase project that needs a default Realtime Database instance using non-interactive CLI.
I've tried deploying the rules:
❯ firebase deploy --only database
Error: It looks like you haven't created a Realtime Database instance in this project before.
Please run firebase init database to create your default Realtime Database instance.
but firebase init is an interactive command, so I can't use it in my CI pipeline.
An attempt to use firebase database:instances:create command also fails with the same message:
❯ firebase database:instances:create default
Error: It looks like you haven't created a Realtime Database instance in this project before.
Please run firebase init database to create your default Realtime Database instance.
I've tried googling for anything around creating default realtime database instances, but can't find anything besides going the interactive CLI way or doing it through the console - both of which are ill-suited for CI.
Is there a way to achieve what I'm after without resorting to interactive tools?
From the second line of that second error message:
Please run firebase init database to create your default Realtime Database instance.
It seems that you'll need to run firebase init database to create the initial/default database instance for the project. After that you can run firebase database:instances:create to create additional instances.
I am trying to create firestore database but got the message "This project is set up to use Cloud Firestore in Datastore mode. This mode can only be accessed from Google Cloud Platform".
This is setup using GCP. How can I do that? Also not able to switch to native mode. Please help me.
This message indicates that your database was created in Datastore mode, when you created your project. You should be able to change it by accessing the Datastore Console using this link here and clicking in the available button to switch the mode - check here. However, as clarified in this other post from the Community here, you can only do it once and if you have not performed any changes in your database.
In case you don't fit in these above scenarios, you will need to create a new project and configure Firestore from the beginning.
Let me know if the information helped you!
I face the same problem on creating a fresh project.
I think it's default behavior with latest changes.
Just go to Datastore and switch to NATIVE MODE.
Go over to Firestore Database
Click on the link provided
Change to Native Mode
Context, I have a project with datastore which already has information loaded, currently we wanted to use cloud firestore (native mode), but we realized that migration is not possible, what alternatives do I have to use cloud firestore (native mode)?
Update June 16, 2021:
You can now do gcloud datastore export in your first project, followed by gcloud firestore import in your new project. The longer more involved migration below is no longer needed.
Just keep in mind that the Datastore export goes to a Cloud Storage bucket. Make sure that the account running the Firestore import has access to that bucket.
Original answer from 2019
I just migrated from Datastore to Firestore (native mode) for one of my web apps. Here is what I needed to do:
Create a new GCP project, as Firestore (native mode) and Datastore
can't co-exist in the same project.
Migrate the data from Datastore in my old project to Firestore (native mode) in my new project. As of this writing, there are no tools to do that in an automatic way. I wrote Python scripts that read all records from Datastore and wrote them to Firestore in the new project. These scripts ran locally on my machine, using service account keys downloaded from the Cloud Console.
(Side note: You might be tempted to use gcloud datastore export followed by gcloud firestore import. It seems to work and no error messages pop up when you do. But doc IDs and JSON properties don't translate well. This was a big time-sink for me. Don't go down this road.)
Rewrite the data access layer in your app. Firestore (native mode) has a different API than Datastore.
This was a fair amount of work, but it was worth it in my case:
I was able to retire a lot of server-side code because the clients can access the database directly.
I was able to retire a lot of client-side code for supporting offline mode because the Firestore client library implements it already.
Best of luck!
Unfortunately, you'll need to create your Cloud Firestore database in a new project that allow your existing service accounts to access that new database.
I am looking into automating the deployment of a project that is made up of multiple functions that are triggers on multiple realtime database instances.
Using firebase deploy allows you to deploy both functions and database rules.
I have also created targets so i can deploy different rules for different database instances.
The only missing part of my automation is actually creating those different database instances via the Firebase CLI tool; Since deploying a trigger function to a specific database that doesn't exist will fail. Ideally i would like to make sure to create those database instances if they don't exist and then continue my deployment.
Is there a way to create different database instances via the CLI ?
I don't know when it was added, but this might be useful for others having the same question.
You can create a Firebase Realtime Database instance using the following CLI command:
firebase database:instances:create [instance-name]
I don't know why this isn't described in the CLI reference, but it is mentioned here: https://firebaseopensource.com/projects/firebase/firebase-tools/
You can Create realtime database instance by firebase CLI using the below command
firebase database:instances:create -P <alias_or_project_id> <DBinstanceName>
This Command work fine if you have paid plan.
For Free Plan it throw error:- FirebaseError: HTTP Error: 400, Precondition check failed.
Note : <DBinstanceName> should be alpha-numeric with hyphens.