How can I view the query `knex` builds?
https://knexjs.org/guide/interfaces.html#other knex(‘statistics’) .del() .where({ ‘stats’: ‘reddit’, }).toSQL().toNative()
https://knexjs.org/guide/interfaces.html#other knex(‘statistics’) .del() .where({ ‘stats’: ‘reddit’, }).toSQL().toNative()
Sequelize is full blown ORM forcing you to hide SQL behind object representation. Knex is plain query builder, which is way too low level tool for application development. Better to use objection.js it combines good parts of ORMs without compromising power of writing any kind of SQL queries. Here is good article about it from … Read more
I think I figured it out. In knex.js, say you specify a table like: knex.select( ‘*’ ).from( ‘Users’ ) Then you can just add the AS keyword within the quotes of the table name to alias it, like so: knex.select( ‘*’ ).from( ‘Users AS u’ ) ..and you can do this for column names, too; … Read more
You can call .orderBy multiple times to order by multiple columns: knex .select() .table(‘products’) .orderBy(‘name’, ‘desc’) .orderBy(‘id’, ‘asc’)
Read carefully from knex documentation how to pass values to knex raw (https://knexjs.org/guide/raw.html#raw). If you are passing values as parameter binding to raw like: knex.raw(‘select * from foo where id = ?’, [1]) In that case parameters and query string are passed separately to database driver protecting query from SQL injection. Other query builder methods … Read more
With Postgres, you’ll need a trigger. Here’s a method I’ve used successfully. Add a function If you have multiple migration files in a set order, you might need to artificially change the datestamp in the filename to get this to run first (or just add it to your first migration file). If you can’t roll … Read more
I solved this problem with these versions: “knex”: “^0.21.1”, “objection”: “^2.1.3”, “pg”: “^8.0.3”
Yes. Use modify. As applied to your example: router.get(“https://stackoverflow.com/questions”, function (req, res) { knex(‘questions’) .select(‘question’, ‘correct’, ‘incorrect’) .limit(50) .modify(function(queryBuilder) { if (req.query.param) { queryBuilder.where(‘somecolumn’, req.query.param); } }) .then(function (results) { res.send(results); }); });