How To Apply Filtering Criteria Before Returning Data

Environment

Service Business Logic
Feature Cloud Code for Data

Applicable only to Cloud Code for Data.

Description

Often you may need to apply a strict filter on the items returned from the data operation or determine whether a filtering expression is incoming with the request.

Solution

Setting the following filter in the beforeRead event handler will instruct the server to return only the items where the field IsActive equals true.

var filter = {
    "IsActive": true
};

request.filterExpression = filter;

Another example is to restrict the returned result only to items that are modified after a certain date:

var rangeStart = new Date();
rangeStart.setDate(rangeStart.getDate() - 10);

request.filterExpression = { "ModifiedAt": {"$gte": rangeStart }}; // items modified in the last 10 days

You may also need to combine the incoming filter (if any) with the business logic filter.

if(request.filterExpression && Object.keys(request.filterExpression).length > 0) {
    var incomingFilter = request.filterExpression;

    var myFilter = {
        "IsActive": true
    };

    newFilter = {
        $and: [incomingFilter, myFilter]
    }; 

    request.filterExpression = newFilter; 
}

See Also

Start a free trial Request a demo
Contact us: +1-888-365-2779
sales@telerik.com
Copyright © 2016-2017, Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved.