Question

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?

Was it helpful?

Solution

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 })

OTHER TIPS

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:

User.findAll({where: {...}, logging: console.log})

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top