Skip to content

Commit

Permalink
feat: accept queryName query option for logging.
Browse files Browse the repository at this point in the history
  • Loading branch information
sandinmyjoints committed Jul 22, 2022
1 parent ec26c44 commit 4ab5074
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/dialects/abstract/query.d.ts
Expand Up @@ -22,6 +22,7 @@ export interface AbstractQueryOptions {
* A function that gets executed while running the query to log the sql.
*/
logging?: boolean | ((sql: string, timing?: number) => void);
queryName?: string;

include: boolean;
includeNames: unknown[];
Expand Down
7 changes: 4 additions & 3 deletions src/dialects/abstract/query.js
Expand Up @@ -290,14 +290,15 @@ export class AbstractQuery {
}

const fmt = `(${connection.uuid || 'default'}): ${sql}${logParameter}`;
const msg = `Executing ${fmt}`;
const queryName = options.queryName ? `${options.queryName}\n` : '';
const msg = `${queryName}Executing ${fmt}`;
debugContext(msg);
if (!benchmark) {
this.sequelize.log(`Executing ${fmt}`, options);
this.sequelize.log(`${queryName}Executing ${fmt}`, options);
}

return () => {
const afterMsg = `Executed ${fmt}`;
const afterMsg = `${queryName}Executed ${fmt}`;
debugContext(afterMsg);
if (benchmark) {
this.sequelize.log(afterMsg, Date.now() - startTime, options);
Expand Down
3 changes: 2 additions & 1 deletion src/dialects/db2/query.js
Expand Up @@ -39,7 +39,8 @@ export class Db2Query extends AbstractQuery {
if (benchmark) {
queryBegin = Date.now();
} else {
this.sequelize.log(`Executing (${this.connection.uuid || 'default'}): ${this.sql}`, this.options);
const queryName = this.options.queryName ? `${this.options.queryName}\n` : '';
this.sequelize.log(`${queryName}Executing (${this.connection.uuid || 'default'}): ${this.sql}`, this.options);
}

const errStack = new Error().stack;
Expand Down
13 changes: 13 additions & 0 deletions test/integration/sequelize/query.test.js
Expand Up @@ -130,6 +130,19 @@ describe(Support.getTestDialectTeaser('Sequelize'), () => {
expect(typeof logger.args[0][1] === 'number').to.be.true;
});

it('executes a query with queryName option and custom logger', async () => {
const logger = sinon.spy();
const sequelize = Support.createSequelizeInstance({
logging: logger,
});

await sequelize.query(`select 1${dialect === 'ibmi' ? ' FROM SYSIBM.SYSDUMMY1' : ''};`, {
queryName: 'tricky select',
});
expect(logger.calledOnce).to.be.true;
expect(logger.args[0][0]).to.be.match(/tricky select[\n]Executing \((\d*|default)\): select 1/);
});

describe('with logQueryParameters', () => {
beforeEach(async function () {
this.sequelize = Support.createSequelizeInstance({
Expand Down

0 comments on commit 4ab5074

Please sign in to comment.