1.0.0 • Published 4 years ago

objection-match v1.0.0

Weekly downloads
2
License
MIT
Repository
github
Last release
4 years ago

objection-match

A lightweight search plugin built on top of Objection.js.

objection-match was created for programs that wish to easily support dynamic searches. In a distributed setting, a set of constraints for a search may be defined on a client machine and sent to a server for processing. However, the task of parsing the payload, loading relations, and building a query can be cumbersome. This library provides a robust and portable query language that can be used to represent a complex search with various constraints, and from it, build an Objection query.

Grammar

The grammar can be found in src/lib/parser/parser.pegjs. It defines the structure of the query language.

Briefly, a query consists of logical and comparison nodes. The tables below describe the mapping from each node to their respective function.

Logical NodeCorresponding Function
match_allAND
match_anyOR
Comparison NodeCorresponding Function
eq=
neq!=
geq>=
leq<=
lt<
gt>
likeLIKE
inIN

Logical nodes can contain children that include both node types while comparison nodes cannot contain any children. Here's an example:

match_all: {
  eq: ["person.name", "Antonio"],
  neq: ["shirt.color", "white"],
  match_any: {
    eq: ["shirt.style", "polo"],
    eq: ["shirt.style", "dress"]
  }
}

A simple, browser-compatible package that constructs this syntax will be available as well.

Usage

The search() method is invoked directly on a model class. In order to support this, the plugin mixin needs to be added to any models that wish to use it. Example usage:

import Search from 'objection-match';

class Employee extends Search({ ...plugin options })(Model) {
  ...
}

Now, you can call the search() method like so:

const results = await Employee.search({
  predicate: `
    match_all: {
      geq: ["salary", 60000],
      geq: ["salary_start_date", "1986-06-26"],
      in: ["first_name", "Georgi, Bob"]
    }
  `,
  limit: 5,
  fields: ['salary', 'salary_start_date'],
  aliases: {
    salary: 'salaries.salary',
    salary_start_date: 'salaries.from_date',
  },
  orderBy: ['salary', 'desc'],
});

search() requires a Search object as its argument, which has the following properties:

PropertyDescription
predicate string (required)The search string, as described in Grammar
limit numberA limit on the number of results
fields string[]Fields to select (supports aliased fields)
aliases Record<string, string>An object that contains mappings from alias name to relation name. These are to be used in predicate and fields.
orderBy [string, 'desc' / 'asc']Used for ordering results.

Caching

objection-match can cache the query builder object so it doesn't have to parse and build frequently used searches. To enable caching pass options to the mixin function when initializing the plugin on a model. The options include:

PropertyDescription
enableCache booleanTurns on caching
cacheMaxSize number (default 10)Sets the size of the cache

For more information on third-party plugins, check out Objection's docs.