clarkie / dynogels

DynamoDB data mapper for node.js. Originally forked from https://github.com/ryanfitz/vogels
Other
490 stars 110 forks source link

Logic gates on chained functions (query) #35

Closed csi-lk closed 8 years ago

csi-lk commented 8 years ago

Hi there, sorry if this is a silly question but i'm looking for a solution to do if statements on query function chains, I currently have it like this:

if(paginateKey){
        Post.query(hashKey)
            .descending()
            .limit(10)
            .startKey(parseInt(paginateKey))
            .exec(fn);
    } else {
        Post.query(hashKey)
            .descending()
            .limit(10)
            .exec(fn);
    }

Wondering if theres some sort of way to simplify it to something like:

Post.query(hashKey)
            .descending()
            .limit(10)
            paginateKey ? .startKey(parseInt(paginateKey)) : ()
            .exec(fn);

My use case is a lot more complex than this, just put together the above as a simplistic example

Thanks :)

csi-lk commented 8 years ago

Figured it out by doing

var query = Post.query(hashKey).descending().limit(10)
if(paginateKey) {
    query.startKey(paginateKey)
}
query.exec();

Happy to close this cheers