codediodeio / geofirex

:globe_with_meridians: :round_pushpin: Geolocation Queries with Firestore & RxJS
https://geo-test-c92e4.firebaseapp.com/
477 stars 106 forks source link

Possibility to use inside cloud functions #14

Open Parez opened 6 years ago

Parez commented 6 years ago

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?

codediodeio commented 6 years ago

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

acmayberry commented 6 years ago

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.

ljcremer commented 6 years ago

Would be great to have this in!

razbakov commented 5 years ago

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)
dmolinae commented 5 years ago

Any update to this?

TinusJ commented 5 years ago

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

razbakov commented 5 years ago

@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?

philipfeldmann commented 5 years ago

@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.

borntodesign commented 5 years ago

I need this working within cloud functions as well

razbakov commented 5 years ago

Check the last comment edit. There is an answer.

borntodesign commented 5 years ago

I get this error as well, https://github.com/codediodeio/geofirex/issues/23

Thought I would subscribe to both issues.

rjburdish commented 5 years ago

Hey guys, I was able to get geofirex working in functions. Please see my answer here: https://github.com/codediodeio/geofirex/issues/23#issuecomment-500259600

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

EkilDeew commented 5 years ago

@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'.

david-arteaga commented 5 years ago

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?

najibghadri commented 4 years ago

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

vandres commented 4 years ago

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);
        }
    });
Noveltysa commented 3 years ago

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;`

vandres commented 3 years ago

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

loolooii commented 2 years ago

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?