Skip to content

Instantly share code, notes, and snippets.

@MichaelSolati
Last active December 17, 2018 23:14
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 MichaelSolati/c44d60126044778b5ee15054efc17d44 to your computer and use it in GitHub Desktop.
Save MichaelSolati/c44d60126044778b5ee15054efc17d44 to your computer and use it in GitHub Desktop.
import * as admin from 'firebase-admin';
import * as gf from 'geofirestore';
import * as yargs from 'yargs';
let db: FirebaseFirestore.Firestore = null;
const argv: yargs.Arguments = yargs.argv;
/**
* Flag to indicate whether function is executing in Cloud Functions or locally
*/
let bolServerSideExec: boolean = true
/*___________________________________________________________*/
function initSettings() {
if (db) return;
const serviceAccount = require(argv.serviceaccount);
admin.initializeApp({ credential: admin.credential.cert(serviceAccount) });
db = admin.firestore();
db.settings({ timestampsInSnapshots: true });
} // end initSettings
/*___________________________________________________________*/
async function geoSearch(lat: number, lon: number, radius: number, locationType: string = null): Promise<Object[]> {
initSettings();
return new Promise((resolve, reject) => {
const collectionRef = db.collection('a_geofirestore');
const geoFirestore = new gf.GeoFirestore(collectionRef);
const results: Object[] = [];
const geoQuery: gf.GeoFirestoreQuery = geoFirestore.query({
center: new admin.firestore.GeoPoint(lat, lon),
radius: radius,
query: (locationType) ? (ref) => ref.where('d.locationType', '==', locationType) : null
});
// As documents come in, add the $key/id to them and push them into our results
geoQuery.on('key_entered', function (key, document) {
const doc = (document.locationType === 'gpx') ? modifyGpx(document) : document;
results.push(doc);
});
geoQuery.on('ready', () => {
resolve(results)
geoQuery();
});
});
}
/*___________________________________________________________*/
/**
* Remove author metadata from object, replace with simple document path to user firestore record. Used to reduce payload and unnecessary data back to caller, especially when used in cloud functions
* @param document Firestore document object
* @returns Same object with author reference removed, replacing with simple user id
*/
function modifyGpx(document: any): Object {
document.author = document.author.ref.id;
return document
} // end modifyGpx
/*___________________________________________________________*/
export {
geoSearch
}
/*___________________________________________________________*/
// testQuery(47.4186, -121.4810)
// let dateStart: Date = new Date()
// geoSearch(47.4186, -121.4810, 10, 'gpx').then( results => {
// let dateEnd: Date = new Date()
// let execTime: number = dateEnd.getTime() - dateStart.getTime()
// console.log(`Execution Time: ${execTime}`)
// console.log(`Results: ${results.length}`)
// })
/*___________________________________________________________*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment