Nozbe / WatermelonDB

🍉 Reactive & asynchronous database for powerful React and React Native apps ⚡️
https://watermelondb.dev
MIT License
10.49k stars 589 forks source link

Querying table with has_many relation length not null #1805

Open KevinBrustolin opened 3 months ago

KevinBrustolin commented 3 months ago

Hello,

I'm looking for a way to query a table where the length of the children (has_many relation) is greater than 0.

I'm currently doing it like this:

  const disciplines = useLstObservable({
    table: 'param_discipline',
  });
  const [filteredDisciplines, setFilteredDiscipline] = useState([]);
  useEffect(() => {
    const getFilteredDisciplines = async () => {
      const filteredDisciplines = [];
      for (const discipline of disciplines) {
        const count = await discipline.epreuves.count;
        if (!!count) {
          filteredDisciplines(discipline);
        }
      }
      return filteredDisciplines;
    };
    getFilteredDisciplines().then(setFilteredDiscipline);
  }, [disciplines]);

Is there a better way ?

KrisLau commented 1 month ago

https://watermelondb.dev/docs/Query#conditions-on-related-tables-join-queries