Before starting with Firestore, we have to initialize Firebase App and get Firestore DB.
const {firebaseHelper, firestoreHelper} = 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;
db.settings({ timestampsInSnapshots: true });The result will be a Promise that returns content of a document or false if document doesn't exist.
firestoreHelper
.getDocument(db, 'collection-name', 'document-id')
.then(doc => console.log(doc));Backup feature use the package (firestore-export-import)[https://www.npmjs.com/package/firestore-export-import]
// Start exporting your collection
firestoreHelper
.backup('collection-name')
.then(data => console.log(JSON.stringify(data)))With this feature you can get and loop through all of your documents from a collection
firestoreHelper
.backup('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])
}
}
})This code will help you to import data from a JSON file to firestore.
Restore feature use the package (firestore-export-import)[https://www.npmjs.com/package/firestore-export-import]
// Start exporting your data
firestoreHelper.restore('your-file-path.json');This will return a Promise<{status: boolean, message: string}>.
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"
}
}
}
}firestoreHelper
.createDocumentWithID(db, 'collection-name', 'document-id', data);This will return a Promise.
firestoreHelper
.createNewDocument(db, 'collection-name', data)
.then(docRef => console.log(docRef.id));This will return a Promise. You can get the document id from that.
This action will only update the current key inside the document, if the data doesn't exist, it will create a new one. However, it will not create a new document if the document doesn't exist in the first place.
let data = {key: value};
firestoreHelper
.updateDocument(db, 'collection-name', 'document-id', data);This will return a Promise. True if success and will throw an error if fail.
firestoreHelper
.deleteDocument(db, 'collection-name', 'document-id');This will return a Promise<{status: bolean, message: string}>
This will return a Promise and the document data if possible
firestoreHelper
.checkDocumentExists(db, 'collection-name', 'document-id')
.then(result => {
// Boolean value of the result
console.log( result.exists ); // will return true or false
// If the document exist, you can get the document content
console.log( JSON.stringify(result.data) ); // return an object of or document
});This will return a Promise of documents.
The orderBy parameter is OPTIONAL.
// Search for data ( <, <=, ==, >, or >= )
const queryArray = [['website', '==', 'dalenguyen.me'], ['email', '==', 'dungnq@itbox4vn.com']];
const orderBy = ['email', 'desc'];
firestoreHelper
.queryData(db, 'collection-name', queryArray, orderby)
.then(docs => console.log(docs));This will return a Promise of documents.
The orderBy parameter is OPTIONAL.
// Search for data ( <, <=, ==, >, or >= )
const queryArray = [['website', '==', 'dalenguyen.me'], ['email', '==', 'dungnq@itbox4vn.com']];
const orderBy = ['email', 'desc'];
const size = 10;
const page = 1;
firestoreHelper
.queryDataWithPagiation(db, 'collection-name', queryArray, orderby, page, size)
.then(docs => console.log(docs));If you want to add more features, please make a request in the Issue Tracker