Reputation: 1845
I am currently working on an iOS app and I'm using Firebase to power it.
Since my app is still relatively small I'm using the database to often perform manual amends on the data. My users can submit places (that I display on a map) and I review entries manually to ensure the data is complete and correct.
I have recently started using GeoFire and thus had to start denormalizing my data for the coordinates (lat & lon) of each places.
As a result I have coordinates at 2 locations in my database
I'm currently looking for a way to automatically update the /geofire side of my database when I update the latitude or longitude of a places on the /places side of the database directly from the Firebase Console.
I'm looking for tips on how to do that. Could Firebase Functions help me for this?
Cheers,
Ed
Upvotes: 1
Views: 705
Reputation: 1845
If someone happens to look for an answer to this question in the future, I followed @J. Doe advice and used Firebase Cloud Functions.
The setup is super simple, steps here.
Here is sample code that let's me update several endpoint of my database when one of my object is updated.
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.placeDataUpdated = functions.database.ref('/places/{placeId}').onUpdate(event => {
const place = event.data.val();
const key = event.params.placeId;
console.log("Updated place data for key: ", key);
var dataToUpdate = {};
dataToUpdate["places_summary/"+key+"/city"] = place.city;
dataToUpdate["places_summary/"+key+"/country"] = place.country;
dataToUpdate["places_summary/"+key+"/latitude"] = place.latitude;
dataToUpdate["places_summary/"+key+"/longitude"] = place.longitude;
dataToUpdate["places_summary/"+key+"/name"] = place.name;
dataToUpdate["places_summary/"+key+"/numReviews"] = place.numReviews;
dataToUpdate["places_summary/"+key+"/placeScore"] = place.placeScore;
dataToUpdate["places_summary/"+key+"/products"] = place.products;
dataToUpdate["places_summary/"+key+"/visible"] = place.onMap;
dataToUpdate["places_GeoFire/"+key+"/l/0"] = place.latitude;
dataToUpdate["places_GeoFire/"+key+"/l/1"] = place.longitude;
return event.data.ref.parent.parent.update(dataToUpdate);
});
It's super convenient and took next to no time to setup.
Upvotes: 2