Filters
Hormo Studio supports a specific filter syntax: it’s a lot like SQL, but designed specifically to serialize safely without injection and to be native to JavaScript. The following table describes Hormo Stuiod's filter types:
Fields filter
A fields filter specifies properties (fields) to include or exclude from the results.
You can also use stringified JSON format in a REST query
Include filter
An include filter enables you to include results from related models in a query, for example models that have belongsTo or hasMany relations, to optimize the number of requests. See Creating model relations for more information. The value of the include filter can be a string, an array, or an object.
filter[include][
relatedModel
]=
propertyName
These examples assume a customer model with a hasMany relationship to a reviews model. Return all customers including their reviews:
Return all customers including their reviews which also includes the author:
Return all customers whose age is 21, including their reviews which also includes the author:
Return first two customers including their reviews which also includes the author
Return all customers including their reviews and orders
Limit filter
A limit filter limits the number of records returned to the specified number (or less).
Return only the first five query results:
Order filter
An order filter specifies how to sort the results: ascending (ASC) or descending (DESC) based on the specified property. Order by one property:
Order by two or more properties:
Return the three loudest three weapons, sorted by the audibleRange
property:
Skip filter
A skip filter omits the specified number of returned records. This is useful, for example, to paginate responses. Use offset
as an alias for skip
.
This REST request skips the first 50 records returned:
Pagination Example The following REST requests illustrate how to paginate a query result. Each request request returns ten records: the first returns the first ten, the second returns the 11th through the 20th, and so on…
Where filter
A where filter specifies a set of logical conditions to match, similar to a WHERE clause in a SQL query.
In the first form below, the condition is equivalence, that is, it tests whether property equals value. The second form below is for all other conditions.
For example, if there is a cars model with an odo
property, the following query finds instances where the odo
is greater than 5000:
For example, here is a query to find cars with odo
is less than 30,000:
Encode the large filter object as “stringified JSON.”
Encode filter object as JSON
Operators
This table describes the operators available in “where” filters. See Examples below.
AND and OR operators Use the AND and OR operators to create compound logical filters based on simple where filter conditions, using the following syntax.
Where condition1 and condition2 are a filter conditions.
Regular expressions
You can use regular expressions in a where filter, with the following syntax. You can use a regular expression in a where clause for updates and deletes, as well as queries. Essentially, regexp
is just like an operator in which you provide a regular expression value as the comparison value.
Tip: A regular expression value can also include one or more flags. For example, append /i
to the regular expression to perform a case-insensitive match.
Where <expression>
can be a:
String defining a regular expression (for example,
'^foo'
).Regular expression literal (for example,
/^foo/
).Regular expression object (for example,
new RegExp(/John/)
).
Or, in a simpler format:
Where <expression>
can be a:
Regular expression literal (for example,
/^foo/
).Regular expression object (for example,
new RegExp(/John/)
).
For more information on JavaScript regular expressions, see Regular Expressions (Mozilla Developer Network).
Where:
property is the name of a property (field) in the model being queried.
expression is the JavaScript regular expression string. See Regular Expressions (Mozilla Developer Network).
The following REST query returns all cars for which the model starts with a capital “T”::
The following REST query returns all models that start with either an uppercase “T” or lowercase “t”:
Note that since the regular expression includes a flag, it is preceded by a slash (/
).
Examples
Equivalence Weapons with name M1911:
Cars where carClass is “fullsize”:
gt and lt
For example, the following query returns all instances of the employee model using a where filter that specifies a date property after (greater than) the specified date:
The top three weapons with a range over 900 meters:
Weapons with audibleRange less than 10:
and / or The following code is an example of using the “and” operator to find posts where the title is “My Post” and content is “Hello”.
between Example of between operator:
near The where.<field>.near
filter is different from other where filters: most where filters limit the number of records returned, whereas near
orders them, making it more like a SQL order by
clause. By combining it with [limit](https://loopback.io/doc/en/lb2/Limit-filter.html)
, you can create a query to get, for example, the three records nearest to a given location. For example:
Inq The inq operator checks whether the value of the specified property matches any of the values provided in an array. The general syntax is:
Where:
property is the name of a property (field) in the model being queried.
val1, val2, and so on, are literal values in an array.
Example of inq operator:
Or
Last updated