Marvin
Marvin

Reputation: 13

JSONStore complex queries

How would you implement the following query against JSONStore

In SQL format it is

select * from table where (A or B) and (C or D)

I'm assuming we would use an advancedFind operation with an array of QueryParts, however in the samples I can see how you can use QueryParts to form and AND but not how to form an OR query.

Any guidance appreciated.

Upvotes: 0

Views: 622

Answers (1)

cnandreu
cnandreu

Reputation: 5111

Taking your example SQL and giving it values it would look like this:

select * from people where (name = 'carlos' or name = 'mike') AND (rank = 'king' or rank = 'pawn')

Which is the same as:

select * from people where (name = 'mike' AND rank = 'king') or (name = 'carlos' AND rank = 'pawn') or (name = 'carlos' AND rank = 'king') or (name = 'mike' and rank = 'pawn')

That can be expressed by JSONStore pseudocode like this:

var queryPart1 = WL.JSONStore.QueryPart()
    .equal('name', 'mike') //and
    .equal('rank', 'king');
//or
var queryPart2 = WL.JSONStore.QueryPart()
    .equal('name', 'carlos') //and
    .equal('rank', 'pawn');
//or
var queryPart3 = WL.JSONStore.QueryPart()
    .equal('name', 'carlos') //and
    .equal('rank', 'king');
//or
var queryPart4 = WL.JSONStore.QueryPart()
    .equal('name', 'mike') //and
    .equal('rank', 'pawn');

WL.JSONStore.get('people').advancedFind([queryPart1, queryPart2, queryPart3, queryPart4])
.then(...);

Everything inside a query part must match (i.e. it's like an and) and as long as one query part matches (i.e. it's like an or) results will be returned. Remember to work with top-level search fields.

Sometimes these fairly complex searches are required, but more often than not I would recommend re-thinking the offline experience. I wrote about that here.

FYI - Feature requests here. Bug reports here.

Upvotes: 1

Related Questions