Should I query my Firebase database directly, or use Cloud Functions?

Doug Stevenson
Jun 2 · 10 min read
const firestore = firebase.firestore()
const snapshot = await firestore
.collection("posts")
.orderBy("lastModified")
.limit(100)
.get()
const admin = require("firebase-admin")
const firestore = admin.firestore()
exports.getLatestPosts =
functions.https.onRequest(async (req, res) => {
const snapshot = await firestore
.collection("posts")
.orderBy("lastModified", "desc")
.limit(100)
.get()
res.send(snapshot.docs.map(doc => doc.data()))
})

Performance

Local caching

Payload size

Price

Security and permissions

Realtime data

const firestore = firebase.firestore()
const unsubscribe = firestore
.collection("posts")
.orderBy("lastModified")
.limit(100)
.onSnapshot(querySnapshot => {
// this gets called whenever the results
// of the query change over time
})

Exposing a public API

So, which should you choose?

Firebase Developers

Tutorials, deep-dives, and random musings from Firebase developers all around the world. Views expressed are those of the authors and don’t necessarily reflect those of Firebase or its parent companies.

Doug Stevenson

Written by

Google Developer Advocate with the Firebase team

Firebase Developers

Tutorials, deep-dives, and random musings from Firebase developers all around the world. Views expressed are those of the authors and don’t necessarily reflect those of Firebase or its parent companies.