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