typesense / firestore-typesense-search

Firebase Extension to automatically push Firestore documents to Typesense for full-text search with typo tolerance, faceting, and more
https://extensions.dev/extensions/typesense/firestore-typesense-search
Apache License 2.0
159 stars 35 forks source link

feat(utils): implement nested field extraction #91

Closed tharropoulos closed 1 month ago

tharropoulos commented 2 months ago

What is this?

This pull request enhances our Firestore to Typesense synchronization by adding support for nested field extraction. It allows users to specify nested fields using dot notation, making it easier to sync complex data structures from Firestore to Typesense.

Rationale

This change resolves issue #90, which requests dot notation support for the "Firestore Collection Fields" parameter. By implementing this feature, we enable users to access any level property on fields they want to explicitly include in the trigger.

Changes

Added Features:

  1. New dependency in functions/package.json:
    • Added lodash.get (version 4.4.2) for safe nested property access.

Code Changes:

  1. In functions/src/utils.js:
    • Imported lodash.get for nested field extraction.
    • Refactored typesenseDocumentFromSnapshot function to use lodash.get for accessing nested fields.
    • Updated field extraction logic to support dot notation for nested fields.

Test Updates:

  1. In test/utils.spec.js:
    • Added new test suite "Nested fields extraction" with four new test cases:
      • Extracting nested fields using dot notation
      • Handling missing nested fields gracefully
      • Extracting nested fields alongside top-level fields
      • Handling array indexing in dot notation

Context

This change improves the flexibility of our Firestore to Typesense synchronization extension. Users can now easily map nested Firestore fields to Typesense, allowing for more complex data structures to be indexed and searched efficiently. This feature is particularly useful for projects with deeply nested document structures, enabling fine-grained control over which specific nested fields are synchronized to Typesense.

PR Checklist