Marcus Ataide
Marcus Ataide

Reputation: 7530

Conditionally set a object property

Its possible to remove the .where clause if queryString is null?

I have tried ternary operator in the middle of expression, but doesn't work.

Also the equals expression dont allow: null, undefined or empty String.

CashierHistory
            .scan()
            .where('userId').equals(path['userId'])
            .where('status').equals(queryString['status']) # remove all this line in case of queryString['status'] is null
            .limit(10)
            .startKey(queryString)
            .exec(function (err, acc) {
                if (err == null) {
                    console.log(acc);
                    return cb(null, Api.response(acc));
                } else {
                    console.log(err['message']);
                    return cb(null, Api.errors(200, {5: err['message']}));
                }
            });

Edit: I am using Dynogels (An ORM for Dynamodb)

Upvotes: 1

Views: 344

Answers (1)

gabesoft
gabesoft

Reputation: 1228

As dandavis said you can break your query into two parts

var query = CashierHistory
        .scan()
        .where('userId').equals(path['userId']);

if (queryString['status']) {
    query = query.where('status').equals(queryString['status']);
}

return query.limit(10)
        .startKey(queryString)
        .exec(function (err, acc) {
            if (err == null) {
                console.log(acc);
                return cb(null, Api.response(acc));
            } else {
                console.log(err['message']);
                return cb(null, Api.errors(200, {5: err['message']}));
            }
        });

Upvotes: 3

Related Questions