Skip to main content

JavaScript queries

info

JavaScript queries are only supported for Cloud Firestore and Realtime Database.

For some databases Ezfire provides a JavaScript-based interface for writing queries. This interface leverages the existing Node.js SDKs for the given database to provide a familiar query-writing exprience for databases without a natural query language.

What is a JavaScript query?

Generally speaking, in Ezfire, a JavaScript query is any JavaScript expression that returns a Promise. In particular, JavaScript queries run in a specialized environment with only access to the following globals:

GlobalTypeDescription
dbDepends on the database. See the relevant connection guide.Exposes the API used to query the curent database. See the relevant connection guide for more information on the API.
resultanyThe result of the previous query expression in the current execution. See workbooks with multiple queries for more information.

The db global available in a JavaScript query will adapt to the currently selected connection. It can be used to access the JavaScript API for the given database to write expressions that return promises.

When running a query with multiple query expressions, the result global provides the result of the previous expression.

Workbooks with multiple queries

Expressions in your workbook separated by the ; character will be run in serial as separate queries to the database, but as part of the same query execution on Ezfire. Using the result global in later query expressions allows the execution of queries that require the manual joining of data. Consider this example from Cloud Firestore:

// Find a user by email.
db.collection("users")
.where("email", "==", "robert@speedwagon.foundation")
.limit(1)
.get();

// Find that user's posts by id.
db.collection("posts").where("userId", "==", result[0].id).get();

Since Cloud Firestore is a document database and does not support joins, they must be done manually. In the first query, we look up a user's document by email. In the second query, we manually join the posts collection by using this user's id from the result global to query for that user's posts.

Examples

Below are some examples of valid JavaScript queries:

// A simple promise is enough to run as a query.
Promise.resolve("Hello World!");

// Querying data from the firestore database, i.e. db has Firestore API.
db.collection("foos").doc("bar").get();

// Querying data from the realtime database, i.e. db has Realtime Database API
db.ref("foos/bar").get();