Query Parser
The Query Parser is a utility for creating complex Nymph entity queries from a simple text input. Essentially, it turns a string into a Nymph query.
Installation
npm install --save @nymphjs/query-parser Usage
The query parser will turn a string into an Options and Selectors array for the Nymph Client. It has a syntax that allows for most features of a Nymph query to be expressed in a text input.
import queryParser from '@nymphjs/query-parser';
import BlogPost from './BlogPost';
import Category from './Category';
async function doQuery() {
const [options, ...selectors] = queryParser({
query:
'limit:4 sort:mdate foobar (| [archived] mdate<"2 weeks ago") category<{Cat Tech}>',
entityClass: BlogPost,
defaultFields: ['title', 'body'],
qrefMap: {
Cat: {
class: Category,
defaultFields: ['name'],
},
},
});
/*
Options will be
{
class: BlogPost,
limit: 4,
sort: 'mdate'
}
And selectors will be (equivalent to)
[
{
type: "|",
truthy: [
"archived"
],
lt: [
["mdate", null, "2 weeks ago"]
]
},
{
type: "&",
qref: [
"category",
[
{
class: Category
},
{
type: "|",
ilike: ["name", "%Tech%"]
}
]
]
},
{
type: "|",
ilike: [
["title", "%foobar%"],
["body", "%foobar%"]
]
}
]
*/
const entities = await nymph.getEntities(options, ...selectors);
} Options
You can set limit, offset, sort, and reverse like this.
limit:numberoffset:numbersort:stringorsort:-reverse:true,reverse:false,reverse:1, orreverse:0
These must appear in the top level of the query (outside of any parentheses).
Using a dash for sort means the results will not be sorted in any particular order. You can also use "cdate" for created date and "mdate" for modified date.
Clauses
These are the available clauses, and their syntax.
equal and not equal
Check for string or JSON representation equality.
name=stringor-name=stringname="string value"or-name="string value"- (Use this if you have a space in your string, or if your string could be interpreted as valid JSON. Escape double quotes with a leading backslash.)
name=JSONor-name=JSON- (Match a JSON encoded value (like
true,1,[1,2,3], or{"prop":"val"}).)
- (Match a JSON encoded value (like
guid and not guid
Check for entity GUID.
{guid}or-{guid}
tag and not tag
Check for a tag.
<name>or-<name>
truthy and not truthy
Check for truthiness.
[name]or-[name]
ref and not ref
Check for a reference to another entity.
name<{guid}>or-name<{guid}>
qref and not qref
Check for a reference to another entity using a query.
name<{refclassname inner query}>or-name<{refclassname inner query}>- (Escape curly brackets with a leading backslash.)
- (Requires a map of refclassname to their actual class and default fields.)
contain and not contain
Check if the array at the named property contains a value.
name<value>or-name<value>- (Escape angle brackets with a leading backslash. If your value could be interpreted as valid JSON, encode it as a JSON string and use the JSON syntax instead.)
name<JSON>or-name<JSON>- (Search for a JSON encoded value (like
true,1,[1,2,3], or{"prop":"val"}).)
- (Search for a JSON encoded value (like
search and not search
Check for full-text search query match. Use single quotes to find sequential terms and double quotes to find exact terms. Use "or" as the or operator. Use "-" before a term as the negation operator. Stop words and punctuation are stripped. Case insensitive.
name(query)or-name(query)name:queryorname:'sequence query'orname:"exact query"or-name:queryor-name:'sequence query'or-name:"exact query"- (The "limit", "offset", "sort", and "reverse" properties cannot be used with the colon syntax.)
match and not match
Check for POSIX regex match.
name~/pattern/or-name~/pattern/
imatch and not imatch
Check for case insensitive POSIX regex match.
name~/pattern/ior-name~/pattern/i
like and not like
Check for pattern match where _ is single char wildcard and % is any length wildcard.
name~patternor-name~patternname~"pattern"or-name~"pattern"- (Use this if you have a space in your pattern.)
ilike and not ilike
Check for case insensitive pattern match where _ is single char wildcard and % is any length wildcard.
name~"pattern"ior-name~"pattern"i
gt and not gt
Check a prop's value is greater than a given value.
name>numberor-name>numbername>relativeor-name>relative- (A single relative time value like
noworyesterday.)
- (A single relative time value like
name>"relative time value"or-name>"relative time value"- (Use this for a time value with a space like
"two days from now","last thursday","+4 weeks", or"5 minutes ago".)
- (Use this for a time value with a space like
gte and not gte
Check a prop's value is greater than or equal to a given value.
name>=numberor-name>=numbername>=relativeor-name>=relative- (A single relative time value like
noworyesterday.)
- (A single relative time value like
name>="relative time value"or-name>="relative time value"- (Use this for a time value with a space like
"two days from now","last thursday","+4 weeks", or"5 minutes ago".)
- (Use this for a time value with a space like
lt and not lt
Check a prop's value is less than a given value.
name<numberor-name<numbername<relativeor-name<relative- (A single relative time value like
noworyesterday.)
- (A single relative time value like
name<"relative time value"or-name<"relative time value"- (Use this for a time value with a space like
"two days from now","last thursday","+4 weeks", or"5 minutes ago".)
- (Use this for a time value with a space like
lte and not lte
Check a prop's value is less than or equal to a given value.
name<=numberor-name<=numbername<=relativeor-name<=relative- (A single relative time value like
noworyesterday.)
- (A single relative time value like
name<="relative time value"or-name<="relative time value"- (Use this for a time value with a space like
"two days from now","last thursday","+4 weeks", or"5 minutes ago".)
- (Use this for a time value with a space like
Selectors
You can specify nested selectors with different types using pairs of parentheses. The first character (or two) inside the parentheses can be a type: "&", "!&", "|", "!|", or "!" (the same as "!&").
Here are some examples of nested selectors.
Either enabled is truthy and abilities contains "subscriber", or abilities contains "lifelong-subscriber". (| ([enabled] abilities<"subscriber">) abilities<"lifeline-subscriber">) Published is not truthy and cdate is not greater than 6 months ago. (! [published] cdate>"6 months ago")
Default Fields
Anything contained in the query (including in selector parentheses) that
doesn't match any of the options or clause syntaxes listed above (bare query
parts) will be added (at the appropriate nesting level) to a selector with
an "|" type in an ilike clause surrounded by "%"
characters for each field passed in to the defaultFields argument.
Bare Query Handler
You can also supply a function in the option bareHandler that
will handle bare query parts instead of the "Default Fields" behavior
described above. It will receive three arguments, the query parts, the
entity class, and the default fields entry for that class. It should return
a partial selector that will replace or extend the "|" selector.