npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

objection-match

v1.0.0

Published

A lightweight search DSL for Objection.js

Downloads

2

Readme

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 Node | Corresponding Function | | ------------ | ---------------------- | | match_all | AND | | match_any | OR |

| Comparison Node | Corresponding Function | | --------------- | ---------------------- | | eq | = | | neq | != | | geq | >= | | leq | <= | | lt | < | | gt | > | | like | LIKE | | in | IN |

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:

| Property | Description | | ---------------------------------- | -------------------------------------------------------------------------------------------------------------------- | | predicate string (required) | The search string, as described in Grammar | | limit number | A 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:

| Property | Description | | ---------------------------------- | -------------------------- | | enableCache boolean | Turns on caching | | cacheMaxSize number (default 10) | Sets the size of the cache |

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