utilities/query-helper.js
function(query, model, logger) {
    const Log = logger.bind()
    if (query.$term) {
      query.$or = [] // TODO: allow option to choose ANDing or ORing of searchFields/queryableFields
      let queryableFields = this.getQueryableFields(model, Log)
      let stringFields = this.getStringFields(model, Log)

      // EXPL: we can only search fields that are a string type
      queryableFields = queryableFields.filter(function(field) {
        return stringFields.indexOf(field) > -1
      })

      // EXPL: search only specified fields if included
      if (query.$searchFields) {
        if (!Array.isArray(query.$searchFields)) {
          query.$searchFields = query.$searchFields.split(',')
        }

        // EXPL: we can only search fields that are a string type
        query.$searchFields = query.$searchFields.filter(function(field) {
          return stringFields.indexOf(field) > -1
        })

        query.$searchFields.forEach(function(field) {
          let obj = {}
          obj[field] = new RegExp(query.$term, 'i')
          query.$or.push(obj)
        })
      } else {
        queryableFields.forEach(function(field) {
          let obj = {}
          obj[field] = new RegExp(query.$term, 'i')
          query.$or.push(obj)
        })
      }
    }

    delete query.$searchFields
    delete query.$term
  }
function(query, model, logger) {
    const Log = logger.bind()
    if (query.$term) {
      query.$or = [] // TODO: allow option to choose ANDing or ORing of searchFields/queryableFields
      let queryableFields = this.getQueryableFields(model, Log)
      let stringFields = this.getStringFields(model, Log)

      // EXPL: we can only search fields that are a string type
      queryableFields = queryableFields.filter(function(field) {
        return stringFields.indexOf(field) > -1
      })

      // EXPL: search only specified fields if included
      if (query.$searchFields) {
        if (!Array.isArray(query.$searchFields)) {
          query.$searchFields = query.$searchFields.split(',')
        }

        // EXPL: we can only search fields that are a string type
        query.$searchFields = query.$searchFields.filter(function(field) {
          return stringFields.indexOf(field) > -1
        })

        query.$searchFields.forEach(function(field) {
          let obj = {}
          obj[field] = new RegExp(query.$term, 'i')
          query.$or.push(obj)
        })
      } else {
        queryableFields.forEach(function(field) {
          let obj = {}
          obj[field] = new RegExp(query.$term, 'i')
          query.$or.push(obj)
        })
      }
    }

    delete query.$searchFields
    delete query.$term
  }
function ( query , model , logger ) { const Log = logger . bind ( ) if ( query . $term ) { query . $or = [ ] // TODO: allow option to choose ANDing or ORing of searchFields/queryableFields let queryableFields = this . getQueryableFields ( model , Log ) let stringFields = this . getStringFields ( model , Log ) // EXPL: we can only search fields that are a string type queryableFields = queryableFields . filter ( function ( field ) { return stringFields . indexOf ( field ) > - 1 } ) // EXPL: search only specified fields if included if ( query . $searchFields ) { if ( ! Array . isArray ( query . $searchFields ) ) { query . $searchFields = query . $searchFields . split ( ',' ) } // EXPL: we can only search fields that are a string type query . $searchFields = query . $searchFields . filter ( function ( field ) { return stringFields . indexOf ( field ) > - 1 } ) query . $searchFields . forEach ( function ( field ) { let obj = { } obj [ field ] = new RegExp ( query . $term , 'i' ) query . $or . push ( obj ) } ) } else { queryableFields . forEach ( function ( field ) { let obj = { } obj [ field ] = new RegExp ( query . $term , 'i' ) query . $or . push ( obj ) } ) } } delete query . $searchFields delete query . $term }
Perform a regex search on the models immediate fields
@param query:The incoming request query.
@param model: A mongoose model object
@param logger: A logging object
Perform a regex search on the models immediate fields