How To Prevent Firebase Firestore Emulator From Clearing The Database At Exit? - firebase

I'm Planning To Use Firebase - Firestore Emulators For Handling Some Huge Amount Of Data That's Coming From A Websocket.
I just need to process the data locally in realtime.
Firestore Emulator Is A great choice since it has all the features I need. But the issue is it clears the database every time the Emulator is turned off. is there any config or settings that can change this behaviour?
if it is not possible. can somebody tell me any other Approach to use firestore locally? or any other alternative to firebase that I can use locally.

With the recent updates to Firebase Emulator, you use export the data when the emulator is running and import when it starts again.
To export the data
firebase emulators:export <export-directory>
To start and import the emulator
firebase emulators:start --import <export-directory>

I've managed this starting Firebase emulator with parameters to export data on exit and import existing one:
firebase emulators:start --import=exported-dev-data --export-on-exit=exported-dev-data
This way the emulator always export the data I've generated to folder exported-dev-data, and loads it back when starting again.

Currently you need to write code to populate the database during each run of the emulator.
If you would like to see different behavior, please file an issue on GitHub.

Related

How to export data from Firebase Firestore emulator to actual Firebase Firestore database

Scenario:
I am working on a POC locally where I am using Firestore as my database. As it is my local setup I am using Firestore Emulator. Now my POC is successful and I want to move local database from emulator to actual Firestore.
Query:
Is it possible to achieve what I am trying to do?
So far I am not able to find any relevant content on internet around this. I did find couple of examples where there is demonstration of exporting data from Firestore and importing to local emulator but I was not able to find the vice-versa option!
Firebase does not provide any sort of tool or service to do this. Your easiest alternative will be to write a program to query the data out of the emulator and write it into your cloud hosted instance. You might find the Firebase Admin SDK helpful for writing to the cloud in a program that you run locally.

Firebase cloud functions emulators always write to a default database

I recently set up the firebase emulators to run my cloud functions locally. After setup, the cloud functions triggered successfully but any write to the real-time database does not reflect in the corresponding local database emulator UI. e.g when I use
snapshot.after.ref.parent.child('busy').set(true)
So I tried exporting and importing the real-time data and I discovered that all database write from cloud functions was being saved in a default database with name localhost:4000/?ns=pick2-c468b-default-rtdb and not the database that is triggering the write event.
Is this the default behaviour of the emulator and how do I go about changing or fixing it?
Writes to snapshot.after.ref should go to the database that triggered the Function.
If that is not happening for you, I recommend reporting a bug with a minimal repro on the repo (which should include an entire Cloud Function, and not just the line that you think is going wrong.

Is there anyway to configure the firebase firestore emulator to use the data inside the production database?

Is there a way I can configure the firestore emulator to start off with the data in my production database. I have a lot of test information in there and want to transition it to the local emulator without having to copy each and every document/collection. My initial thought was that there must be a configuration in the firebase.json file but I'm sure what it would be.
There is no feature to configure the Firestore emulator to read its initial data from the production database.
What you can do is export the data from your production database (through its regular API(, and import it to the emulator (also through its regular API) and after that use the import and export commands to get the data in and out of the emulator.

Prevent wiping data after stopping firebase simulation on localhost

tanx to google , firebase can deploy on localhost for testing. afer stoping simulation all of the states are going to reset and database is wiping out too.
I need to know how to avoid from losing this data and configs.
If you're using Cloud Firestore you can import data to and export data from the emulator suite. For other emulated products such functionality does not (yet) exist, so you will have to ensure you can re-initialize the data in your application code.

Run firebase functions emulator on database without database emulator (towards production cloud instead)

I am having trouble running the firebase functions emulator towards a production database. I have a project which is not publicly released yet so I can run towards production with any negative effects.
My project uses only the realtime database, it does not use Firestore (so other questions on SO are not relevant) The documentation states "Cloud Firestore and Realtime Database triggers already have sufficient credentials, and do not require additional setup." so according to that, I shouldn't need any additional setup in order to point to the production database.
According to all of the documentation on Firebase, the project should run towards the real firebase database if I only start the functions emulator and do not start the database emulator. This warning seems to say so, too:
functions: The following emulators are not running, calls to these services from the Functions emulator will affect production: auth, firestore, database, hosting, pubsub
However, this is not what happens. Instead, I get the following error:
functions[onGlobalClientRequest]: function ignored because the database emulator does not exist or is not running.
I have read the firebase documentation and nothing is really mentioned other than setting credentials should not be needed (but I am setting credentials anyway using export GOOGLE_APPLICATION_CREDENTIALS="/path/to/credentials.json" before running the functions emulator.)
onGlobalClientRequest looks like this:
export const onGlobalClientRequest = functions.database
.ref( client_requests_key + "/{pushedid}")
.onCreate(
async (
snap: functions.database.DataSnapshot,
context: functions.EventContext,
) => {
///.... code here...
},
);
The locally emulated Firebase Functions will be able to write to the Prod database, but will not be able to get triggered by the production database.
Here is a SO answer from a Google employee that states the aforementioned.
Also, quoting from this other SO answer from another Google employee:
In general the rule with emulators:start is that we comprehensively emulate whatever is running. So for example if you're running the Functions and Database emulator, all writes from functions (through admin.database().... will be redirected to the Database emulator. But writes to Firestore (admin.firestore()...) will try and hit production because that emulator is not running.
Operations on a Realtime Database (RTDB) trigger the functions in the corresponding project. So the production RTDB triggers functions of your production project, and the emulated RTDB triggers emulated functions.
If you want to test functions triggered by RTDB operations locally with the emulator, you have to use the RTDB emulator as well. This is explained in the doc here.
If you only want to test HTTP callable functions, then you can use a remote RTDB.

Resources