utilities/query-helper.js
function(model, query, mongooseQuery, logger) {
    // This line has to come first
    validationHelper.validateModel(model, logger)
    const Log = logger.bind()
    // (email == 'test@user.com' && (firstName == 'test2@user.com' || firstName == 'test4@user.com')) && (age < 15 || age > 30)
    // LITERAL
    // {
    //  and: {
    //    email: {
    //      equal: 'test@user.com',
    //    },
    //    or: {
    //      firstName: {
    //        equal: ['test2@user.com', 'test4@user.com']
    //      }
    //    },
    //    age: {
    //      gt: '15',
    //      lt: '30'
    //    }
    //  }
    // {
    // and[email][equal]=test@user.com&and[or][firstName][equal]=test2@user.com&and[or][firstName][equal]=test4@user.com&and[age][gt]=15
    // ABBREVIATED
    // {
    //  email:'test@user.com',
    //  firstName: ['test2@user.com', 'test4@user.com'],
    //  age: {
    //    $or: {
    //      $gt: '15',
    //      $lt: '30'
    //    }
    //  }
    // }
    // [email]=test@user.com&[firstName]=test2@user.com&[firstName]=test4@user.com&[age][gt]=15&[age][lt]=30

    delete query[''] // EXPL: hack due to bug in hapi-swagger-docs

    delete query.$count

    mongooseQuery = this.setExclude(query, mongooseQuery, Log)

    let attributesFilter = this.createAttributesFilter(query, model, Log)
    if (attributesFilter === '') {
      attributesFilter = '_id'
    }

    let result = this.populateEmbeddedDocs(
      query,
      mongooseQuery,
      attributesFilter,
      model.routeOptions.associations,
      model,
      Log
    )
    mongooseQuery = result.mongooseQuery
    attributesFilter = result.attributesFilter

    mongooseQuery = this.setSort(query, mongooseQuery, Log)

    mongooseQuery.select(attributesFilter)

    if (typeof query.$where === 'string') {
      query.$where = JSON.parse(query.$where)
    }

    if (query.$where) {
      mongooseQuery.where(query.$where)
      delete query.$where
    }

    // EXPL: Support single (string) inputs or multiple "or'd" inputs (arrays) for field queries
    for (let fieldQueryKey in query) {
      let fieldQuery = query[fieldQueryKey]
      if (!Array.isArray(fieldQuery)) {
        fieldQuery = tryParseJSON(query[fieldQueryKey])
      }
      if (
        fieldQuery &&
        Array.isArray(fieldQuery) &&
        fieldQueryKey !== '$searchFields'
      ) {
        query[fieldQueryKey] = { $in: fieldQuery } // EXPL: "or" the inputs
      }
    }

    // EXPL: handle full text search
    if (query.$text) {
      query.$text = { $search: query.$text }
    }

    // EXPL: handle regex search
    this.setTermSearch(query, model, Log)

    let whereQuery = _.extend({}, query)

    // EXPL: delete pagination parameters
    delete whereQuery.$limit
    delete whereQuery.$skip
    delete whereQuery.$page

    mongooseQuery.where(whereQuery)
    return mongooseQuery
  }
function(model, query, mongooseQuery, logger) {
    // This line has to come first
    validationHelper.validateModel(model, logger)
    const Log = logger.bind()
    // (email == 'test@user.com' && (firstName == 'test2@user.com' || firstName == 'test4@user.com')) && (age < 15 || age > 30)
    // LITERAL
    // {
    //  and: {
    //    email: {
    //      equal: 'test@user.com',
    //    },
    //    or: {
    //      firstName: {
    //        equal: ['test2@user.com', 'test4@user.com']
    //      }
    //    },
    //    age: {
    //      gt: '15',
    //      lt: '30'
    //    }
    //  }
    // {
    // and[email][equal]=test@user.com&and[or][firstName][equal]=test2@user.com&and[or][firstName][equal]=test4@user.com&and[age][gt]=15
    // ABBREVIATED
    // {
    //  email:'test@user.com',
    //  firstName: ['test2@user.com', 'test4@user.com'],
    //  age: {
    //    $or: {
    //      $gt: '15',
    //      $lt: '30'
    //    }
    //  }
    // }
    // [email]=test@user.com&[firstName]=test2@user.com&[firstName]=test4@user.com&[age][gt]=15&[age][lt]=30

    delete query[''] // EXPL: hack due to bug in hapi-swagger-docs

    delete query.$count

    mongooseQuery = this.setExclude(query, mongooseQuery, Log)

    let attributesFilter = this.createAttributesFilter(query, model, Log)
    if (attributesFilter === '') {
      attributesFilter = '_id'
    }

    let result = this.populateEmbeddedDocs(
      query,
      mongooseQuery,
      attributesFilter,
      model.routeOptions.associations,
      model,
      Log
    )
    mongooseQuery = result.mongooseQuery
    attributesFilter = result.attributesFilter

    mongooseQuery = this.setSort(query, mongooseQuery, Log)

    mongooseQuery.select(attributesFilter)

    if (typeof query.$where === 'string') {
      query.$where = JSON.parse(query.$where)
    }

    if (query.$where) {
      mongooseQuery.where(query.$where)
      delete query.$where
    }

    // EXPL: Support single (string) inputs or multiple "or'd" inputs (arrays) for field queries
    for (let fieldQueryKey in query) {
      let fieldQuery = query[fieldQueryKey]
      if (!Array.isArray(fieldQuery)) {
        fieldQuery = tryParseJSON(query[fieldQueryKey])
      }
      if (
        fieldQuery &&
        Array.isArray(fieldQuery) &&
        fieldQueryKey !== '$searchFields'
      ) {
        query[fieldQueryKey] = { $in: fieldQuery } // EXPL: "or" the inputs
      }
    }

    // EXPL: handle full text search
    if (query.$text) {
      query.$text = { $search: query.$text }
    }

    // EXPL: handle regex search
    this.setTermSearch(query, model, Log)

    let whereQuery = _.extend({}, query)

    // EXPL: delete pagination parameters
    delete whereQuery.$limit
    delete whereQuery.$skip
    delete whereQuery.$page

    mongooseQuery.where(whereQuery)
    return mongooseQuery
  }
function ( model , query , mongooseQuery , logger ) { // This line has to come first validationHelper . validateModel ( model , logger ) const Log = logger . bind ( ) // (email == 'test@user.com' && (firstName == 'test2@user.com' || firstName == 'test4@user.com')) && (age < 15 || age > 30) // LITERAL // { //  and: { //    email: { //      equal: 'test@user.com', //    }, //    or: { //      firstName: { //        equal: ['test2@user.com', 'test4@user.com'] //      } //    }, //    age: { //      gt: '15', //      lt: '30' //    } //  } // { // and[email][equal]=test@user.com&and[or][firstName][equal]=test2@user.com&and[or][firstName][equal]=test4@user.com&and[age][gt]=15 // ABBREVIATED // { //  email:'test@user.com', //  firstName: ['test2@user.com', 'test4@user.com'], //  age: { //    $or: { //      $gt: '15', //      $lt: '30' //    } //  } // } // [email]=test@user.com&[firstName]=test2@user.com&[firstName]=test4@user.com&[age][gt]=15&[age][lt]=30 delete query [ '' ] // EXPL: hack due to bug in hapi-swagger-docs delete query . $count mongooseQuery = this . setExclude ( query , mongooseQuery , Log ) let attributesFilter = this . createAttributesFilter ( query , model , Log ) if ( attributesFilter === '' ) { attributesFilter = '_id' } let result = this . populateEmbeddedDocs ( query , mongooseQuery , attributesFilter , model . routeOptions . associations , model , Log ) mongooseQuery = result . mongooseQuery attributesFilter = result . attributesFilter mongooseQuery = this . setSort ( query , mongooseQuery , Log ) mongooseQuery . select ( attributesFilter ) if ( typeof query . $where === 'string' ) { query . $where = JSON . parse ( query . $where ) } if ( query . $where ) { mongooseQuery . where ( query . $where ) delete query . $where } // EXPL: Support single (string) inputs or multiple "or'd" inputs (arrays) for field queries for ( let fieldQueryKey in query ) { let fieldQuery = query [ fieldQueryKey ] if ( ! Array . isArray ( fieldQuery ) ) { fieldQuery = tryParseJSON ( query [ fieldQueryKey ] ) } if ( fieldQuery && Array . isArray ( fieldQuery ) && fieldQueryKey !== '$searchFields' ) { query [ fieldQueryKey ] = { $in : fieldQuery } // EXPL: "or" the inputs } } // EXPL: handle full text search if ( query . $text ) { query . $text = { $search : query . $text } } // EXPL: handle regex search this . setTermSearch ( query , model , Log ) let whereQuery = _ . extend ( { } , query ) // EXPL: delete pagination parameters delete whereQuery . $limit delete whereQuery . $skip delete whereQuery . $page mongooseQuery . where ( whereQuery ) return mongooseQuery }
Create a mongoose query based off of the request query
@param model: A mongoose model object.
@param query: The incoming request query.
@param mongooseQuery: A mongoose query.
@param logger: A logging object.
@returns {*}: A modified mongoose query.
Create a mongoose query based off of the request query