algolia / gatsby-plugin-algolia

A plugin to push to Algolia based on graphQl queries
https://yarn.pm/gatsby-plugin-algolia
Apache License 2.0
177 stars 45 forks source link

feat: deep compare matchFields #124

Closed thecodingwizard closed 3 years ago

thecodingwizard commented 3 years ago

Previously, when using enablePartialUpdates with matchFields, fields were compared using the === operator. This meant that objects and arrays would be flagged to be updated even if the contents of the objects and arrays were unchanged. This PR uses deep-equal to detect changes, so objects and arrays that have the same value would not trigger an unnecessary update.

Haroenv commented 3 years ago

I see what this fix achieves, but myself I only had strings / numbers in mind for the value of matchFields. What's the reason you went with an object?

thecodingwizard commented 3 years ago

In my application I had records of "programming problems" that could be filtered by tags (ie. Dynamic Programming, Shortest Path, Graphs, etc). Each record would contain an array of tags. (WIP example: https://usaco.guide/problems/)

As for objects, my use case was a bit more specific -- for each problem, I would store an array of "pages" on the website that each problem appeared in. Each page would be stored as an object with two properties: the slug and the title of the page.

(This is my first time using Algolia, so perhaps there's a better way to design this?)

Haroenv commented 3 years ago

matchFields is more meant to check whether a record has changed, so something like a date modified, or a hash of the content on that record. If you only add the slug & tags, it will not update when the content updates

thecodingwizard commented 3 years ago

Apologies if I'm misunderstanding, but if I want the record to update when the tags have changed, how should I achieve this? (I don't have a last modified timestamp, and it seems overly complicated to hash the object and add a hash field to the record solely to check for changes...)

To clarify, I had previously included tags in the matchFields array, but that was being marked as a change every time, even if the contents of the tags array didn't change.

Haroenv commented 3 years ago

if the record indeed basically only contains those records, comparing the whole value is a reasonable option. Let's go with this