I wrote a Firebase cloud function to get data from external api and read/write to Firestore. I ran the function locally with 'firebase serve' several times. Then I noticed for each hour ~500 Firestore API Calls were made, even when the function is not invoked. And Firestore Writes and Deletes remains 0, only Reads increased.
The spike in reads stopped only when I shut down my computer so I believe the cloud function is the cause.
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const axios = require('axios');
admin.initializeApp();
exports.someFunc = functions.https.onRequest((request, response) => {
const url = 'https://someurl.com';
const getData = async url => {
try {
const response = await axios.get(url);
const data = response.data;
if (data.status === 'ok') {
data.articles.forEach(element => {
if (element.author) {
element.author = element.author.replace(/\//g, ',');
var authorRef = admin
.firestore()
.collection('authors')
.doc(element.author);
authorRef
.get()
.then(doc => {
if (!doc.exists) {
authorRef.set({
sources: [element.source.name]
});
} else {
var sources = doc.data().sources;
if (!sources.includes(element.source.name)) {
sources.push(element.source.name);
authorRef.update({
sources: sources
});
}
}
return null;
})
.catch(error => {
console.log('Error getting document:', error);
});
}
if (element.source.name) {
element.source.name = element.source.name.replace(/\//g, ',');
var sourceRef = admin
.firestore()
.collection('sources')
.doc(element.source.name);
sourceRef
.get()
.then(doc => {
if (!doc.exists) {
var sourceUrl = 'https://' + element.url.split('/')[2];
sourceRef.set({
sourceUrl: sourceUrl
});
}
return null;
})
.catch(error => {
console.log('Error getting document:', error);
});
}
admin
.firestore()
.collection('articles')
.add(element);
});
}
} catch (error) {
console.log(error);
}
};
getData(url);
response.send('Success!');
});
'authors', 'sources' and 'articles' collection only have at most 20 documents each so there must be something repeatedly calling the Firestore that I could not found.