algolia / firestore-algolia-search

Apache License 2.0
112 stars 35 forks source link

Use with emulator? #95

Closed benomatis closed 2 years ago

benomatis commented 2 years ago

I'm trying to set this extension up with the emulator, and I did manage to download the extension, and subsequently launch it, but the problem is that the import script will obviously take the data from the online project I downloaded the extension from, and I'm looking for a way for algolia to use my local firestore db instead.

Starting with the first import (I'd create a separate Application in Algolia for it and use its details when configuring), and subsequently ensuring that when data is added to my local firestore db it triggers the onWrite in the extension, I'd really appreciate some guidance.

Things I tried:

I replaced this: functions.handler.firestore.document

...with this (as recommended by SO answers): functions.firestore.document(`projects/${process.env.PROJECT_ID}/databases/(default)/documents/${process.env.COLLECTION_PATH}/{documentID}`)

(envs do exist and do not appear to report errors during init)

I also tried adding default exporting the executeIndexOperation variable from index to no avail.

What happens on record creation is that the log.init() function does appear to kick in, but nothing inside onWrite (not even log.start()).

smomin commented 2 years ago

Are you looking to import data into your emulator? If so, see if the Firestore team can help with importing data into your emulator. There is documentation on the Firebase site to do this.

benomatis commented 2 years ago

Are you looking to import data into your emulator? If so, see if the Firestore team can help with importing data into your emulator. There is documentation on the Firebase site to do this.

No, I have already set up the emulator, I'm looking to integrate Algolia search with the emulator. The problem I have right now is that the onWrite function of this extension is not going to kick in, new data written into my emulator will not update in Algolia, because the function is expecting calls from my online project.

I tried adding the extension to my emulator manually, but then I realized that it's written in such a way, that I have no way to direct traffic via my emulator through it, it appears to 100% rely on the existence of an online project.

smomin commented 2 years ago

The extension was built using the emulator. There is a npm start script at the root of the project that will start the emulator npm run emulator:ext:start. You might need to set the project in the npm run script command and set the test-params.env. Once you have made these changes, starting up the emulator will load some test data. Any changes to the test data in the emulator will be reflected in your Algolia index.

benomatis commented 2 years ago

I get error: unknown option '--import' - do I have to run firebase --open-sesame extdev first?

I have tried this by simply downloading the code here and extracting into a folder inside my project, but that might not be the right way to do it.

Do I follow the contribution guide at all or do you recommend doing this any other way?

smomin commented 2 years ago

yes, you will need to run firebase --open-sesame extdev. The import is a valid flag but if you are having issue, reach out to the firebase team and they will help you get past this blocker.

pabloleone commented 2 years ago
{
  ...
  "emulators": {
    "functions": {
      "port": 5002
    },
    "auth": {
      "port": 9099 // I have added this as it is part of my project
    },
    "firestore": {
      "port": 9098
    },
    "ui": {
      "enabled": true,
      "port": 4000
    }
  }
  ...
}

I'm using this emulator to run my app locally so that's why I have changed the --project value. I removed the import flag as I don't need the data that's coming with the project, however, if you want your data loaded... you need to export the DB you want going here https://console.cloud.google.com/firestore/import-export?project=YOUR GOOGLE PROJECT ID. To do this you will need a bucket. Then you should navigate to your bucket where the backup has been stored and download it using gsutil command. The folder you download is the one you can use with the --import flag to get your data in the emulator from a cloud instance.

benomatis commented 2 years ago

@smomin @pabloleone Thank you so much for your help.

The thing is that I already have a project that uses the firebase emulator, and I am using the import flag in there of a saved state of my online db, so all that is working fine. So I'm guessing if I move all my firebase / firestor related config over to the extension's folder, amend the folder path for the import, it should work, my app should be able to use that along with the local algolia db...? I will need to give this a try and report back.

benomatis commented 2 years ago

@smomin @pabloleone While with your help I could fire up the emulator, I have no way to simulate my app working with it, hosting will just show a blank page. I feel I'm missing an ingredient, especially if I want to use this with an existing app locally...

smomin commented 2 years ago

Hey @benomatis, have you reviewed this Firebase article on connection App to Firebase Emulator, https://firebase.google.com/docs/emulator-suite/connect_firestore?

benomatis commented 2 years ago

Yes @smomin, like I said above, it's all up and running, and have been testing my app for over a year now with the emulator, but I'm new to Algolia with firestore, and extensions cannot be installed on the emulator as they can on a real project...

pabloleone commented 2 years ago

@benomatis I think you are right, extensions cannot be installed the same way. I personally had to move my backend development to the algolia project as I couldn´t figure out how to move the extension into my own project emulator, so I did the opposite, I moved my project functions over to the algolia extension project and make my custom functions work with it... it isn´t ideal, but it works. I did it by adding a symlink to the algolia project with my functions folder and I imported my index.js into the cli.js file of the algolia emulator project. Hope it helps!

benomatis commented 2 years ago

@pabloleone well, that sounds complicated :) but I appreciate your advice; at least I can see I'm not alone with this challenge.

I'm happy to close the issue as there is apparently no official process around this and ample info has been provided here already, or I could leave it open in case anyone still wants to add something...?

smomin commented 2 years ago

I will close this for now, but open it if you need any more support.