Firebase Cloud Firebase: How to move data between projects in Native mode - firebase

I managed to move data between projects using the instructions as per this guide: Moving data between projects.
During the import, I got an error:
ERROR: (gcloud.firestore.import) PERMISSION_DENIED: The Cloud Firestore API has not been used in project XXX before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/firestore.googleapis.com/overview?project=project-name and then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.
I enabled Cloud Firestore API and completed the import process.
Now, on Firebase console, when I go the Cloud Firestore page of the destination project, I see this message:
This project is set up to use Cloud Firestore in Datastore mode. This mode can only be accessed from Google Cloud Platform
The import command creates the Cloud Firestore database in Datastore mode. And it's not possible to switch it back Native mode as the first write on the database executed.
How can I import the data in Cloud Firestore Native mode?

I understand the import operation coerced the type of the new project database to Firestore in Datastore mode instead of Firestore Native, which was the mode of the database you exported the data from.
I have opened the following issue tracker so that this behavior could be investigated in further detail. I would recommend to star the issue and add your mail in the CC list so that you're aware of the updates made therein.
Separately, once the first write has been commited to the database it's no longer possible to change the database mode, therefore you'll have to create another project to use Firestore in Native mode.
To that effect I have tested the following workaround so that you could succesfully make the import in a new project:
Create a new project. Then go to the Firebase Console and create a Firestore database.
Using the console, create a mock collection and a mock document within it.
Proceed with the import as outlined in the documentation.
After completing these steps you could delete the mock collection and keep with your development as usual.

Related

Google firebase database with google functions running locally

I have an app connected to google firebase realtime database. I want to test google cloud functions as a server for running backend functions triggered once every day or triggered by data changed in the database.
I have an emulator instance running locally with a typescript function that I wrote.
I have the realtime database running, connected to the app and working well.
Now when I change data in the database, the functions are not being triggered.
function in index.ts:
import * as functions from "firebase-functions";
exports.simpleDbFunction = functions.region('europe-west1').database.ref('/some path in the database')
.onCreate((snap, context) => {
console.log("functions triggered by datachange:", snap.val())
});
the firebase project is connected to the correct project (using the interface in the terminal).
the app and realtime database are currently working and I can see changes in the data inside the firebase console.
What am I missing?
I've also tried the more complicated example from google's documents (makeUppercase function). But when it didn't work I went back to a simple function that is triggered on data creation and prints to the console.
Edit: I notice that in the emulator UI the real time database is empty. Shouldn't it connect to the real time database of the app? isn't that why I connected the emulator to a specific project?
Edit2: I tried changing the function line to:
functions.region('europe-west1').database.instance("the URL of the
rtdb").ref(...
and I get an error saying that the event function has malformed resource member
So I kinda solved it:
I found out that you can't really connect from a local emulator to the production environment of your actual google firebase. (Which is kinda weird, you have to commit to this technology even before you had a chance to see any code in action) <-- fix me if I'm wrong about this
If you use realtime database cloud functions you can't use any region other than 'us-central1'. as mentioned in this document:
https://firebase.google.com/docs/functions/locations
You can connect your app to the local emulator on your machine!
Get your database instance:
FirebaseDatabase database = FirebaseDatabase.getInstance();
Add the following line in your app (which is weird) before the first database operation:
database.useEmulator("10.0.2.2", 9004);
change 9004 to your local realtime database port.

How do I access Cloud Firestore when created as a service within Cloud Platform?

I'm familiar with using Cloud Firestore when created through Firebase, but I've created a Cloud Firestore as a service within Cloud Platform and I can't figure out how to have a client access that database. I was expecting there would be someway to generate a GoogleService-Info.plist so that my iOS app can access Firestore.
How is Cloud Firestore different when created from GCP, and how can clients access it?
If you want to access Firestore from iOS, you will still have to go through the Firebase console to add your app to the project and generate that plist file.
What you probably don't realize is that a Firebase project is just like a GCP project. Every Firebase project IS a GCP project, just pre-configured with some services and APIs enabled. Read this blog for a full discussion. If you created your project via the Cloud console, you can just as easily access the very same project from the Firebase console.
Your Firstore instance is 100% the same, no matter how you access it. All the same data and scaling behavior that you expect from Firestore. The only differences lie in how Firebase client apps add special mobile functionality. Read this blog for a full discussion specific to Firestore.
Bottom line is that, for use with mobile apps, you should still use the Firebase console to set that up. You can use both console together at the same time. They show almost entirely the same data, but Firebase is simplified for mobile-centric use.

Is it possible to test Firestore in Flask Web App locally (without deploying the website)

I am working on small project where data is uploaded to Firestore on one page, and retrieved on another in the same website. Is it possible to test the Firestore integration when the website is hosted on localhost?
When I try accessing the Firestore functionality after deploying my website on localhost, I get:
"google.api_core.exceptions.FailedPrecondition: 400 The Cloud Firestore API is not available for Datastore Mode projects." error.
The only documentation I can find is: "How to deploy Flask Crud API with Firestore..."
Note:
1. I looked at the error, and double checked that my project is in Firestore mode (I created the project from Firebase Console), and then used following in my Flask application to authenticate database:
import firebase_admin
from firebase_admin import credentials
cred = credentials.Certificate("path/to/serviceAccountKey.json")
firebase_admin.initialize_app(cred)
Any other inputs on this would be appreciated!
I tried looking into Simulators, but most of the documentation said that it's only for Cloud functions, whereas I'm trying to test simple operations.
Thank you for looking into this.
The error message means that your Cloud Firestore database is in Datastore mode instead of Native mode. Unless you never saved anything to your database, you can't change the mode.
Try creating a new project, creating a new Cloud Firestore database, and selecting Native mode:
In the GCP console, it'll ask you to choose.
In the Firebase console, it'll automatically select Native mode.
I found out the issue. I was using cred = credentials.Certificate("path/to/serviceAccountKey.json") code with new generated private key for authenticating my firestore database, however, system was taking an old $GOOGLE_APPLICATIONS_CREDENTIALS for this purpose.
Updating that to new key fixed the issue, and yes, I am able to test FLASK app integration with Firestore on Localhost :)
export GOOGLE_APPLICATION_CREDENTIALS='/Users/..../newkey.json'

How to switch from Datastore to Firestore in existing project?

I have an existing Google Cloud project which uses Datastore. I'm excited by Firestore and would like to switch. According to https://www.youtube.com/watch?v=SYG-BgXoJFQ it is recommended to create a new project.
Is it possible to just nuke Datastore somehow (I don't care about the data) and start from scratch with Firestore?
If not, what are the implications of creating a new project?
If there is no data written to Cloud datastore, it's possible to convert from datastore to firestore with the following steps. I tried on 2 projects and each project took 3 minutes. The user should be the project owner to attempt:
Disable Cloud datastore API
Disable Cloud Firestore API
gcloud firestore databases create --region=us-central --project $PROJECT_ID
I have another project that has little datastore entities. I deleted them and executed the steps, but received the following error. While disabling datastore API, I read Disable Cloud Datastore API? If any resources were created by Cloud Datastore API, they may be deleted soon after Cloud Datastore API is disabled. All code that uses this project's credentials to call Cloud Datastore API will fail.
ERROR: (gcloud.firestore.databases.create) Error Response: [9] The
"database_type" field cannot be modified for this application. Note:
If data has already been written for this application, then the
"database_type" may not be modified.
The solution is to contact Google cloud support to convert from Datastore to Firestore. You need to make sure no entities exist and nothing is creating any entities.
In the case where no Datastore entities have been written, the operation should succeed:
Success! Selected Google Cloud Firestore Native database for $PROJECT_ID
It is not possible to switch from Datastore to Firestore within the same project as yet, but you may operate Firestore in Datastore mode. By creating a Cloud Firestore database in Datastore mode, you can access Cloud Firestore's improved storage layer while keeping Cloud Datastore system behavior. You may find more information of relevance by reading the "Automatic Upgrade to Cloud Firestore" documentation page.

maintain connection status of user with firestore. using firebase db and cloud function

I am trying to maintain connection status of user with firestore . but i don't understand some points.
Link : https://firebase.google.com/docs/firestore/solutions/presence
i refer the above link description but i don't understand how to write code.
see the below given topics:
Using presence in Realtime Database
where to write this given code in application or Cloud functions?
2.Connecting to Cloud Firestore
Updating Cloud Firestore's local cache
where to write local cache update code?
I use this in my android application.
Each section of the code in the Build Presence in Cloud Firestore is marked.
If it is marked with WEB, you'll need to run this code in your web application. If your clients are native Android or iOS, you'll need to write and run similar code for those platforms.
If the code is marked with NODE.JS, you will need to run this code in a Node.js environment. It should be possible to accomplish the same in Cloud Functions, but no sample is provided for that.

Resources