You are reading this article, that means you know what the heck is Cloud Firestore. If you don’t, please read Google Firestore Docs — it’s basically just a cloud-hosted, NoSQL database.

Honestly, I really enjoy working with Firestore, especially the ability to sync across client apps through realtime listeners and even offline support for mobile and web. Please notice that, at the time of writing, Cloud Firestore is currently in beta release.

Although I like working with Firestore, there are few missing features that Google team needs to improve like backup and restore firestore for example. Realtime Database has already implemented that. More than that, the query is kind of repeat myself every time I need to work on something, and I don’t like to repeat myself.

So I wrote an NPM package that mitigates the lines of code and improves the features that they are lacking of.

* Remember that this project is still in the development process. If you want to use this in production, it’s at your own risk.

Let’s take a look at how to get a document from Firestore docs

var docRef = db.collection("cities").doc("SF");



docRef.get().then(function(doc) {

if (doc.exists) {

console.log("Document data:", doc.data());

} else {

// doc.data() will be undefined in this case

console.log("No such document!");

}

}).catch(function(error) {

console.log("Error getting document:", error);

});

I guess you have repeat everything when you want to get a another document. Now let’s take a look at the process of getting a document from firebase functions helper

firebaseHelper.firestore

.getDocument(db, 'collection-name', 'document-id')

.then(doc => console.log(doc));

This is much cleaner in my case, and I already did all the catching things for you ;)

Now, if you are still interested in this project. Please give it a try, report bugs and make feature requests in the Issue Tracker, fork and create pull requests!

Install Firebase Functions Helper

npm install firebase-functions-helper

2. Initialize Firebase Application

const firebaseHelper = require('firebase-functions-helper');

const serviceAccount = require('./serviceAccountKey.json'); // Initialize Firebase App

const app = firebaseHelper.initializeApp(serviceAccount, databaseURL); // Get Firestore DB

const db = app.firestore;

After getting firestore ready, there are bunch of things that you do with this package such as:

2.1 Get Firestore Collection with Sub Collection

// Start exporting your collection

firebaseHelper.firestore

.backup(db, 'collection-name', 'sub-collection-optional')

.then(data => console.log(JSON.stringify(data)))

With this feature you can get and loop through all of your documents from a collection

firebaseHelper.firestore

.backup(db, 'collection-name')

.then(data => {

let docs = data['collection-name'];

for (const key in docs) {

if (docs.hasOwnProperty(key)) {

console.log('Doc id: ', key);

console.log('Document data: ', docs[key])

}

}

})

2.2 Import data to firestore

// Start exporting your data

firebaseHelper.firestore.restore(db, 'your-file-path.json');

The JSON is formated as below. The collection name is test. first-key and second-key are document ids.

{

"test" : {

"first-key" : {

"email" : "dungnq@itbox4vn.com",

"website" : "dalenguyen.me",

"custom" : {

"firstName" : "Dale",

"lastName" : "Nguyen"

}

},

"second-key" : {

"email" : "test@dalenguyen.me",

"website" : "google.com",

"custom" : {

"firstName" : "Harry",

"lastName" : "Potter"

}

}

}

}

2.3 Create a document with id in firestore

firebaseHelper.firestore

.createDocumentWithID(db, 'collection-name', 'document-id', data);

2.4 Create a new document without an ID

firebaseHelper.firestore

.creatNewDocument(db, 'collection-name', data);

2.5 Update a document

let data = {key: value};

firebaseHelper.firestore

.updateDocument(db, 'collection-name', 'document-id', data);

2.6 Delete a document

firebaseHelper.firestore

.deleteDocument(db, 'collection-name', 'document-id');

2.7 Check whether a document exists

This will return a Promise<boolean>

firebaseHelper.firestore

.checkDocumentExists(db, 'collection-name', 'document-id')

.then(exists => console.log(exists));

2.8 Query data from firestore

This will return a Promise<array> of documents

// Search for data ( <, <=, ==, >, or >= )

const queryArray = ['website', '==', 'dalenguyen.me']; firebaseHelper.firestore

.queryData(db, 'collection-name', queryArray)

.then(docs => console.log(docs));

If you are interested in this, please check my github repository for updates: https://github.com/dalenguyen/firebase-functions-helper