Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore case using breeze FilterQueryOp

I am using breeze to query data from the server and seem to be running into problems. Is there a way to filter this data and ignore cases or making the value from the field a lower case? Example:

var term = "john"; 
query = query.where("Name", "contains", Term);

The problem I am having is if the 'Name' field contains John with capital 'J', It return false but returns true if I change term to 'John'. I know this is case issue but how can I make breeze ignore the casing? without using jquery.each.

Thanks. Any help will be greatly appreciated.

like image 911
dejobo Avatar asked Nov 15 '13 00:11

dejobo


2 Answers

In my opinion there is a simpler approach to this.

By default OData is case sensitive, but nonetheless provides functions to transform a string to lower or upper case. So to fire a case-insensitive query to the server simply modify your code as follows:

var term = "john"; 
query = query.where("tolower(Name)", breeze.FilterQueryOp.Contains, term.toLowerCase());

Thus OData is told to transform the subject to lower case before comparing it to your search string, which has been converted to lower case before sending it to the server.

like image 170
Spontifixus Avatar answered Sep 23 '22 09:09

Spontifixus


Ok, there are two parts to this. Breeze supports a LocalQueryComparisonOptions object that is used for all localQueries.

    var lqco = new breeze.LocalQueryComparisonOptions({
        name: "custom comparison options",
        isCaseSensitive: false,
        usesSql92CompliantStringComparison: true
    });
    // either apply it globally
    lqco.setAsDefault();
    // or to a specific MetadataStore
    var ms = new breeze.MetadataStore({ localQueryComparisonOptions: lqco });
    var em = new breeze.EntityManager( { metadataStore: ms });

You should set this once at the beginning of your application. In this example, all localQueries performed after this point will be case insensitive.

The problem is that unless your database is ALSO set to "match" these settings ( performing this differs by database vendor), then remote queries against the server will return different results then the same query applied locally.

Basically, Breeze cannot set the "server" side implementation, so the recommendation is usually to create a localQueryComparisons object that matches your server side database settings.

Hope this makes sense.

like image 42
Jay Traband Avatar answered Sep 22 '22 09:09

Jay Traband