Related Blogs
1. Introduction to Firebase – Cloud Firestore
Google is a frontrunner in technology innovation with a countless number of products and services offered to businesses. Firebase is the platform provided by Google to develop serverless applications, specifically used for mobile applications. Firebase provides different types of services like Authentication, Storage, Database, Analytics, Hosting, etc. Here, we are going to discuss the Firebase database. Firebase provides two types of database solutions.
- Realtime Database: Realtime database is Cloud hosted database provided by firebase which allows you to store and sync data in real-time to your users.
- Cloud Firestore: Cloud Firestore is the latest database provided by Firebase after a real-time database. It facilitates users to store, sync and query databases.
Firestore is a scalable database provided by firebase and google cloud platform. It keeps your data in sync and provides offline support for mobile and web development which works without dependency on network or internet connectivity. Firestore provides integration with other google cloud platforms, also you can create cloud functions to query the firestore database.
2. Key Features of Firebase – Cloud Firestore
- Designed to Scale: Firestore is designed to scale very large data structures. It is capable of handling large workloads. It provides the best infrastructure with multi-region data replication, consistency, transaction support and batch operations.
- Real-time updates: Firestore offers businesses real-time data synchronization that would timely update data in all connected devices.
- Offline Supports: Firestore provides offline support to databases. So, you can read, write and query databases even when you are offline. It will synchronize data again when you are online and connected.
- Expressive querying: Firestore allows queries database, where you can read/write a single document as well as you can fetch a list of documents with sorting and filter. You can include multiple filters in your query. Firestore uses indexes in the querying database for better performance.
Firestore requires creating indexes for better query performance. Indexes required are created automatically when you run a query. It will throw an error and provide you a link to create indexes. Error Message includes a link to Create Index, You just need to hit that on the browser and it will provide you details about the index to create. Firestore supports Single-field as well as composite indexes.
Jason-like structure is used by firestore to store our database that has a key and value pair. It supports hierarchical and flexible data structures. Data is stored in documents that have field mapping of values. All the documents are stored in a container that is called Collection. Collections are used to organize data and create queries for data operation. We can also create sub-collections within documents.
Below is the list of supported data types by firestore:
- String
- Number
- Boolean
- Map
- Array
- Null
- Timestamp
- Geo point
- Reference
3. Firestore Function Triggers
For every change in the database, Firestore provides the ability to create a handler and effectively administer the change. Users can create a handler on the database by using create, update or delete functions. Below are some of the databases that trigger support by the Firestore.
- Create Document: Firestore has onCreate trigger option available which is called whenever a new document is created.
- Update Document: Firestore has onUpdate trigger option which is called whenever the document needs to be updated.
- Delete Document: Firestore has an onDelete trigger option which is called when a document is to be deleted.
- General Document trigger: Firestore has onWrite trigger which is called when a document is created, updated or deleted.
4. Query Limitations of Cloud Firestore
Firestore has few limitations on querying databases. It is tough to execute complex queries on the Firestore database.
- Users can use a range of multiple filters that are supported by firestore. The major filters that are supported in firestore are like, >, < etc.
- Special queries with “!=” are not supported in Firestore.
- The Logical OR queries are not supported by Firestore.
- There are limited alternatives to OR query. You can either use in the query or n array-contains-any operators as logical OR but it supports up to 10 values only. Developers can use array-contains conditions on a single field for more effectiveness. For other remaining cases, users can create a unique query for each OR condition and can merge the results into the app.
- You cannot use In and array-contains-any in a single query.
- Users can use array-contain within query but cannot use it with array-contain-any
- Sorting is not allowed on any field which is included In In clause or (=) equal clause.
5. Things to Understand Before Using Firestore for Your Application
Here are a few real-time issues and some possible solutions that may simplify the administration while using firestore to create cloud functions for your app
- In an app development company, if you want to develop mobile application all you need is a unique Username. Whenever any new user SignUp, you will need to check your database whether the Username entered by the new user is available in the database or not. For that, you will need to compare that entered Username with records already existing in the database. But as the firestore is case-sensitive, It will not show you the result if the same Username is already used but with a different case.For example, if there is a user that already exists on the database with a Username say “FirebaseAdmin” and someone is trying to create a new user with “firebaseadmin”. When you query to check the database for Username “firebaseadmin”, it will return no results as firestore is one is written in uppercase and other in Lower case. To fix this, you will need to add a new field on the database which always has the UpperCase or LowerCase value of Username. You can use that new field to compare Username by converting user-entered username to UpperCase or LowerCase.
- One more major issue with case-sensitivity of firestore is Sorting data. Suppose you have some application where you are displaying the list of users sorted as per their names. It’s possible that some users may have entered the data in LowerCase while others have entered in UpperCase. So, you must have full names of all the users in the database which has some records starting with UpperCase and some of them are LowerCase. Let’s take some random user list to understand this.
- Evangelia Escalera
- Pancho Presnall
- sunshine Orcott
- Herta Trevon
- Gunter Papaccio
- Madelon Castelijn
- Saul Pigdon
- Briggs Josipovic
- Othelia Choudhury
- Archibold Ogan
Suppose, we need above to list and sort it in ascending order with name, what firebase will return is as below:
- Briggs Josipovic
- Gunter Papaccio
- Herta Trevon
- Madelon Castelijn
- Othelia Choudhury
- Pancho Presnall
- Saul Pigdon
- Archibold Ogan
- Evangelia Escalera
- sunshine Orcott
So, this is not what you expected to display on your app. For fixing this, you will probably need to store the name of the user as either UpperCase or LowerCase or take another field on the firestore document to store this value, which will only be used for Sorting data.
- Next issue is related to searching data, which is the most general feature that every app might have. Firestore doesn’t support “Contains” or “Like” queries like other databases have. Suppose, you have a list of companies in your database, and if a user wants to implement a search filter on the Company List Page on your app then they will not be able to implement this easily with where condition on your cloud function. Suppose, there is one company “sysgenpro” in your database and you want to get this listed when someone searches with “Tatva” or “soft”. With Firestore database it is easy to implement this.You will need to store each letter combination as an array to the firestore database. You will need to add a new array field to store “sysgenpro” for searching and you need to store all possible combinations to that array.
- “T”
- “Ta”
- “Tat”
- ….
- ….
- “sysgenpro”
- ….
- ….
- “a”
- “at”
- “atv”
- ….
- …
- “atvasoft”
- And so on……
As you can see, you will need to store all the combinations to an array and you can use the “array-contains” or “array-contains-any” clause. One more complexity here is, if someone tries to search with “TATVA”, it will still not return any results as the firestore is case sensitive. So, in that case, you will need to store all the above combinations to UpperCase or LowerCase and convert search text to the same case to get the results. Again, you will need to keep in mind that a firestore allows a maximum of 1MB of data per document.
Firebase also suggests using third-party services like “Algolia” or “Elastic Search” to implement search in your application. But, again it will add extra time and cost to implement it and manage. Because you will always need to keep third-party service data updated whenever anything is updated from your app.
- Another issue is, you can’t have multiple Firestore databases associated with a single project. So, if you want to create a separate firestore database for development, you need to create a separate project. It won’t be easier to migrate data from Development to Production easily, because there is no automated process available for that. Because there is no automated backup process for your firestore database developers need to do this manually. You need to export the database and you can import it to your project. Again, whatever document you export, it will be considered in Reading counts and it will be billable.
- Another thing is firestore charges you per document when you read/write/delete data. As it does not support complex queries for filtering, sorting and merging of data, It will always lead you to fetch all data and filter it on the client-side. In the end, this will increase the overall project cost in terms of development time as well firestore billing.
6. Conclusion
To sum up, we would say there are numerous Pros and Cons of using the Firestore as a database. Firestore is an optimized database used in user cases but it cannot be used in all situations. There is a scope of some workaround to overcome these limitations. Hence, users can use Firestore with another database solution simultaneously for more effective results. While using Firestore, the most important thing is your database structure. Whether you want to develop an Android app or iOS app Firebase can provide you a great platform. You should design your data structure in a way that fits your needs and helps you take advantage of the Firestore and overcome the limitations.
Vishal Shah
Vishal Shah has an extensive understanding of multiple application development frameworks and holds an upper hand with newer trends in order to strive and thrive in the dynamic market. He has nurtured his managerial growth in both technical and business aspects and gives his expertise through his blog posts.
Subscribe to our Newsletter
Signup for our newsletter and join 2700+ global business executives and technology experts to receive handpicked industry insights and latest news
Build your Team
Want to Hire Skilled Developers?
Comments
Leave a message...