Clone knex instance
See original GitHub issueIs 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:
- Created 10 years ago
- Comments:12 (5 by maintainers)
Top 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 >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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Knex is great, but mutability and a fluent/chainable API makes a dangerous and counter-intuitive combination. Registering interest in a
immutableQueryBuilder: true
option.@elhigu Do you think there is value in the following approach:
Knex QueryBuilder supports an intermediate JSON/serializable representation (along with fromJSON and toJSON functions) and internals (Query compiler etc.) depend on this JSON.
Two query builder implementations (mutable and immutable) can target the same representation internally with same (or almost same) APIs.
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.