0

I'm currently working on a project where I'm leveraging Algolia's full-text search capabilities alongside my Firestore users collection. The goal is to have Algolia serve as a near-perfect representation of my users collection minus any personally identifiable information. I've got a working solution, but I'm not sure my implementation is as clean as it could be.

The problem stems from functions of mine that update the user when various actions are taken on my application. Here is a contrived example to illustrate my point:

When a user registers they supply their full name. Their data is then sent to the users collection where a Firestore.onCreate trigger fires, splits that fullName into firstName and lastName, and updates the document. I then have a Firestore.onUpdate trigger that takes out all of the PII and sends that version to Algolia. The problem (I think?) is that I've now sent two operations to Algolia for one user action even though we knew the first action (fullName) would have a second one (firstName & lastName) coming through moments later. (Let's assume this is happening at scale and cost is a concern)

Is there a recommended way to deal with these types of scenarios: an update has side effects that also need to send updates? The first thing that comes to mind is to have logic in the onUpdate trigger that decides whether the change is worth sending to Algolia or not, but that quickly turns a simple operation into a garbage one. I imagine a better approach would be to convert the Firestore.onChange trigger to a basic HTTP Trigger that I fire only when I actually care to change the data, but don't know if that's a bandaid around my lack of understanding. Any guidance on this is greatly appreciated!

1 Answer 1

1

The first thing that comes to mind is to have logic in the onUpdate trigger that decides whether the change is worth sending to Algolia or not, but that quickly turns a simple operation into a garbage one.

This is actually kind of common, and even required for correctly dealing with onUpdate triggers that modify the same document they trigger on.

I imagine a better approach would be to convert the Firestore.onChange trigger to a basic HTTP Trigger that I fire only when I actually care to change the data

This is also common.

What you really have here is a matter of preference. Neither approach is incorrect. Pick the one that best meets your needs and preference.

I will point out that your client app can write documents while offline, and will eventually sync with the server, and cause the trigger to fire. HTTP triggers obviously will not work offline, so you will have to write code to retry from the client, which could be challenging to get correct.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.