Negate existing knex where clause in query builder
up vote
0
down vote
favorite
I have a knex QueryBuilder object qb that contains a number of where clauses. I would like to be able to wrap the entire where clause in a not statement effectively negating the entire where clause.
Example:
const qb = knex.queryBuilder();
qb.whereIn('name', ['alice', 'bob']);
// ... in some other part of the file ...
// Example of what I would like to do, effectively negate the above query if
// some condition is met.
if (negateSearch) {
const outerQb = knex.queryBuilder();
outerQb.whereNot(qb);
// execute outerQb
} else {
// execute qb
}
This should result in a query that looks like
where not (name in ('alice', 'bob'))`
knex.js
add a comment |
up vote
0
down vote
favorite
I have a knex QueryBuilder object qb that contains a number of where clauses. I would like to be able to wrap the entire where clause in a not statement effectively negating the entire where clause.
Example:
const qb = knex.queryBuilder();
qb.whereIn('name', ['alice', 'bob']);
// ... in some other part of the file ...
// Example of what I would like to do, effectively negate the above query if
// some condition is met.
if (negateSearch) {
const outerQb = knex.queryBuilder();
outerQb.whereNot(qb);
// execute outerQb
} else {
// execute qb
}
This should result in a query that looks like
where not (name in ('alice', 'bob'))`
knex.js
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a knex QueryBuilder object qb that contains a number of where clauses. I would like to be able to wrap the entire where clause in a not statement effectively negating the entire where clause.
Example:
const qb = knex.queryBuilder();
qb.whereIn('name', ['alice', 'bob']);
// ... in some other part of the file ...
// Example of what I would like to do, effectively negate the above query if
// some condition is met.
if (negateSearch) {
const outerQb = knex.queryBuilder();
outerQb.whereNot(qb);
// execute outerQb
} else {
// execute qb
}
This should result in a query that looks like
where not (name in ('alice', 'bob'))`
knex.js
I have a knex QueryBuilder object qb that contains a number of where clauses. I would like to be able to wrap the entire where clause in a not statement effectively negating the entire where clause.
Example:
const qb = knex.queryBuilder();
qb.whereIn('name', ['alice', 'bob']);
// ... in some other part of the file ...
// Example of what I would like to do, effectively negate the above query if
// some condition is met.
if (negateSearch) {
const outerQb = knex.queryBuilder();
outerQb.whereNot(qb);
// execute outerQb
} else {
// execute qb
}
This should result in a query that looks like
where not (name in ('alice', 'bob'))`
knex.js
knex.js
asked Nov 16 at 15:59
mchlstckl
1,16421017
1,16421017
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
That is not currently supported by knex.
In the future, with this feature one could probably achieve that by having where clause stored separately from rest of the query builder https://github.com/tgriesser/knex/issues/1893
So right now you need to change your code that uses knex to not actually build the where clause until you know if it should be negated or not.
add a comment |
up vote
0
down vote
If I understand what do you want, you can use modify:
knex('table')
.whereIn('name', ['alice', 'bob']);
.modify((query) => {
if (negateSearch) {
query.whereNot('yourcondition')
}
})
.orderBy('anyColumn', 'desc')
It will add the 'whereNot' condition only if negateSearch was true.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
That is not currently supported by knex.
In the future, with this feature one could probably achieve that by having where clause stored separately from rest of the query builder https://github.com/tgriesser/knex/issues/1893
So right now you need to change your code that uses knex to not actually build the where clause until you know if it should be negated or not.
add a comment |
up vote
0
down vote
That is not currently supported by knex.
In the future, with this feature one could probably achieve that by having where clause stored separately from rest of the query builder https://github.com/tgriesser/knex/issues/1893
So right now you need to change your code that uses knex to not actually build the where clause until you know if it should be negated or not.
add a comment |
up vote
0
down vote
up vote
0
down vote
That is not currently supported by knex.
In the future, with this feature one could probably achieve that by having where clause stored separately from rest of the query builder https://github.com/tgriesser/knex/issues/1893
So right now you need to change your code that uses knex to not actually build the where clause until you know if it should be negated or not.
That is not currently supported by knex.
In the future, with this feature one could probably achieve that by having where clause stored separately from rest of the query builder https://github.com/tgriesser/knex/issues/1893
So right now you need to change your code that uses knex to not actually build the where clause until you know if it should be negated or not.
answered Nov 18 at 17:22
Mikael Lepistö
6,54912627
6,54912627
add a comment |
add a comment |
up vote
0
down vote
If I understand what do you want, you can use modify:
knex('table')
.whereIn('name', ['alice', 'bob']);
.modify((query) => {
if (negateSearch) {
query.whereNot('yourcondition')
}
})
.orderBy('anyColumn', 'desc')
It will add the 'whereNot' condition only if negateSearch was true.
add a comment |
up vote
0
down vote
If I understand what do you want, you can use modify:
knex('table')
.whereIn('name', ['alice', 'bob']);
.modify((query) => {
if (negateSearch) {
query.whereNot('yourcondition')
}
})
.orderBy('anyColumn', 'desc')
It will add the 'whereNot' condition only if negateSearch was true.
add a comment |
up vote
0
down vote
up vote
0
down vote
If I understand what do you want, you can use modify:
knex('table')
.whereIn('name', ['alice', 'bob']);
.modify((query) => {
if (negateSearch) {
query.whereNot('yourcondition')
}
})
.orderBy('anyColumn', 'desc')
It will add the 'whereNot' condition only if negateSearch was true.
If I understand what do you want, you can use modify:
knex('table')
.whereIn('name', ['alice', 'bob']);
.modify((query) => {
if (negateSearch) {
query.whereNot('yourcondition')
}
})
.orderBy('anyColumn', 'desc')
It will add the 'whereNot' condition only if negateSearch was true.
answered Nov 19 at 11:31
Vandré A. Schaedler
112
112
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53341406%2fnegate-existing-knex-where-clause-in-query-builder%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown