Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

knex.js - debug only SQL

Is there any way to display only SQL queries on console when debugging mode is on? I want to reduce the amount of informations which is displayed.

Thanks for the help ;)

like image 427
rizidoro Avatar asked Jan 22 '14 19:01

rizidoro


People also ask

How do I debug KNEX?

How do I debug? # Knex is beginning to make use of the debug module internally, so you can set the DEBUG environment variable to knex:* to see all debugging, or select individual namespaces DEBUG=knex:query,knex:tx to constrain a bit.

Is KNEX JS an ORM?

Sequelize is an ORM that includes some query builder stuff; Knex is just a query builder, not an ORM.

What is Knexjs?

Knex.js (pronounced /kəˈnɛks/) is a "batteries included" SQL query builder for PostgreSQL, CockroachDB, MSSQL, MySQL, MariaDB, SQLite3, Better-SQLite3, Oracle, and Amazon Redshift designed to be flexible, portable, and fun to use.

Is KNEX secure?

js is safe by default”


3 Answers

Set environment variables to configure the debug module:

  • DEBUG=knex:query for just queries
  • DEBUG=knex:tx for transactions
  • and DEBUG=knex* for everything.
like image 180
timruffs Avatar answered Oct 14 '22 05:10

timruffs


If what you need is to show the query string, one way is to register a function that logs the query data using the event 'query' that Knex emit just before executing the query.

For example:

var knex = require( 'knex' );
knex.on( 'query', function( queryData ) {
    console.log( queryData );
});

After that, before every query, the anonymous function is called and queryData contains json with the information about the query.

like image 44
Emanuele Benedetti Avatar answered Oct 14 '22 05:10

Emanuele Benedetti


Actually, if you're using MySQL, you can set

debug: ['ComQueryPacket']

as part of the config settings for mysql (not Knex).

I'll looking into adding this as an option in Knex though.

like image 5
tgriesser Avatar answered Oct 14 '22 07:10

tgriesser