How can I see the SQL generated by Sequelize.js?

node.jssequelize.js

node.js Problem Overview


I want to see the SQL commands that are sent to the PostgreSQL server because I need to check if they are correct. In particular, I am interested in the table creation commands.

For instance, ActiveRecord (Ruby) prints its SQL statements to standard output. Is this possible with Node.js/ActionHero.js and Sequelize.js as well?

node.js Solutions


Solution 1 - node.js

You can pass a logging option when initializing sequelize, which can either be a function or console.log

var sequelize = new Sequelize('database', 'username', 'password', {
    logging: console.log
    logging: function (str) {
        // do your own logging
    }
});

You can also pass a logging option to .sync if you only want to view the table creation queries

sequelize.sync({ logging: console.log })

Solution 2 - node.js

As stated in the log Error: Please note that find* was refactored and uses only one options object from now on.. For the latest sequelize version (4) if you want to have the result for only one command:

Solution 3 - node.js

If you want to look at the sequelize for one command you can listen to it and attach a function to the print the sql.

Check out this example:

User.find(1).on('sql', console.log).then(function(user) {
  // do whatever you want with the user here

Solution 4 - node.js

You can also take advantage of Sequelize's use of the Debug module, by setting your environment, thus: DEBUG=sequelize:sql* before starting your app.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionideaboxerView Question on Stackoverflow
Solution 1 - node.jsJan Aagaard MeierView Answer on Stackoverflow
Solution 2 - node.jsAdlen AfaneView Answer on Stackoverflow
Solution 3 - node.jsvpontisView Answer on Stackoverflow
Solution 4 - node.jssteevView Answer on Stackoverflow