Yes we can: Writing Firestore Admin functions in Dart, and using the Firestore Emulator


Image result for firebase logo

Thanks to the awesome Firestore Admin Interop library,  you can write Firestore admin functions in Dart (and yes Googlers, if you are reading this, we *really* need a native Dart Firebase SDK).

If you are curious how the interop library works: The dart2js compiler translates your Dart code to Javascript. The interop library wraps the native nodejs library for Firestore,  allowing you to call it from your Dart program. See here for information on the Admin SDK.

As a nodejs and Firestore newbie, there were a couple of missing pieces for me on how to use this library:
  • How do I wire this into my Firestore project?
  • How do I use the local Firestore emulator?
I won't cover this in depth (please see the example ), but here are few tips:
  • The library is wired to your Firestore project via the service-account.json file that you download from the Firebase console (Project Settings > Service Accounts). Download and save this file (and DO NOT check it in to git !!)
  • You do *NOT* need to have your program open the service account file (the example from the interop library does this, but I do not think it is required). The canonical way to pass a service account to your program is to set the environment variable:
 export GOOGLE_APPLICATION_CREDENTIALS=path/to/your/service-account.json.
  • Then, to initialize your application:
final admin = FirebaseAdmin.instance;
final app = admin.initializeApp(); // this is all you need
// start using app.firestore() functions. They will be wired to your projects DB

Using the emulator also turns out to be simple once you know the trick.  Set the following environment variable:

export FIRESTORE_EMULATOR_HOST=localhost:8080

Start the emulator in another shell window:

firebase emulators:start


If you want to switch back to the hosted Firestore, unset FIRESTORE_EMULATOR_HOST.

Comments

Popular posts from this blog

Introducing ds-operator, the ForgeRock Directory Services Operator for Kubernetes

Automating OpenDJ backups on Kubernetes

Deploying the ForgeRock platform on Kubernetes using Skaffold and Kustomize