Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(firestore): Firestore Filter instance. Use Filter, Filter.or() & Filter.and() in Firestore queries. #7045

Merged
merged 30 commits into from
Apr 27, 2023

Conversation

russellwheatley
Copy link
Member

@russellwheatley russellwheatley commented Apr 14, 2023

Description

Latest Filter feature for RNFB. Integrated into the existing implementation. This allows chaining even with existing query capability:

firebase.firestore().collection('some-collection').where(
        Filter.or(
          Filter.and(Filter('foo', 'array-contains', 'some-string'), Filter('bar', '==', 'baz')),
          Filter.and(Filter('foo', '==', 'not-this'), Filter('foo', '==', 'bar')),
        ),
      )
      .where('existing', '==', 'where')
      .where(Filter.and(Filter('another', '==', 'filter'), Filter('chain', '==', 'and')))
      .get();

It is largely based on recursion.

  1. We have recursion on the JS side for validation & generating the correct map for native parsing here.
  2. We have recursion on the native side for generating the filters. android & iOS.

This PR also preserves the query order of chained filters that the user generates.

We still send the same list of filters over to the native side except the list can have objects that are nested Filter queries:

[
  {
    "operator": "OR",
    "queries": [
      {
        "operator": "AND",
        "queries": [
          {
            "fieldPath": {
              "_segments": [
                "foo"
              ]
            },
            "operator": "ARRAY_CONTAINS",
            "value": [
              8,
              "1681731303828"
            ]
          },
          {
            "fieldPath": {
              "_segments": [
                "bar"
              ]
            },
            "operator": "EQUAL",
            "value": [
              8,
              "baz"
            ]
          }
        ]
      },
      {
        "operator": "AND",
        "queries": [
          {
            "fieldPath": {
              "_segments": [
                "foo"
              ]
            },
            "operator": "EQUAL",
            "value": [
              8,
              "not-this"
            ]
          },
          {
            "fieldPath": {
              "_segments": [
                "bar"
              ]
            },
            "operator": "EQUAL",
            "value": [
              8,
              "baz"
            ]
          }
        ]
      }
    ]
  },
  {
    "fieldPath": {
      "_segments": [
        "existing"
      ]
    },
    "operator": "EQUAL",
    "value": [
      8,
      "where"
    ]
  },
  {
    "operator": "AND",
    "queries": [
      {
        "fieldPath": {
          "_segments": [
            "another"
          ]
        },
        "operator": "EQUAL",
        "value": [
          8,
          "filter"
        ]
      },
      {
        "fieldPath": {
          "_segments": [
            "chain"
          ]
        },
        "operator": "EQUAL",
        "value": [
          8,
          "and"
        ]
      }
    ]
  }
]

Related issues

Release Summary

Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
    • Yes
  • My change supports the following platforms;
    • Android
    • iOS
  • My change includes tests;
    • e2e tests added or updated in packages/\*\*/e2e
    • jest tests added or updated in packages/\*\*/__tests__
  • I have updated TypeScript types that are affected by my change.
  • This is a breaking change;
    • Yes
    • No

Test Plan


Think react-native-firebase is great? Please consider supporting the project with any of the below:

@vercel
Copy link

vercel bot commented Apr 14, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
react-native-firebase ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 27, 2023 9:25am
1 Ignored Deployment
Name Status Preview Comments Updated (UTC)
react-native-firebase-next ⬜️ Ignored (Inspect) Apr 27, 2023 9:25am

@russellwheatley russellwheatley changed the title feat: Firestore Filter query feat(firestore): Firestore Filter instance. Use Filter, Filter.or() & Filter.and() in Firestore queries. Apr 17, 2023
Ehesp
Ehesp previously approved these changes Apr 25, 2023
mikehardy
mikehardy previously approved these changes Apr 26, 2023
Copy link
Collaborator

@mikehardy mikehardy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was about to ask "where's the modular API?" then remembered firestore doesn't have it yet :-). Will commit the example.com changes and otherwise LGTM, this is an amazing feature

I did see some formatting moving on the java side and I think we've had false-negatives on google-java-format before so I'll check that locally and push a spacing commit as needed, but otherwise good to go

docs/firestore/usage/index.md Outdated Show resolved Hide resolved
docs/firestore/usage/index.md Outdated Show resolved Hide resolved
docs/firestore/usage/index.md Outdated Show resolved Hide resolved
docs/firestore/usage/index.md Outdated Show resolved Hide resolved
@mikehardy mikehardy dismissed stale reviews from Ehesp and themself via a8e4df4 April 26, 2023 03:56
Copy link
Collaborator

@mikehardy mikehardy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh wait - what about types? Should there be a typescript types update in https://github.com/invertase/react-native-firebase/blob/main/packages/firestore/lib/index.d.ts ? Seems like there has to be or this won't be usable in typescript projects

@mikehardy
Copy link
Collaborator

Yep as suspected there was a false-negative on java formatting, I ran lint android:format locally until it was clean then commited+pushed the result.

Only pending issue for me is typescript types question now

* The Filter.and() static function used to generate a logical AND query using multiple Filter instances.
* e.g. Filter.and(Filter('name', '==', 'Ada'), Filter('name', '==', 'Bob'))
*/
and(...queries: Filter[]): Filter;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@russellwheatley

🤔 these types don't seem like a fit vs the firebase-js-sdk types, is that intentional?

Here's the and() from firebase-js-sdk https://firebase.google.com/docs/reference/js/firestore_.md#and

Has all sorts of QueryFilterCompositeConstraint stuff, takes QueryFilterConstraint[] etc types that aren't present here

Not sure how this will work as a drop-in replacement

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated types to match naming: 20e9c8d. Let me know if this works.

Copy link
Collaborator

@mikehardy mikehardy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The types seem much more compatible now yes - I think code written for firebase-js-sdk will interop here without changes in general. The types aren't 100% but at the same time there is no SLA on compound or queries yet, so if types break a little it isn't a breaking change here and people can tell us in practice where there are problems as folks attempt to use it - so +1

Separately, as a feature (beyond nitpicking on types), this is awesome. I know lots of people will be excited

@mikehardy mikehardy added Workflow: Pending Merge Waiting on CI or similar and removed Workflow: Waiting for User Response Blocked waiting for user response. labels Apr 27, 2023
@mikehardy mikehardy merged commit f7ec3d1 into main Apr 27, 2023
@mikehardy mikehardy deleted the firestore-or-query branch April 27, 2023 16:35
@mikehardy mikehardy removed the Workflow: Pending Merge Waiting on CI or similar label Apr 27, 2023
@Aleksandar-FFWD
Copy link

Great work! 👏 When can we expect this release? 👍

exaby73 pushed a commit that referenced this pull request Aug 8, 2023
…r()` & `Filter.and()` in Firestore queries. (#7045)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants