What is the difference between admin.firestore, admin.firestore() and getFirestore()

Viewed 64

The first few lines of my cloud functions code is like this:

// The Cloud Functions for Firebase SDK to create Cloud Functions and setup triggers.
const functions = require('firebase-functions');

// The Firebase Admin SDK to access Cloud Firestore.
const admin = require('firebase-admin');

admin.initializeApp();
const db = admin.firestore();

I'm using the constants auth and db in my code like this:

const documentReference/collectionReference = db.collection(...)...;
await admin.auth().setCustomUserClaims(user.uid, { adminOf: languages });
t.update(docRef, {
  [`field.${data.innerField}`]: admin.firestore.FieldValue.delete(),
  anotherField: admin.firestore.FieldValue.arrayRemove(data.someData),
  yetAnotherField: admin.firestore.FieldValue.arrayUnion(data.someOtherData),
});
const batch = await db.batch();

and everything works well. Yet, it took me some time and guessing to get this right, as I find the documentation for how to set up firebase is confusing.
Notice the uses of both admin.firestore and admin.firestore(). Can someone please explain to me what's the difference? I won't touch that code as it works.

Get started with Cloud Firestore (the official docs):
First of all, I have no idea what the difference between Initialize on Cloud Functions and Initialize on Google Cloud Platform should be. Both examples use differenct require(...)s than me, and const db = getFirestore();. When I tried to do that, it didn't work. getFirestore() is explained here

And honestly, I have no idea about the source(s) where I found to initialize my cloud functions code the way I did. It was long ago. Here's another example of someone who has the same initialization code.

To create a batch:
admin.firestore.batch didn't work.
admin.firestore.batch() didn't work.
const db = getFirestore(); //when I want to firebase deploy -> "ReferenceError: getFirestore is not defined"

Finally, only this worked:

const db = admin.firestore();
const batch = await db.batch();

One last thing:
in the official docs, they just do it like this:

FieldValue.arrayUnion('greater_virginia')
FieldValue.arrayRemove('east_coast')
FieldValue.arrayUnion('south_carolina', 'texas')

yet I have to do:

admin.firestore.FieldValue.arrayRemove(data.someData),

Why does this all have to be so confusing? Why is there not just ONE correct way to do it all, which is also written in the Firebase docs?

(I checked all urls the day I posted this question, as the content of those webpages may change in the future -> can use wayback machine then)

1 Answers

From V10+, the Admin SDK also offers a Modular API just like the Firebase JS SDK.

// name-spaced version
const admin = require("firebase-admin");
admin.initializeApp();

// Modular Version
const { initializeApp } = require("firebase-admin/app");
initializeApp();

The Firebase Client SDK still has a compat version that let's you use the older name-spaced syntax but that'll bne removed eventually so it's recommended to use the new Modular API.

The Admin SDK similarly let's you use both name-spaced and Modular syntax however it's not totally modular yet. It only offers get[SERVICE]() functions as of now.

const { getFirestore } = require("firebase-admin/firestore");
// const { getSERVICE } = require("firebase-admin/SERVICE");

In your case, do ensure that you are importing getFirestore() from firebase-admin/firestore .

Related