Just getting started with Sequelize and trying to get migrations working and would like to see what SQL the migrations are actually running against the DB.
I found a github comment which suggests how to pipe the logging to the debug module, but where do I put the "options".
options: {
logging: debug('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.
Enable "logging":true in config conection data base (file data_base.json).
Example
{
"username": "postgres",
"password": "postgres",
"database": "agencias",
"host": "127.0.0.1",
"dialect": "postgres",
"timezone": "America/La_Paz",
"migrationStorage": "sequelize",
"migrationStorageTableName": "sequelize_migration",
"logging":true,
"pool": {
"max": 15,
"min": 0,
"idle": 10000
}
}
Execute in the project
sequelize db:migrate --migrations-path src/migrations --config data_base.json
There are a few possible places to add this for sequelize. In the .sequelizerc
if you want to see all logging for the sequelize CLI (See documentation) . If you just want to see it for specific queries in your up/down migrations, then you can pass it in for each queryInterface call as part of options.
For example, for createTable
you can do:
queryInterface.createTable('myNewTable', {
'my_column': Sequelize.STRING
}, {
logging: console.log
});
Sequelize CLI passes the configuration defined in config/config.js
to the sequelize constructor. Use the following to log SQL queries when running sequelize db:migrate
'use strict';
module.exports = {
development: {
host: process.env.MSSQL_SERVER || 'localhost',
username: process.env.MSSQL_USER || 'MSSQL_USER',
password: process.env.MSSQL_USER_PASS || 'MSSQL_PASS',
database: process.env.MSSQL_DB || 'DATABASE_NAME',
dialect: process.env.DIALECT || 'mssql',
logging: console.log
}
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With