linq2js / linq2fire

Supports special operators: IN, NOT IN, OR, !=, startsWith (^=), array-contains (has) and many more

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

question/feature-request: why linq2fire can't operate query with few `has` operators?

artolshansky opened this issue · comments

If I'm understood rightly, linq2fire generate new query request for each operator (or how it works with or and in operators?). But when I'm generating a query with multiple or and has operator, I got an error:

ERROR Error: Invalid query. Queries only support a single array-contains filter.

So, is it possible to create a query otherwise with multiple or and has operators?


Code example (for example, I need to get posts according to specific user's info):

/*   USER INFO EXAMPLE  */
    const userInfo = {
      gender: 'male',
      ageGroup: '18-25',
      country: 'U.S.',
      ethnicity: 'white'
    };
/*   POST EXAMPLE   */
    const post = {
      /* ... some fields */
      postAudience: {
        genders: ['male', 'female'],
        ageGroups: ['18-25', '25-35'],
        ethnicities: null,
        countries: ['U.S.', 'UK'],
      }
    };
/*   QUERY   */

    let query = this.queryCreator.from('posts');
    query = query
      .orderBy({createdAt: 'desc'})
      .where({active: true, public: true, premium: true});

    for (const prop of Object.keys(userInfo)) {
      switch (prop) {
        case 'gender':
          query = query.where({
            or: {
              'postAudience.genders has': userInfo[prop],
              'postAudience.genders ==': null
            }
          });
          break;
        case 'ageGroup':
          query = query.where({
            or: {
              'postAudience.ageGroups has': userInfo[prop],
              'postAudience.ageGroups ==': null
            }
          });
          break;
        case 'ethnicity':
          query = query.where({
            or: {
              'postAudience.ethnicities has': userInfo[prop],
              'postAudience.ethnicities ==': null
            }
          });
          break;
        case 'country':
          query = query.where({
            or: {
              'postAudience.countries has': userInfo[prop],
              'postAudience.countries ==': null
            }
          });
          break;
        default:
          query = query.where({
            or: {
              [`postAudience.${prop} has`]: userInfo[prop],
              [`postAudience.${prop} ==`]: null
            }
          });
      }
    }

Hi @olshansky , thank you for your feedback
That is not linq2fire issue, it causes by firestore syntax checker, please refer firebase/firebase-js-sdk#1169