Skip to content

Instantly share code, notes, and snippets.

@zeusbaba
Last active August 17, 2020 21:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeusbaba/2815163075d731588f895e53a422babb to your computer and use it in GitHub Desktop.
Save zeusbaba/2815163075d731588f895e53a422babb to your computer and use it in GitHub Desktop.
Example cloud function that handles processing Firebase DB data into Firestore
//this cloud function listens to new records,
// then process them to create models aligned with App logic which is using Firestore.
function firestoreRecord_parkingnorway(dbRecord) {
let firestoreRecord = {};
// this method creates a mapped version according to app logic
// etc etc etc ....
return firestoreRecord;
}
exports.parkingNorway = functions
.runWith(runtimeOpts)
.region(regionName)
.database.ref('/parking_norway/{recordId}')
.onCreate(async (snap, context) => {
const dbRecord_parkingnorway = snap.val();
console.log('...newDbRecord with recordId:', context.params.recordId, JSON.stringify(dbRecord_parkingnorway));
let firestoreRecord = firestoreRecord_parkingnorway(dbRecord_parkingnorway);
// Create a GeoFirestore reference
const geofirestore = new GeoFirestore(firebaseAdmin.firestore());
// Create a GeoCollection reference
const geocollection = geofirestore.collection('parking_norway');
await geocollection.add(firestoreRecord);
return true;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment