Search code examples
javascriptdatabaseember.jsember-data

Write an Or filter in Ember


I want to write an Or filter in Ember. The filter that I want to implement is as follows:

(end_date <= filter_end_date && end_date >= filter_start_date) || (start_date >= filter_start_date && start_date <= filter_end_date) || (start_date <= filter_start_date && end_date >= filter_end_date)

I am collecting all the filters together and then passing it while sending the query:

filterOptions: [
    {
      name : 'state',
      op   : 'eq',
      val  : 'published'
    }
  ]

Query is as follows:

return this.get('store').query('event', {
      sort   : 'starts-at',
      filter : filterOptions
    });

I went through the docs but didn't find anything. What is best way to model this filter?


Solution

  • Yes, @jelhan is correct. We are using Flask-REST-JSONAPI as the filtering system. The filtering system is completely related to the data layer used by the ResourceList manager. So, I used https://flask-rest-jsonapi.readthedocs.io/en/latest/filtering.html as a reference and wrote the filter as follows :

    dateFilterWithEndDate: computed('start_date', 'end_date', function() {
        return EmberObject.create(
          {
            or:
              [
                {
                  and: [
                    {
                      name : 'starts-at',
                      op   : 'ge',
                      val  : this.get('start_date')
                    },
                    {
                      name : 'starts-at',
                      op   : 'le',
                      val  : this.get('end_date')
                    }
                  ]
                },
                {
                  and: [
                    {
                      name : 'ends-at',
                      op   : 'ge',
                      val  : this.get('start_date')
                    },
                    {
                      name : 'ends-at',
                      op   : 'le',
                      val  : this.get('end_date')
                    }
                  ]
                },
                {
                  and: [
                    {
                      name : 'starts-at',
                      op   : 'le',
                      val  : this.get('start_date')
                    },
                    {
                      name : 'ends-at',
                      op   : 'ge',
                      val  : this.get('end_date')
                    }
                  ]
                }
              ]
          }
    
        );
      }),
    
      dateFilterBasic: computed('start_date', function() {
        return EmberObject.create(
          {
            or: [
              {
                name : 'starts-at',
                op   : 'ge',
                val  : this.get('start_date')
              },
              {
                name : 'ends-at',
                op   : 'ge',
                val  : this.get('start_date')
              }
            ]
          }
        );
      }),
    

    I add this to the filter parameter in the query method as follows:

        if (endDate) {
          filterOptions.pushObject(this.get('dateFilterWithEndDate'));
        } else {
          filterOptions.pushObject(this.get('dateFilterBasic'));
        }
    
        return this.get('store').query('event', {
          sort   : 'starts-at',
          filter : filterOptions
        });