Setup of a Cloud Firestore Database tutorial (step by step)

AndroidCrypto
5 min readDec 24, 2023

--

The Firebase environment needs a setup for each product, and in this article I’m setting up the Cloud Firestore Database product. It is part 4 of an article series about the setup of the most used Firebase products. It is based on the first tutorial “Setup of a Firebase project tutorial (step by step)” — please check that you are ready to add a product to a project. There is no code for an Android or Web app (see the links to my other articles for this topic. As most apps require a user authentication you should read the tutorial about the “Authentication” product as well.

What is Firebase ? Firebase is a set of backend cloud computing services and application development platforms provided by Google. It hosts databases, services, authentication, and integration for a variety of applications, including Android, iOS, JavaScript, Node.js, Java, Unity, PHP, and C++. (source: Wikipedia).

You may have noticed that this is the tutorial for Cloud Firestore Database and another for Firebase Realtime Database, is that just a typo error ? No, it isn’t. The Firebase RT Database is the older one, Firestore Database is younger, but both of them are still under development. This is the “official” answer by Google’s Firebase team (see complete answer on https://firebase.google.com/docs/database/rtdb-vs-firestore):

Firebase offers two cloud-based, client-accessible database solutions that support realtime data syncing:

  • Cloud Firestore is Firebase’s newest database for mobile app development. It builds on the successes of the Realtime Database with a new, more intuitive data model. Cloud Firestore also features richer, faster queries and scales further than the Realtime Database.
  • Realtime Database is Firebase’s original database. It’s an efficient, low-latency solution for mobile apps that require synced states across clients in realtime.

Although Firebase products seem to be “free” there a (generous) limits for all products (e.g. on Storage size) and they differ between the Firebase RT Database and the Cloud Firestore Database, for details see https://firebase.google.com/pricing.

If you want to setup Cloud Firestore Database, you are right here. Choose the product:

Click on the button to proceed:

Click on “Create Database” to proceed:

We need to choose at what server location the database should run — chose carefully as you cannot change this setting at a later time. As I’m located in Germany I have chosen “eur3 (Europe)”. Click on “Next”:

The second step is about Security rules and I don’t really like the two possible options:

  • start in start in production mode: means create an “unusable database” because no data can get appended, deleted or even read. Choose this option if you know what individual rules need to get setup in a following step.
  • start in test mode: means the database will allow an unlimited read and write access without any limitations. As this setup is insecure the database has a time limit of 1 month. This option is good for testing purposes but there is a high risk that you forget about the time limit and
    are surprised when your application stops to work properly.

At this time the database is empty, but you should click on the “Rules” tab to edit the “locked mode”. Change the existing (locking) rule from:

rules_version = '2';

service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if false;
}
}
}

to this values, meaning that only authenticated users are allowed to read and write to documents. This means on the other hand: every authenticated user can edit every document; a better solution could be that all user can read every document, but only the user who had created a document is allowed to edit the data. For more information about “rules” see https://firebase.google.com/docs/database/security. If you want to “play” with rules visit the “Rules Playground”.

rules_version = '2';

service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if request.auth != null;
}
}
}

Click on “Publish” to use the new rules.

And that’s it, your new database is ready to use.

If you just needed an authentication service and the database for your project you are ready to download the “google-services.json” file, move it to your app folder and start developing your app.

Article series about Firebase:

  1. Setup of a Firebase project
  2. Setup of Firebase Authentication
  3. Setup of a Firebase Realtime Database
  4. Setup of a Cloud Firestore Database (this article)
  5. Setup of Firebase Storage
  6. Finish the setup of the Firebase project for an Android app tutorial (step by step)
  7. Finish the setup of the Firebase project for a Web app tutorial (step by step): will follow soon
  8. Firebase tutorial for an Android app (Java): will follow soon
  9. Firebase tutorial for a Web app (Javascript): will follow soon

--

--