Non-relational databases, and in particular, document databases like Cloud Firestore and Firebase Realtime Database are popular for a reason. They are flexible, easy to use and also easy to scale out horizontally for large workloads. These benefits don't come for free however, and sometimes a developer needs to make multiple trips to the database to fetch all the required data to solve a problem. Since our release however, Ezfire has only supported running a single query at a time, making these use cases manual, cumbersome and sometimes not possible!
That's why we are excited to announce support for query workbooks in Ezfire. Query workbooks are an extension to the queries already present in Ezfire, but they allow the execution of multiple queries in a single run. With query workbooks, you can easily orchestrate complex data fetching workloads across multiple queries. Let's take a closer look at one of the primary use cases.
Using Query Workbooks for Manual Joins
In a relational database, joins are used to select data across multiple tables in a single query. While convenient, supporting joins imposes some restrictions on how data is store in order to enable this kind of querying. For most document stores like Cloud Firestore and Firebase Realtime database, related data is intended to be collocated in a single document. As such, joining data across collections is not supported in order to make horizontal scaling easier. However, sometimes joins are unavoidable and must be done manually on the application side.
To make this more concrete, consider the example of a Cloud Firestore database where we have a users
collection and a posts
collection holding a given user's posts. If we know a user's id, we can easily find all their posts with the following query:
db.collection("posts").where("userId", "==", userId).get();
However, what do we do if we only know a user's email? We need to manually join the data using two queries! Before query work books, performing a manual join like was not possible. With Ezfire query workbooks, this is simple. All expressions in the query workbook separated by a semicolon character are executed in serial as part of a single run. By using the result
global in you query, you can access the result of the previous query in the series.
Putting this altogether, we can write a query workbook to fetch a user's posts by email in the following way:
db.collection("users").where("email", "==", email).limit(1).get();
db.collection("posts").where("userId", "==", result[0].id).get();
This workbook will first fetch the user by email, then use the user's id to fetch the associated posts.
There is no limit on the number of query expressions you can have in a single workbook, so you can perform an arbitrarily complex join you need.
For more information on workbooks, check out using workbooks in our docs.
Limitations
Currently, query workbooks are limited to JavaScript queries only. We will be adding support for other query languages soon.
Conclusion
With the introduction of query workbooks, many data fetching scenarios that previously were not possible in Ezfire have been unlocked with a simple and easy to use interface. We are excited to what you can build with query workbooks. If you have any further questions, don't hesitate to contact us and we will help you as soon as possible.