Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

People also ask

How do I log a SQL query in Sequelize?

Log SQL Query to the Console 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.

How do I get raw query in Sequelize?

As there are often use cases in which it is just easier to execute raw / already prepared SQL queries, you can use the sequelize. query method. By default the function will return two arguments - a results array, and an object containing metadata (such as amount of affected rows, etc).

Is Sequelize SQL?

Sequelize is a modern TypeScript and Node. js ORM for Postgres, MySQL, MariaDB, SQLite and SQL Server, and more. Featuring solid transaction support, relations, eager and lazy loading, read replication and more.

Does Sequelize create database?

Each Sequelize ORM instance initializes a new connection to your database server with a specific database name. This means you must already have the database exists in your server before opening a connection with Sequelize.


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

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.