algolia / algolia-firebase-nodejs

An example showing how to push data from Firebase to Algolia
24 stars 10 forks source link

How to deploy this to firebase functions? #7

Open uzaysan opened 5 years ago

uzaysan commented 5 years ago

Algolia function strucrure is different than offical firebase function examples? How can i deploy this function?

Haroenv commented 5 years ago

Does this guide help you? https://firebase.google.com/docs/firestore/solutions/search

uzaysan commented 5 years ago

@Haroenv I will check

uzaysan commented 5 years ago

No It shows how to search. But ı already did inapp searching and Writed my fuction to copy my firebase data to algolia.But ı wanna upload my funxtion to firebase functions. asd

Haroenv commented 5 years ago

This example isn't made for firebase functions in fact, but for regular hosting. cc @bramses who made a functions version I think

uzaysan commented 5 years ago

This example isn't made for firebase functions in fact, but for regular hosting. cc @bramses who made a functions version I think

I couldnt find anything in his profile. Can you share a link if you can?

Haroenv commented 5 years ago

Just saw that it's Firestore, not firebase data: https://gist.github.com/Haroenv/9e3c5904dc5fbe2934991dd7b3c72f41

bramses commented 5 years ago

Hi @uzaysan, when uploading functions you can use firebase deploy --only functions to add your functions to Firebase. However, in order for Firebase to interact with Algolia directly, you must be on the Blaze plan for Firebase. The reason for this is that Google doesn't allow 3rd party outbound calls on the free tier

uzaysan commented 5 years ago

Hi @bramses I know i have to switch blaze plan. What i'm asking is how can i upload this example function to firebase functions.

Look this is official firebase function example: asdff

And this is algolia example: asd

As you can see they have different arcitecture. How can i upload example algolia function to firebase?

Edit: Also algolia example wants my admin json file. But in firebase functions we dont need that. We can easily read data from database with firebase admin.

One more question: If this example is not usable in firebase function will you make another example for firebase functions?

bramses commented 5 years ago

Ah, yeah, I put all of my functions into the same file (index.js). My thought is that Firebase looks for an index.js or index.ts file to pull from. I checked the firebase config file but I didn't find any explicit mention of this.

As for the admin json file, can you point me to where this is the case?

Finally, yes, we'll add it to our future work to update this example to be clearer :).

Does this info help?

uzaysan commented 5 years ago

Thanks for reply.

This is how we initialize admin in algolia example.

firebaseAdmin.initializeApp({
  credential: firebaseAdmin.credential.cert(serviceAccount),
  databaseURL: "https://***************.firebaseio.com"
});

And this is how i initialize firebase admin: admin.initializeApp();

But i need to mention that. When i install firebase functions with console they asked me login and took my gmail and password.after that i pointed which project i'm gonna use/upload etc..

Also I writed a code for firebase unction and i want to hear your opinion since i dont know javascript very well.

Does this code work?

exports.algoliauser = functions.database.ref('/algoliaobjects/{objectId}').onWrite((data, context)=>{
  const objectid = context.params.objectId;
  var db=admin.database();
  var algolia = algoliasearch("App Id", "Api key");
  var index = algolia.initIndex('Algolia index name');
  db.ref("algoliaobjects").child(objectid).once('value',function(snapshot){
    if(snapshot.exists()){
      //If new object writed to databse or updated an existing object
      var firebaseObject = snapshot.val();
      firebaseObject.objectID = dataSnapshot.key;
      index.saveObject(firebaseObject, function(err, content) {
        if (err) {
          throw err;
        }
        console.log('Firebase<>Algolia object saved', firebaseObject.objectID);
      });

    }
    else{
      //If object deleted
      index.deleteObject(objectid, function(err, content) {
        if (err) {
          throw err;
        }
        console.log('Firebase<>Algolia object deleted', objectID);
      });
    }
  });
});

Edit: I deployed my function. I think it works but im using free plan so it give me error Billing account not configured. External network is not accessible and quotas are severely limited. Configure billing account to remove these restrictions

Also right after this i got these error. AlgoliaSearchNetworkError

I think this error happens because ım using free plan.

I tried to upgrade to balze plan but google didnt accept meastro card. If ı can find another card i will try. If you are able to try please try.

bramses commented 5 years ago

Hi @uzaysan, that code looks fine to me. Let me know what happens after you're able to update your plan with Firebase. It may just be that Algolia isn't receiving any data from firebase. I have a question though; I'm not sure what you mean to do with this else block

else{
      //If object deleted
      index.deleteObject(objectid, function(err, content) {
        if (err) {
          throw err;
        }
        console.log('Firebase<>Algolia object deleted', objectID);
      });

if you delete the object from the index if it exists, won't your index eventually end up empty?