codediodeio / geofirex

:globe_with_meridians: :round_pushpin: Geolocation Queries with Firestore & RxJS

Home Page:https://geo-test-c92e4.firebaseapp.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Possibility to use inside cloud functions

Parez opened this issue · comments

commented

I really like the library and how clear and smooth it is. Thank you for your work.
Now I have a use case where I need to use this library inside Google Cloud Functions. The init function of the library expects FirebaseApp instance. Is it possible to initialise it inside cloud functions in order to query firestore database using firebase-admin?

This is a much needed feature. I will prioritize better support for node/functions.

Is there any ETA on this? Given that the library needs to do some client side filtering, it would be very nice in some cases to do all filtering server side in cloud function before shipping the final data set to client via a REST call.

Would be great to have this in!

It works for me to calculate the geohash, but I haven't test the search

import admin from 'firebase-admin'
import { config } from 'firebase-functions'
import * as geofirex from 'geofirex'

admin.initializeApp(config.firebase)

const firestore = admin.firestore()

const settings = {
  timestampsInSnapshots: true
}

firestore.settings(settings)

const geo = geofirex.init(admin)

const events = geo.collection('events')

const id = '123' // entitiy id
const lat = 1 // lat
const lng = 1 // lng

await events.setPoint(id, 'position', lat, lng)

Any update to this?

It works for me to calculate the geohash, but I haven't test the search

import admin from 'firebase-admin'
import { config } from 'firebase-functions'
import * as geofirex from 'geofirex'

admin.initializeApp(config.firebase)

const firestore = admin.firestore()

const settings = {
  timestampsInSnapshots: true
}

firestore.settings(settings)

const geo = geofirex.init(admin)

const events = geo.collection('events')

const id = '123' // entitiy id
const lat = 1 // lat
const lng = 1 // lng

await events.setPoint(id, 'position', lat, lng)

VSCode throws error on init and cannot build

@TinusJ

  1. Which error?
  2. How do you build?
  3. Did you configure babel and webpack?
  4. Can you share your code?
  5. Which version of firebase do you use?

@TinusJ

  1. Which error?
  2. How do you build?
  3. Did you configure babel and webpack?
  4. Can you share your code?
  5. Which version of firebase do you use?

I am also getting an error, with vscode saying the types are not compatible:

Argument of type 'typeof import("functions/node_modules/firebase-admin/lib/index.d.ts")' is not assignable to parameter of type 'FirebaseApp'.
  Types of property 'firestore' are incompatible.
    Type 'typeof firestore' is not assignable to type '() => FirebaseFirestore'.

I am using Firebase CLI 6.5.0, I didn't configure babel or webpack myself, I was using the default that Firebase CLI creates when creating a new project with typescript functions.

The code is equivalent to yours, the error happens when initializing geofirex using admin as parameter. The only difference is that I need to import firebase admin as import * as admin from "firebase-admin"; as the module has no default export.

EDIT:

I found out that you can fix the error by using this not so nice cast:
geofirex.init((admin as unknown) as geofirex.firestore.FirebaseApp);
Admin is still compatible this way.

I need this working within cloud functions as well

Check the last comment edit. There is an answer.

I get this error as well, #23

Thought I would subscribe to both issues.

Hey guys, I was able to get geofirex working in functions. Please see my answer here:
#23 (comment)

Make sure you're not using the admin-sdk. There is currently no support for onNext with the admin-sdk

@rjburdish How do you firebase.initializeApp inside a cloud function ?
I'm doing

const env = JSON.parse(process.env["FIREBASE_CONFIG"] as string)

const app = firebase.initializeApp({
        projectId: env["projectId"],
        databaseURL: env["databaseURL"],
        storageBucket: env["storageBucket"],
})

const geo = geofirex.init(app);

and TS gives me this error on .init(app):
Type 'FirebaseApp' has no properties in common with type 'FirebaseApp'.

Are there any plan to support this any time soon?
What is it that prevents this library from using firebase admin in a cloud function?

We are currently using the client sdk in cloud functions.
Is there any way to have the client sdk authenticate with the admin credentials so no specific firebase user has to be used?

So, can we use geofirex in cloud functions? This is now especially important for flutter developers

I am using it in a function like this:

import * as admin from 'firebase-admin';
import * as functions from 'firebase-functions';
import {get} from 'geofirex';

const geo = require('geofirex').init(admin);
const db = admin.firestore();

exports.search = () => functions
    .firestore
    .document('/locations/{id}')
    .onWrite(async (change) => {
        const location = change.after.data();

        const distance = geo.distance(location.home, location.work) / 2;
        const geoRef = geo.query('locations');
        const query = geoRef.within(location.center, distance * 1.5, 'center');
        const hits = await get(query);

        for (const hit of hits) {
            console.log(hit);
        }
    });

I am using it in a function like this:

import * as admin from 'firebase-admin';
import * as functions from 'firebase-functions';
import {get} from 'geofirex';

const geo = require('geofirex').init(admin);
const db = admin.firestore();

exports.search = () => functions
    .firestore
    .document('/locations/{id}')
    .onWrite(async (change) => {
        const location = change.after.data();

        const distance = geo.distance(location.home, location.work) / 2;
        const geoRef = geo.query('locations');
        const query = geoRef.within(location.center, distance * 1.5, 'center');
        const hits = await get(query);

        for (const hit of hits) {
            console.log(hit);
        }
    });

This approach did not work for me: I get the error
`node_modules/geofirex/dist/client.d.ts:5:18 - error TS2694: Namespace '"/home/tiegomala/development/intertaxi_functions/functions/node_modules/firebase/index"' has no exported member 'firestore'.

5 geopoint: fb.firestore.GeoPoint;`

@Noveltysa I guess you upgraded to Firebase 8.x.x? This library seems completly incompatible with it.

commented

I'm using this guide: https://firebase.google.com/docs/firestore/solutions/geoqueries now, but it's somehow unstable in returning nearby firestore documents. Sometimes it returns empty list when it should really not be empty. I was hoping to use geofirex, but it seems it doesn't work with Cloud Functions?