Querying is one of the most important aspects of a good data management library. JayData was always very strong at this with using natural JavaScript expressions as vehicles to transfer the developer intention into storage specific things.
Good old JSLQ
So to express “Completed Tasks” in the past we could simply write
1 |
Tasks.filter(function(it) { <span class="kwrd">return</span> it.Completed == <span class="kwrd">true</span> }).toArray(…) |
or with the string from and using parameters
1 |
Tasks.filter(<span class="str">" it.Completed == param "</span>,{param: <span class="kwrd">true</span>}).toArray(…) |
This is pure JavaScript syntax and has the benefits of already well documented and needs no learning. To express something more complex you do what you’d do otherwise in JavaScript: use logical operators.
1 |
Tasks.filter(<span class="str">" it.Completed == p1 && it.Priority > p2 "</span>,{p1: <span class="kwrd">true</span>, p2: 100}).toArray(…) |
Most of the time however we simply need to just “filter” for some field values without anything complex. 80% of the time we really do not use the power of the full fledged syntax, yet we always have to pay some extra characters to endorse it. So why don’t we just have…
NEW! …the filter(field, op, value) syntax
On top of the function and string based predicate syntax as of JayData 1.3 you can use a simple builder syntax to support simplex query scenarios – we call this internally the “BreezeJS syntax”. This syntax helps code generated filters to be created more easily but what is more important: let’s you express simple things even more simpler:
1 |
Tasks.filter(<span class="str">"Completed"</span>, <span class="str">"=="</span>, <span class="kwrd">true</span>).toArray(...) |
You can chain filter() statements to create and expressions
1 |
Tasks.filter(<span class="str">"Completed"</span>, <span class="str">"=="</span>, <span class="kwrd">true</span>).filter(<span class="str">"Priority"</span>,<span class="str">">"</span>,100).toArray(...) |
You can use all the operators you are familiar with (even the bitwise ones). To express method based expressions like contains or startsWith signify it with a dot (“.”)
1 |
Tasks.filter(<span class="str">"Title"</span>, <span class="str">".startsWith"</span>, <span class="str">"My blog"</span>).toArray(...) |
Search on navigation properties:
1 |
Tasks.filter(<span class="str">"Author.Name"</span>, <span class="str">".startsWith"</span>, <span class="str">"Bob"</span>).toArray(...) |
Limitations for the simplified syntax:
Well everything that comes from it’s simplicity. You can not express in row expressions (field1 > field2), and complex expression (field1 % 2 == 0). Also grouping and the use of the or logic operator requires full JSLQ syntax.
To get these working you’ll need the build from the JayData 1.3 master branch from github. – or after the release get the JayData 1.3 download package from JayData.
How we should never…
The JavaScript syntax ‘covers it all’ and gives you language elements to express complicated filters in a line or two in a way you already know. Instead some libraries require you to build complex expression trees with methods, as for example BreezeJS do:
1 2 3 4 5 6 |
var query = EntityQuery.from("Tasks"); var p1 = Predicate.create("Completed", "==", true) var p2 = Predicate.create("Severity", ">", 100); var pred = Predicate.and([p1, p2]); var filter = query.where(pred); manager.executeQuery(query1a) |