How to create multi environment DB's with Firestore

13,808

Solution 1

Firebase doesn't support the use of multiple Firestore instances in a single project.

The Firebase team strongly recommends creating different projects for each of your environments to keep them separate. This probably means you will have to build different apps that each point to different instances, or you will have to somehow configure your app at runtime to select which project you want to work with.

There is no obligation to add billing to any of the projects you don't want to pay for, as long as you accept the limitations of the Spark plan.

Solution 2

Yes Firebase doesn't support multiple instance in a single project. However my case, i created 3 different document in Firestore root and setup each environment to refer these documents accordingly. Each document will have the same collections, sub collections and documents.

For example,

dev -> users -> details
    -> others_collection -> details

stag -> users
     -> others_collection -> details

prod -> users
     -> others_collection -> details

On the client side, each environment will get the collection like this :

db.collection(`${env}/users`)

I am doing this on small project and seem pretty happy with what Firestore provided. In single project. i can create many ios/android apps according to the environment and each environment have it own document and collections in Firestore.

Solution 3

The safest way is to create a new google cloud project. You can export your current firestore database to your new project if needed.

Step by step:

  1. Backup your current database by exporting all your documents into a bucket: https://firebase.google.com/docs/firestore/manage-data/export-import

    gcloud beta firestore export gs://[BUCKET_NAME]

  2. Create a new project -> https://cloud.google.com/resource-manager/docs/creating-managing-projects

  3. In the dashboard, select Firestore into your new project and create an empty database
  4. In the dashboard, go to your BACKUP_BUCKET and add your new project service account in the permission tab

  5. Switch project to your new project gcloud config set project 'new-project'

  6. Then export your data gcloud beta firestore import gs://[BUCKET_NAME]/[EXPORT_PREFIX]/ The EXPORT_PREFIX being the name of the folder created in your bucket.

I use this method to have clearly separated 'preprod' and 'production' environments (the team can access and synchronize with production without having access to the production environment codebase and configuration).

Share:
13,808
Dyan
Author by

Dyan

Updated on June 03, 2022

Comments

  • Dyan
    Dyan about 2 years

    I've been looking at how to create multiple Firestore instances in Firebase, I need different Db's for prod, staging and development. I read the documentation and seems that I only need to modify the "Google-services.json" file in the applications. what I don't get is what should I modify and how that would look in the console.

    • Am I going to see different instances in the same project?

    • I need to create different projects for every environment and modify those values in the file?

    • If I need to test something that requires testing in all environments and all of them require Blaze to run that test do I have to pay Triple?

    Thanks in advance

  • AdamHurwitz
    AdamHurwitz over 5 years
    Thanks @DougStevenson! I'm refactoring my Firestore project accordingly to prepare for prod. Any plans in the road map for multiple Firestore dbs in one project similar to Firebase?
  • Doug Stevenson
    Doug Stevenson over 5 years
    @AdamHurwitz Realtime Database allows you to add multiple databases because there are limits to its scalability, and you need to shard your data across multiple DBs when you reach those limits. Firestore doesn't have those limitations, so there's no need for multiple DBs. You'll have to create different projects for each instance, if that's what you really need.
  • RonTLV
    RonTLV over 5 years
    Thanks Doug. Is it legit to use two Cloud Firestore projects for one app ?
  • Doug Stevenson
    Doug Stevenson over 5 years
    @RonTLV Sure, there's nothing stopping you from doing that, if that's what you really need to do.
  • RonTLV
    RonTLV over 5 years
    Two projects means two google-services.json files. How do I handle that ?
  • Doug Stevenson
    Doug Stevenson over 5 years
    @RonTLV It doesn't mean that at all. Do a search for "firebase multiple projects in one app" for some walkthroughs on how it can be done. You will have to write some init code. Auth may be kind of a pain.
  • Jschiff
    Jschiff over 5 years
    This is convenient from a development perspective, but it seems less than ideal from a security standpoint. Any client from any of your environments can now access all the data from any of your other environments!
  • zzas11
    zzas11 over 5 years
    Understood your concern. I think the rules help to increase the security and prevent users to access other env.
  • Andrey Gordeev
    Andrey Gordeev about 5 years
    Export/import requires billing enabled. Which doesn't make sense for staging environment
  • Quentin C
    Quentin C about 5 years
    Depends of the size of your company I guess. Actually I don't understand your point, why billing shall be disabled in staging?
  • Ruben
    Ruben over 4 years
    why is Multitenancy not better supported? or at least documented. I'm looking for a solution using angularfire but I just can't find an answer
  • Doug Stevenson
    Doug Stevenson over 4 years
    @Ruben If you have questions about why something is the way it is for Firebase products, you should post to the Firebase discussion group. groups.google.com/forum/#!forum/firebase-talk
  • Teodor Ciuraru
    Teodor Ciuraru almost 4 years
    Any other drawbacks related to this approach?
  • user8467470
    user8467470 almost 4 years
    Would you take the same approach with a database per service for microservices?
  • Fahmiin
    Fahmiin over 3 years
    Where do I store the env variable? Does Firebase automatically detect this?
  • Wesley Barnes
    Wesley Barnes over 3 years
    How do you prevent yourself from writing data to the wrong env? I can just imagine when you forget and do a write to the db by accident
  • rolznz
    rolznz about 3 years
    This will not easily work with firestore functions unless you copy your function code so you have differently named functions for each environment, which I would not recommend. Separate projects seems much safer.