Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

advanced_search

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

advanced_search

  • 0.0.2
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

AdvancedSearch

Builds database search queries with complex, nested logical expressions.

You provide an abstract syntax tree representing the search, and AdvancedSearch builds the search clause of the query (eg. the where clause, in SQL).

Good forNot Good For
searchesreports
complex logic expressionssimple "conjunction of all parameters" forms
natural language99% of business search forms

Input can be natural language,

'age less than 40 and ssn equal to 123-45-6789'

or a typical HTML form,

{
  age_lt: 40,
  ssn_eq: '123-45-6789'
}

You organize and name your parameters however you want. You convert those parameters into an AdvancedSearch::AST:

       and
    /       \
   lt        eq
  /  \      /  \
age  40   ssn   '123-45-6789'

A convenient S-expression syntax is provided to help you build your tree:

s(:and,
  s(:lt, s(:id, :age), s(:value, 40),
  s(:eq, s(:id, :ssn), s(:value, '123-45-6789')
)

AdvancedSearch converts your tree into a parameterized search clause (eg. the where clause, in SQL).

'where age < $1 and ssn = $2'
[40, '123-45-6789']

Finally, you do whatever you want with that search clause. Prepare a complete query and execute it using ActiveRecord's find_by_sql method, perhaps? Maybe execute it directly using a gem like pg or mysql2? What you do with your query, in the privacy of your own home, is your own business.

Complete Example

There is a complete example using ActiveRecord's find_by_sql method in the test suite. See:

spec/support/activerecord/models
spec/support/activerecord/searches/library_catalog.rb
spec/activerecord/searches/library_catalog_spec.rb

Adapters

adaptergemstatus
PGpgProof of concept
Mysql2mysql2Not yet implemented
ActiveRecordactiverecordIf only ARel had docs

Design Goals

  • Simple, not easy. You'll have to write substantial code to get started, but future changes will be simple.
  • Requires basic understanding of graph theory, must know what a tree is.
  • Agnostic: Support for specific databases and ORMs is provided via adapters.
  • No dependencies
  • Unit tests of your search objects do not need to touch database, so they are very fast.

Roadmap

  • full-text search
  • "raw" node?

FAQs

Package last updated on 30 Oct 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc