Skip to content

Use Spotr to generate standardized filters for Mongo, GraphQL queries, or lodash chains? #7

@andymerskin

Description

@andymerskin

Why not have Spotr generate query schemas to pass off to lodash or a Mongo-style query library like sift.js?

This would be in conflict with #6, where collections are passed into query() and return a result. Instead query would return a filter schema to pass off to a different library or routine.

OR

It may still be necessary to pass the collection into Spotr to utilize its field string matching and separating field strings from keyword queries. The resulting collection from field matching can be passed in the result payload to (sift.js, lodash). The use case would be to filter on field-matched results.

Example:

const search = new Spotr({
  fields: {
    accountName: {
      boost: 9
    }
  },

  keywords: {
    accountHoldingsAmount: {
      queries: ['accounts {{operator}} {{amount}}'],
      filter: (collection, {operator, amount}) => {
        let $operator
        
        switch(operator) {
          case 'above':
            $operator = '$gt'
            break;
          case 'below':
            $operator = '$lt'            
            break;
          case 'equal to':
            $operator = '$eq'
            break;
        }

        return {
          collection: collection,
          filter: {
            type: 'account',
            holdings: {
              [$operator]: parseInt(amount) 
            }
          }
        }
      }
    }
  }
})

search.query('custody accounts below $1000')

Returns:

{
  collection: collection,
  filter: {
    type: 'account',
    holdings: {
      $lt: 1000 
    }
  }
}

... which can be passed to sift.js, which then filters the collection it's passed.

import sift from 'sift'

const { filter, collection } = search.query('custody accounts below $1000')
const results = sift(filter, collection)

Or even better:

...
const custodyBelow1000 = sift(filter)
custodyBelow1000(collection)
// => array of custody accounts below $1000

Other considerations:

  • Not to add complexity, but it may be worth exploring filter adapters, giving users the option to output Mongo queries or simply use the original keyword functions to filter on collections and return the filtered collection.
  • Could be neat for generating GraphQL queries
  • Does lodash have built-in memoization (to save chains or sets of operations)?

Conflicts: #6
Dependencies: #1

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions