geofirex icon indicating copy to clipboard operation
geofirex copied to clipboard

Possibility to use inside cloud functions

Open Parez opened this issue 6 years ago • 19 comments

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?

Parez avatar Aug 28 '18 20:08 Parez

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

codediodeio avatar Sep 05 '18 13:09 codediodeio

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.

acmayberry avatar Nov 01 '18 14:11 acmayberry

Would be great to have this in!

ljcremer avatar Nov 08 '18 09:11 ljcremer

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)

razbakov avatar Dec 04 '18 11:12 razbakov

Any update to this?

dmolinae avatar Jan 12 '19 03:01 dmolinae

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 avatar Jan 22 '19 12:01 TinusJ

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

razbakov avatar Jan 23 '19 14:01 razbakov

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

philipfeldmann avatar Mar 22 '19 09:03 philipfeldmann

I need this working within cloud functions as well

borntodesign avatar May 24 '19 14:05 borntodesign

Check the last comment edit. There is an answer.

razbakov avatar May 24 '19 14:05 razbakov

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

Thought I would subscribe to both issues.

borntodesign avatar May 24 '19 15:05 borntodesign

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

rjburdish avatar Jun 10 '19 00:06 rjburdish

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

EkilDeew avatar Jun 11 '19 12:06 EkilDeew

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?

david-arteaga avatar Sep 18 '19 02:09 david-arteaga

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

najibghadri avatar Jul 09 '20 00:07 najibghadri

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);
        }
    });

vandres avatar Jul 09 '20 04:07 vandres

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 avatar Nov 23 '20 12:11 Noveltysa

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

vandres avatar Nov 23 '20 12:11 vandres

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?

loolooii avatar Oct 13 '22 09:10 loolooii