Sequelize — Show or Log Generated SQL Query Statements

Sequelize is a Node.js ORM for all SQL databases, like MySQL, MariaDB, PostgreSQL, SQLite, and Microsoft SQL Server. By default, it logs all SQL queries to the console. This is helpful during development, but also noisy because your terminal is flooded with SQL statements.

This tutorial shows you how to enable logging in Sequelize for individual SQL queries.

Sequelize Series Overview

Log SQL Query to the Console

We published a related tutorial showing you how to disable Sequelize query logging. If you disabled query logging, you can still log individual queries when needed.

Here’s how: Sequelize supports the logging option when composing a query object. This configuration overrides the default logging setup in your SQL connector.

The logging option expects a log function, like console.log which receives the generates SQL statement. You can then log the SQL statement to the terminal or send it to a query collector. Here’s the query setup that logs the generated SQL statement to the console:

const User = require('../sequelize-models/user')

await User.findOne({

  logging: console.log, // log SQL statement to console

  where: { id: 2207 },
  include: […]
})

You can also write a custom logging function besides logging the SQL query to the console. Read the next paragraph for more details!

Custom Sequelize Logging Function

The logging option accepts a logging function that receives the generated SQL statement. You can write your own logging function and handle the query logging in your own way.

Sequelize passes two arguments to your custom logging function: the generated sql statement and a JavaScript queryObject. Typically you only want the SQL query. If you need more details about the query composition, you may look through the query object as well.

Here’s a sample custom logging function:

const User = require('../sequelize-models/user')

await User.findOne({

  logging: (sql, queryObject) => {
    sendToElasticAndLogToConsole(sql, queryObject)
  },

  where: { id: 2207 },
  include: […]
})

function sendToElasticAndLogToConsole (sql, queryObject) {  
  // save the `sql` query in Elasticsearch
  console.log(sql)

  // use the queryObject if needed (e.g. for debugging)
}

The generated sql query may look like this:

Executing (default): SELECT [user].[id], [user].[name], [user].[email] WHERE [user].[id] = 2207;  

Notice the “Executing (default):“ prefix in the generated SQL statement. You may need to remove this prefix if you only want the raw SQL query.

The related Sequelize query object (received as the second parameter) looks like this:

{
  plain: false,
  raw: false,
  logging: [Function: logging],
  where: '[user].[id] = 2207',
  include: [],
  hooks: true,
  rejectOnEmpty: false,
  originalAttributes: [
    'id',
    'name',
    'email'
  ],
  hasJoin: false,
  model: User,
  includeNames: [],
  includeMap: {},
  hasSingleAssociation: false,
  hasMultiAssociation: false,
  topModel: User,
  topLimit: undefined,
  hasDuplicating: true,
  hasRequired: true,
  hasWhere: true,
  hasIncludeWhere: true,
  hasIncludeRequired: true,
  subQuery: false,
  attributes: [
    'id',
    'name',
    'email'
  ],
  tableNames: [
    'user'
  ],
  type: 'SELECT',
  keysEscaped: true
}

The query object contains all the details about your SQL query as a JavaScript object. It may help you to determine whether the attribute mapping is correct or find missing joins. The query object contains all the query details.

Enjoy logging your SQL statements!


Mentioned Resources

Explore the Library

Find interesting tutorials and solutions for your problems.