Last active
August 17, 2020 21:06
-
-
Save zeusbaba/2815163075d731588f895e53a422babb to your computer and use it in GitHub Desktop.
Example cloud function that handles processing Firebase DB data into Firestore
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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