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.

Clone knex instance

See original GitHub issue

Is it possible to clone a knex instance, in order to reuse code?. For example, supose we have:

var query = knex('some-table as t1')
  .select('something')
  .join('other-table as t2', 't1.id', '=', 't2.t1_id');

function where1() {
  //reuse previous joins in query, 
  //but with specific where
  var q = query.clone()
    .where('t1.a', '=', 1);
  q.exec(function(err, response) {
    //do something
  });
}

function where1() {
  //reuse previous joins in query, 
  //but with specific where and join
  var q = query.clone()
    .join('third-table as t3', 't2.id', '=', 't3.t2_id')
    .where('t3.b', '=', 1);
  q.exec(function(err, response) {
    //do something
  });
}

Thanks.

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
rcrogerscommented, Dec 21, 2020

Knex is great, but mutability and a fluent/chainable API makes a dangerous and counter-intuitive combination. Registering interest in a immutableQueryBuilder: true option.

0reactions
lorefnoncommented, Oct 29, 2019

@elhigu Do you think there is value in the following approach:

  1. Knex QueryBuilder supports an intermediate JSON/serializable representation (along with fromJSON and toJSON functions) and internals (Query compiler etc.) depend on this JSON.

  2. Two query builder implementations (mutable and immutable) can target the same representation internally with same (or almost same) APIs.

  3. Over time the mutable one can be deprecated if there are no obvious performance concerns.

If this is something that can potentially be adopted, I can fiddle around this over the next couple weeks to gauge how much effort and testing will be required.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Knex Query Builder
Uses ON DUPLICATE KEY UPDATE in MySQL, and adds an ON CONFLICT (columns) DO ... This allows modifying the context for the cloned...
Read more >
Instance Methods | Objection.js
transactionOrKnex, object, Optional transaction or knex instance for the query. ... If the item to be cloned has instances of Model as properties...
Read more >
How do I reset query part with knex.js - Stack Overflow
To reset query part there is a way for example: this.qb. ... You can clone the query and add parts to those individual...
Read more >
Knex cheatsheet - Devhints
Update ; ) .where ; 135 }) ; { · : 'hi@example.com' ...
Read more >
Build a Simple CRUD Node.js App with CockroachDB and ...
Build a Simple CRUD Node.js App with CockroachDB and Knex.js ... Use Knex.js ... git clone https://github.com/cockroachlabs/example-app-node-knex ...
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