Search query is a javascript library that allows your users to use a query like language (highly based off of MySQL) to search. This is designed for web applications like project management systems where users might need a more advance option for querying data.
Including the search-query.js
file will exposed an object called searchQuery
which provide 2 methods.
The parse()
method takes in a string which is the query and returns a json representation of that query. So if you were to pass:
firstName = 'john' and (lastName != 'doe' or statusId in(1, 2, 3)) order by lastName desc
You would get back the following JSON:
{
search: [{
field: 'firstName',
comparison: '=',
value: 'john'
}, {
connector: 'and',
items: [{
field: 'lastName',
comparison: '!=',
value: 'doe'
}, {
connector: 'or',
field: 'statusId',
comparison: 'in',
value: [
1,
2,
3
]
}]
}],
order: [{
field: 'lastName',
way: 'desc'
}]
}
You can view the unit test to see a lot more examples.
Also note that the keywords (and, or, in, not in, order by, etc...) are not case sensitive.
You can use the following value types (the JSON also returns them in the same value type):
- string:
firstName = 'john'
- number:
statusId = 1
- null:
updateTimestamp is null
- boolean:
isAdmin = false or isSuperUser = true
- identifier:
status = Active
(stored as string)
You can use the following comparison operators
- equals:
firstName = 'john'
- not equals:
firstName != 'john'
- greater than:
loginCount > 100
- greater than or equal to:
loginCount >= 100
- less than:
loginCount < 100
- less than or equal to:
loginCount <= 100
- in:
statusId in(1, 2, 3)
- not in:
statusId not in(1, 2, 3)
- is null:
updatedTimestamp is null
- is not null:
updatedTimestamp is not null
- like:
username like '%john%'
- not like:
username not like '%john%'
- between:
createdTimestamp between '2013-01-01 00:00:00' and '2014-01-01 00:00:00'
- not between:
createdTimestamp not between '2013-01-01 00:00:00' and '2014-01-01 00:00:00'
The compile()
method takes in the JSON that is generated by the parse()
method and return the query.
MIT (see https://raw.githubusercontent.com/ryanzec/search-query/master/LICENSE).
This would not be possible with the Jison (http://zaach.github.io/jison/) library which is used to create the parser.