Yes we can: Writing Firestore Admin functions in Dart, and using the Firestore Emulator
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?
- 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
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