Home

Awesome

jsql

Build Status

codecov

Overview

jsql is a JavaScript query language, along with a simple JavaScript objects (POJSO) reference implementation.

This is not intended to search the dom, or jQuery, or some specific database, nor is it intended to enable using SQL in a browser. jsql is intended to provide a native JSON query format for querying anything, although initially limited to JavaScript objects.

Reference Implementation

The reference implementation, searchjs, uses jsql to query a JS object, or an array of objects, and to return those results that match the query.

Syntax Definition

jsql syntax is defined as follows.

jsql always is a single JavaScript object: {} with properties that determine the parameters for the query.

There are three kinds of properties for a query:

Primitives

A primitive is an object with properties that are matched.

Multiple fields in a primitive are, by default, joined by logical AND. See under Modifiers to change this.

The name of a field in a primitive is always the name of the field to match in the record. The value can be one of:

Primitives will search against individual values and against one or more matches in an array. So the search {name:"John"} will match against any of the following objects:

Deep Searching

You are not limited to searching only at the top level. You also can do deep searching on an object of an object using dot-notation. So if you want to match on the object {city: {Montreal: true}} then you can search:

{"city.Montreal": true}

The above is a search primitive that checks that the field "city" has an object as its value, which in turn has a key "Montreal" with a value of true. You can go as deep as you want. The following is a completely valid deep-search primitive:

{"country.province.city.street":"Dorchester Blvd"}

Any modifiers that apply to simple primitives apply to deep fields as well.

Deep Searching Arrays

Deep searching is not limited to objects embedded in objects. You can have arrays of objects embedded in objects. You even can have arrays of objects embedded in arrays of objects embedded in... (you get the idea!).

Thus, the search primitive {"name.cars.hp":{from:200}} will match any of the following:

You can also deep-search for an object using nested primitives. Search primitive {cars: {brand: 'porsche',hp:450}} will match:

But will not match following:

Number of levels of deep-searching is not limited. The valid primitive may look like {cars: {brand: 'porsche',hp:450, color: {exterior: 'red', interior: {seats: 'beige', dashboard: 'black'}}}}

Property Search

If you are not sure in which level a specific property can be found you can use the propertySearch modifier. It checks on each level if a property exists and then checks if it matches.

The following search would find the item below:

{"name":"tom", _propertySearch:true}

Item:

{"level1":{"level2":{"level3":{name: "tom"}}}}

This works also in combination with Deep Search.

It is possible to omit any level in between. So all the following queries will match the above item.

{"name":"tom", "_propertySearch": true}
{"level1.name":"tom", "_propertySearch": true}
{"level1.level2.name":"tom", "_propertySearch": true}
{"level3.name":"tom", "_propertySearch": true}
{"level1.level3.name":"tom", "_propertySearch": true},
{"name":"tom", "_propertySearch": true, "_propertySearchDepth": 4}
{"level1.name":"tom", "_propertySearch": true, "_propertySearchDepth": 4}

It is also possible and often recommended to limit the search depth. The following query would match the above item:

{"name": "tom", "_propertySearch": true, "_propertySearchDepth": 4}

However this one would not because it stops the search one level before:

{"name":"tom", "_propertySearch": true, "_propertySearchDepth": 3}

searchjs normally matches exactly the objects and depths you provide. With Property Searching, it is possible, especially on a large data set, to spend a lot of time (and CPU and memory) searching. We strongly recommend limiting the propertySearchDepth unless you know the data set with which you are working is limited.

Array Primitive

If the value of a field in a primitive is an array, then it will accept a match of any one of the array values.

{name:["John","Jack"]} // accepts any record where the name field matches 'John' or 'Jack'
{_join:"OR",terms:[{name:"John"},{name:"Jack"}]} // equivalent to the previous

Additionally, if the target record also has an array, it will accept a match if any one of the values in the array of the record matches any one of the values in the array of the search term.

{name:["John","Jack"]}

will match any of these:

{name:"John",phone:"+12125551212"}
{name:"Jack",location:"Canada"}
{name:["John","Jim"],company:"Hot Startup"}

Range

If the value of a field in a primitive is an object with "from", "to", "gt", "lt", "gte" or "lte" fields, then it will treat it as a range.

{age:{from:30}}  // accepts any age >=30
{age:{gte:30}}  // accepts any age >=30
{age:{gt:30}}  // accepts any age >30
{age:{to:80}}    // accepts any age <=80
{age:{lte:80}}    // accepts any age <=80
{age:{lt:80}}    // accepts any age <80
{age:{from:30,to:80}}  // accepts any age from 30 to 80 (inclusive)
{_not:true,age:{from:30}} // accepts any age <30
{age:{nothing:"foo"}}  // ignored

Accept values in to and from fields in a range are numbers and strings. The type of the target record's data must match the type of the value of from and to. If not, it is treated as unmatched. You cannot match {age:{from:30}} to a record {age:"veryold"}!

Note that "gte" and "from", and "lte" and "to", are interchangeable, while "gt" and "lt" are is equivalent to ">" and "<" respectively!

Modifiers

Modifiers change the search term of a primitive.

Negation

Negation just sets the opposite. Instead of checking if the "name" field equals "John", you can check if it does not equal "John":

{name:"John",_not:true}   // match all records in which name !== "John"

Just add the field _not to the primitive and set it to true. If the _not field does not exist, or is set to false or null, it will be ignored.

Join

Join determines how multiple fields are put together. Instead of checking if "name" equals "John" AND "age" equals 30, you can check if "name" equals "John" OR "age" equals 30:

{name:"John",age:30,_join:"OR"}   // match all records in which name === "John" || age === 30

Just add the field _join to the primitive and set it to "OR". If the _join field does not exist, or is set to "AND", it will join the field in "AND".

Text Searching

In general, if you search a field that is a string, and the search primitive is a string, then it will be an exact match, ignoring case.

{name:"davi"} will match a record whose content is {name:"davi"}, as well as one whose "name" field matches "Davi" and "DAVI", but not one whose content is {name:"david"} or even {name: "davi abc"}.

If you want a text search that can do partial matches, text searching is here to help!

There are two variants on text search that can expand your ability to search text fields:

  1. substring: if you set the flag {_text: true} as part of your search, then it searches for your match as part of the field. In other words, if your search is {name:"davi", _text:true} then it will check if the field matches /davi/i.
  2. word: if you set the flag {_word: true} as part of your search, then it search for your match as a complete word in the field. In other words, if your search is {name:"davi",_word:true} then it will check if the field matches /\bdavi\b/i.
  3. start: if you set the flag {_start: true} as part of your search, then it search for your match as a part of begin of the field. In other words, if your search is {name:"davi",_start:true} then it will check if the field matches /^davi/i.
  4. end: if you set the flag {_end: true} as part of your search, then it search for your match as a part end of the field. In other words, if your search is {name:"davi",_end:true} then it will check if the field matches /davi$/i.

The _text option will override the _word option if both exist.

Here are some examples of text searching:

Regular Expressions

A more generic form of word and text searching is regular expressions. You can use JavaScript regular expressions (regexp) in their /regex/ format as the value of the field. Because json does not support having regex as a value, you need to do two things to make this work:

For example:

{"_regexp": true, "name": "/ames/i"}

Deep Search Separator

As described above, you can search deep objects using dot notation. {"city.Montreal": true} will match an object {city: {Montreal: true}}.

However, what if you do not want the '.' character to be your separator? For example, what if your object key itself has a dot?

{"city.Montreal": "bagels"}

If you try to match it with a search {"city.Montreal": "bagels"}, it will look for {city: {Montreal: "bagels"}}, which is not what you have?

You can change the separator from '.' to any other character that makes you happy. Enter the search term as follows:

{"city:Montreal": "bagels", _separator: ':'} will match {city: {Montreal: "bagels"}}

Composites

If you want to combine multiple composites into a single search term, you put them in an array, name it "terms", and create a composite search term. You can search for ("name" equals "John" and age equals 30) OR ("name" equals "Jill" and "location" equals "Canada"):

{_join:"OR",terms:[{name:"John",age:30},{name:"Jill",location:"Canada"}]}

Composities can be layered inside composites, since each term in terms can itself be a composite.

Examples

  1. {name: "John", age: 30} - all records that have name === "John" (ignore-case) && age === 30
  2. {_join: "AND", name: "John", age: 30} - all records that have name === "John" (ignore-case) && age === 30 (same as above)
  3. {_join: "OR", name: "John", age: 30} - all records that have name === "John" (ignore-case) || age === 30
  4. {_not: true, name: "John"} - all records that have name !== "John" (ignore-case)
  5. {_not: true, name: "John", age: 30} - all records that have name !== "John" (ignore-case) AND age !== 30
  6. {_not: true, _join: "OR", name: "John", age: 30} - all records that have name !== "John" (ignore-case) OR age !== 30
  7. {_join: "OR", terms: [{name:"John", age:30},{age:35}]} - all records that have (name === "John" && age === 30) || (age === 35)
  8. {email: "john@foo.com"} - all records that have the email === "john@foo.com", if the record has email as a string; or if email is an array, then each element is checked; or if email is an object, then the keys are checked. All of the following will match: {email:"john@foo.com"} and {email:["john@foo.com","js@gmail.com"]} and {email:{"john@foo.com":true}}
  9. {name:["John","Jill"]} - all records that have name === "John" || name === "Jill"
  10. {email:["john@foo.com","jf@gmail.com"]} - all records that have email === "john@foo.com" || email === "jf@gmail.com" OR email in the record is an array, and at least one value in that array is "john@foo.com" or "jf@gmail.com"
  11. {_not: true, name:["John","Jill"]} - all records that have name !== "John" && name !== "Jill"
  12. {_not:true, email:["john@foo.com","jf@gmail.com"]} - all records that have (email !== "john@foo.com" && email !== "jf@gmail.com") OR email in the record is an array, and not one single value in that array is "john@foo.com" or "jf@gmail.com"
  13. {age: 30} - all records that have age === 30
  14. {age: 30, _not: true} - all records that have age !== 30
  15. {age: {from:30, to:35}} - all records that have age >= 30 && age <=35
  16. {age: {gte:30, lte:35}} - all records that have age >= 30 && age <=35
  17. {age: {gt:30, lt:35}} - all records that have age > 30 && age <35
  18. {_not: true, age: {from:30, to:35}} - all records that have age !(>= 30 && age <=35) i.e. age < 30 || age > 35
  19. {name: "John", age: {from:30, to:35}} - all records that have name === "John" && age >= 30 && age <=35
  20. {_not: true, name: "John", age: {from:30, to:35}} - all records that have name !== "John" && age !(>= 30 && age <=35)
  21. {terms:[{name: "John"}, {_not: true, age: {from:30, to:35}}]} - all records that have name === "John" && age !(>= 30 && age <=35)

searchjs

Overview

searchjs is the reference implementation of jsql. It uses jsql to check if an object matches a query, or to go through a list of objects and return those that match. For now, it uses objects in memory only; in the future, it could be extended to other data stores.

Installation & Usage

Node

In node, install using:

npm install searchjs

Browser-version is being worked on. There is nothing node-specific about searchjs.

Next, require it using:

// for commonjs
var s = require('searchjs');
// for es6
import * as searchjs from "searchjs"

Make a query. There are three types of searches: object, array of objects, and single value.

All objects are stateless. The following examples show how to use matchObject and matchArray. For more details, look at the test.js file included with searchjs.

var list = [{name:"John",age:25},{name:"Jill",age:30}];
matches = s.matchObject(list[0],{name:"Johnny"}); // returns false
matches = s.matchArray(list,{name:"John"}); // returns [{name:"John",age:25}]
matches = s.matchField(list[0].name,"John"); // returns true

matchField

matchField(value,comparator,text,word) is the underlying matcher for matching an individual object. It always returns true or false, depending on whether the item matched. It infers the type of value, and then tests it against the comparator.

The argument structure is as follows:

String matching

If value is a string, then comparator, which also should be a string, can be matched in one of 3 ways:

  1. Exact match: Do not set text or word. This is the default.
  2. Anywhere: comparator can exist anywhere in value. Set text = true.
  3. Exact word: comparator must be a word somewhere in value. Set word = true.

Note that text overrides word if both are set to true.

Examples:

matchField("This is a cool program","progr"); // false
matchField("This is a cool program","progr",false,false); // false
matchField("This is a cool program","progr",false); // false
matchField("This is a cool program","progr"); // false
matchField("This is a cool program","progr", true); // true
matchField("This is a cool program","program",false,true); // true
matchField("This is a cool program","This is a cool program"); // true
Comparator

The comparator can be one of the following, and match based on the following comparator rules

Override Defaults

Most of the functionality in searchjs has a given set of defaults. If you wish to override those defaults globally, you can do so as follows:

var s = require('searchjs');
s.setDefaults(defaults);

where defaults is an object with the property name and its new default. This can be convenient, for example, if you wish to set the same separator for all searches, and not set them independently for each one.

As of this writing, the following defaults can be overridden:

At any point, you can reset defaults by doing:

s.resetDefaults();

Browser

In the browser, you simply need to include the file lib/search.js. Download it from github (where you are probably reading this) and include it in your path. Lots of libraries do require() and inclusion, but the raw, basic way to do it is:

<script src="lib/searchjs.js"></script>

This will make a global variable SEARCHJS in your window. You can then use it as above:

var list = [{name:"John",age:25},{name:"Jill",age:30}];
matches = SEARCHJS.matchObject(list[0],{name:"Johnny"}); // returns false
matches = SEARCHJS.matchArray(list,{name:"John"}); // returns [{name:"John",age:25}]