question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Proposal] Stand-alone Where Clauses

See original GitHub issue

I wanted to bounce a feature idea off the community here.

I think it’d be nice to have standalone where clauses that you could pass around. My proposal for the API would be:

var whereClause = knex.where({state: 'CA'});
knex.select().from('users').where(whereClause);

The purpose here would be the ability to pass where clauses into functions. So you could make a function that looks like this:

var getUsers = function(whereClause){
    return knex.select().from('users').where(whereClause);
};

and then call it with:

getUsers(knex.where({state:'CA'}));

(Obviously, this would be a useless abstraction - just trying to keep the example simple)

I, personally, have a use case where this functionality would be useful. In the example above, the solution might be to just getUsers().where({state:'CA'}) but for my use case, the where clause is actually applied to a subquery.

I currently solve the problem by defining the function like so:

var getUsers = function(userSelector){
    return knex.select().from('users').whereIn('username', userSelector);
};

and then executing it like so:

getUsers(knex.select('username').from('users').where({state: 'CA'})

(I realize that this solution has potential drawbacks but, for my use case, they’re not significant)

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:4
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
timhuffcommented, Feb 3, 2017

I agree. It’s a subtle difference but I do think the pull request remains a worthy improvement to the code base. As we’ve demonstrated, it’s not new functionality but it makes the API cleaner, IMO. I think the difference is most significant when building up where clauses. Here’s the before/after of that use-case:

Before:

var scopeToActiveUsers = function(whereClause){
    return function(){ this.where({active: true}).where(whereClause)};
};
var whereClause = function(){ this.where({state: 'CA'})};
var scopedWhereClause = scopeToActiveUsers(whereClause);
knex.select().from('users').where(scopedWhereClause);

After:

var scopeToActiveUsers = function(whereClause){
    return whereClause.where({active: true});
};
var whereClause = knex.where({state: 'CA'});
var scopedWhereClause = scopeToActiveUsers(whereClause);
knex.select().from('users').where(whereClause);

It’s not all that much less code but I would say the code is far cleaner and more readable.

For my use case, this difference is exacerbated by conditional where clauses, where I want to scope a query only under certain conditions.

0reactions
wubzzcommented, Jul 13, 2018

#2673 could potentially be used for this if merged. Alternatively a similar solution could be added to handle builders in .where:

if(column instanceof Builder) {
    const sql = column.toSQL();
    column = new Raw(column.client);
    column.set(sql.sql.substr(15), sql.bindings).wrap('(', ')');
}
var whereClause = knex.where({state: 'CA'});

console.log(knex.select().from('users').where(whereClause).toString());
//select * from "users" where ("state" = 'CA')

But there are probably lots of edge cases to consider, I’m guessing… For example that would also allow and update/insert sql builder to be used in .where which would most likely break.

Read more comments on GitHub >

github_iconTop Results From Across the Web

6 Linguistic Principles to Improve Your Proposal Edits
This clause holds the main idea, and its location impacts meaning. In a cumulative sentence, this main clause comes first, followed by the ......
Read more >
PART 2452 - SOLICITATION PROVISIONS AND CONTRACT ...
Subpart 2452.2 - Texts of Provisions and Clauses ... Proposals shall be enclosed in sealed packaging and addressed to the office specified ...
Read more >
New clauses and new schedules - MPs' Guide to Procedure
A new clause or a new schedule is a proposal for a new standalone element of the bill. If there's nowhere obvious to...
Read more >
How to Write Terms & Conditions to Protect Your Company ...
Ensure your company and proposals are safeguarded with these nine details ... about how you will protect their data both online and offline....
Read more >
How to Write a Project Proposal | Proofed's Writing Tips
Executive summary – Most project proposals will include an executive summary. This is a standalone document that summarizes the key points of ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found