Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to log queries with bounded paramenters in Sequelize?

Tags:

sequelize.js

I am using sequelize (version 5.3.5) to connect to a postgre database and have configured the logging to use console.log, but whenever a query with bounded parameters appears, I am unable to see which parameters are being bound.

The configuration is very standard.

import Sequelize from 'sequelize';
let db = new Sequelize (
    "database", "username", "password",
    {
        dialect: 'postgres',
        logging: console.log
    }
);

Taking for example this log of an INSERT operation (inside a transaction):

Executing (a7ed97b4-66a2-43a2-b4c5-eaa067e7ec28): INSERT INTO "Entities" ("id","type","createdAt","updatedAt") VALUES ($1,$2,$3,$4) RETURNING *;

Is there a way to make sequelize show me what values are being mapped to $1 .. $6?

like image 529
Rafael Schimassek Avatar asked Apr 16 '19 19:04

Rafael Schimassek


Video Answer


1 Answers

I revisited this after a while, and with sequelize at version 5.19.0 I found that there is a property where all the bound values are stored.

Going back to it:

import Sequelize from 'sequelize';
let db = new Sequelize (
    "database", "username", "password",
    {
        dialect: 'postgres',
        logging: customLogger
    }
);


function customLogger ( queryString, queryObject ) {
    console.log( queryString )      // outputs a string
    console.log( queryObject.bind ) // outputs an array
}
like image 187
Rafael Schimassek Avatar answered Sep 28 '22 18:09

Rafael Schimassek