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'))`









share|improve this question


























    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'))`









    share|improve this question
























      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'))`









      share|improve this question













      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 16 at 15:59









      mchlstckl

      1,16421017




      1,16421017
























          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.






          share|improve this answer




























            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.






            share|improve this answer





















              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              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

























              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.






              share|improve this answer

























                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.






                share|improve this answer























                  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.






                  share|improve this answer












                  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.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 18 at 17:22









                  Mikael Lepistö

                  6,54912627




                  6,54912627
























                      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.






                      share|improve this answer

























                        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.






                        share|improve this answer























                          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.






                          share|improve this answer












                          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.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 19 at 11:31









                          Vandré A. Schaedler

                          112




                          112






























                              draft saved

                              draft discarded




















































                              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.




                              draft saved


                              draft discarded














                              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





















































                              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







                              Popular posts from this blog

                              Paul Cézanne

                              UIScrollView CustomStickyHeader Resize height generates problems when scroll is too fast

                              Angular material date-picker (MatDatepicker) auto completes the date on focus out