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.

Bookshelfjs ORM How to get or see SQL statement its building

See original GitHub issue

When building ORM query, I want to see what the actual (raw) query is that is executed.

For example in Rails we can do like this:

User.where(name: 'Oscar').to_sql
# => SELECT "users".* FROM "users"  WHERE "users"."name" = 'Oscar'

This feature present in Bookshelfjs? or any other way to get this?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:14 (7 by maintainers)

github_iconTop GitHub Comments

9reactions
vellotiscommented, Jul 12, 2016

Hi @raj-optisol

You can use .query().toSQL() or .query().toString(). But achieving exact result as in Rails is a bit more complicated as queries may be not complete. The cause is that many statements get applied just before performing the query in Bookshelf. For example Bookshelf relations behave so. Also many plugins use events to apply query statements. If you want to debug the queries then I would suggest you to use Knex#debug instead. For example

model.query(function(qb) {
  qb.debug(true);
}).fetch()

It prints the debug info in the console.

5reactions
Paxacommented, Sep 15, 2016

If you run with DEBUG=knex:query it will print queries but it will be something like:

  knex:query select "users".* from "users" where "company_id" = ? limit ? +3ms

If you need to see more - you can patch current code and add custom logging:

var util = require('util');
var knexClient = require('knex/lib/client');
var origQuery = knexClient.prototype.query;
knexClient.prototype.query = function (connection, obj) {
  console.log(`SQL: ${obj.sql}  --  ${util.inspect(this.prepBindings(obj.bindings))}`);
  return origQuery.apply(this, arguments);
};

Output will be:

SQL: select "users".* from "users" where "company_id" = ? limit ?  --  [45, 1]
Read more comments on GitHub >

github_iconTop Results From Across the Web

Bookshelfjs ORM How to get or see SQL statement its building
Looks like there is no direct way to get SQL statement like Rails produce.. You can use .query() .toSQL() or .query() .toString().
Read more >
Bookshelf.js | Home
Bookshelf is a JavaScript ORM for Node.js, built on the Knex SQL query builder. It features both Promise-based and traditional callback interfaces, transaction ......
Read more >
Learn Bookshelf JS - A NodeJS ORM with Knex Query Builder
Bookshelf is a JavaScript ORM for Node. js, built on the Knex SQL query builder. It can work with PostgreSQL, MySQL, and SQLite3....
Read more >
Bookshelf.js: A Node.js ORM - Stack Abuse
js ORM packages. It stems from the Knex.js, which is a flexible query builder that works with PostgreSQL, MySQL and SQLite3. Bookshelf.js builds ......
Read more >
Getting Started with Bookshelf.js - SitePoint
There are many ORM libraries that exist for Node.js, the popular ones being ... We need knex query builder because bookshelf depends on...
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