Write raw Firebase queries

Learn how to write queries directly against the Firebase Admin SDK in Retool.

You can write queries against the Firebase Admin SDK if you have existing Firebase queries, or if you want to work more granularly with firebase-admin. If you haven't created a Firebase resource yet, see the Connect to Firebase guide before continuing.

1. Create a Firebase query

Get started by creating a Firebase query and selecting Raw mode in the dropdown.

Selecting Raw mode in the query editor

2. Write the query

In the query editor, you can write JavaScript queries which call the firebase-admin SDK. In Raw mode, you don't need to use curly braces {{ }} to reference Retool data such as textinput.value.

Using raw mode to write a query

🚧

Data limitations

Retool limits the amount of data queries can access to about 6 megabytes.
Try dividing data into chunks using a JavaScript query first, and then reference those pieces in the Firebase raw query.

Predefined Firebase variables

You can use the following predefined variables in Firebase queries.

VariableFirebase docs
dbadmin.database
db.firestore()firestore.Firestore
db.auth()auth.Auth
firestoreadmin.firestore

The admin.firestore package contains mostly static helper methods. To perform actions on your Firestore database, use db.firestore().

Example Firebase queries

Below are some example queries you can use if you aren't sure where to start with Firebase queries.

📘

Remember to await and return any promises in order to see the results of your query.

Query a collection selected in a Select component

const snapshot = await db.firestore().collection(select1.value).limit(1).get();
return snapshot.docs.map((d) => d.data());

Insert a record

const data = {
  stringExample: "Hello World",
  booleanExample: true,
  numberExample: 3.14159265,
  dateExample: firestore.Timestamp.fromDate(new Date("December 10, 1815")),
  arrayExample: [5, true, "hello"],
  nullExample: null,
  objectExample: {
    a: 5,
    b: true,
  },
};

return await db
  .firestore()
  .collection("myFirestoreCollection")
  .doc("one")
  .set(data);

Upload a batch of documents

// Get a new write batch
var batch = db.firestore().batch();

// Try replacing this sample data with a reference to a component or a query `const data = query1.data`
const data = [
  {
    name: "Max Conversation",
    email: "[email protected]",
    sales: 5,
  },
  {
    name: "Sally Smith",
    email: "[email protected]",
    sales: 12,
  },
];

data.forEach((r) => {
  var ref = db.firestore().collection("myCollection").doc(r.email);
  batch.set(ref, {
    name: r.name,
  });
});

// Commit the batch
return await batch.commit();