Skip to content

Commit

Permalink
feat: support export sql query template in logger (#314)
Browse files Browse the repository at this point in the history
Co-authored-by: JimmyDaddy <[email protected]>
  • Loading branch information
JimmyDaddy and JimmyDaddy authored May 16, 2022
1 parent 95f8636 commit ac39adb
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 28 deletions.
5 changes: 3 additions & 2 deletions src/drivers/mysql/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,20 +94,21 @@ class MysqlDriver extends AbstractDriver {
});
});
const sql = logger.format(query, values, opts);
const logOpts = { ...opts, query };
const start = performance.now();
let result;

try {
result = await promise;
} catch (err) {
logger.logQueryError(err, sql, calculateDuration(start), opts);
logger.logQueryError(err, sql, calculateDuration(start), logOpts);
throw err;
} finally {
if (!opts.connection) connection.release();
}

const [ results, fields ] = result;
logger.tryLogQuery(sql, calculateDuration(start), opts, results);
logger.tryLogQuery(sql, calculateDuration(start), logOpts, results);
if (fields) return { rows: results, fields };
return results;
}
Expand Down
5 changes: 3 additions & 2 deletions src/drivers/postgres/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,21 @@ class PostgresDriver extends AbstractDriver {
const command = sql.slice(0, sql.indexOf(' ')).toLowerCase();

async function tryQuery(...args) {
const logOpts = { ...spell, query: sql };
const formatted = logger.format(sql, values, spell);
const start = performance.now();
let result;

try {
result = await connection.query(...args);
} catch (err) {
logger.logQueryError(err, formatted, calculateDuration(start), spell);
logger.logQueryError(err, formatted, calculateDuration(start), logOpts);
throw err;
} finally {
if (!spell.connection) connection.release();
}

logger.tryLogQuery(formatted, calculateDuration(start), spell, result);
logger.tryLogQuery(formatted, calculateDuration(start), logOpts, result);
return result;
}

Expand Down
5 changes: 3 additions & 2 deletions src/drivers/sqlite/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,21 @@ class SqliteDriver extends AbstractDriver {
}

const { logger } = this;
const logOpts = { ...opts, query };
const sql = logger.format(query, values, opts);
const start = performance.now();
let result;

try {
result = await connection.query(query, values, opts);
} catch (err) {
logger.logQueryError(err, sql, calculateDuration(start), opts);
logger.logQueryError(err, sql, calculateDuration(start), logOpts);
throw err;
} finally {
if (!opts.connection) connection.release();
}

logger.tryLogQuery(sql, calculateDuration(start), opts, result);
logger.tryLogQuery(sql, calculateDuration(start), logOpts, result);
return result;
}

Expand Down
18 changes: 11 additions & 7 deletions test/unit/drivers/mysql/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,33 @@ describe('=> MySQL driver', () => {
result.push([ sql, duration, opts, res ]);
},
});
await driver2.query('SELECT 1');
await driver2.query('SELECT ?, ? FROM users WHERE email = ? AND status = ?', ['id', 'nickname', '[email protected]', 1]);
const [ sql, duration, opts, res ] = result[0];
assert.equal(sql, 'SELECT 1');
assert.equal(sql, "SELECT 'id', 'nickname' FROM users WHERE email = '[email protected]' AND status = 1");
assert.ok(duration >= 0);
assert.ok(res);
assert.ok(opts);
assert.equal(opts.query, 'SELECT ?, ? FROM users WHERE email = ? AND status = ?');
});

it('driver.logger.logQueryError', async () => {
const result = [];
const driver2 = new MysqlDriver({
...options,
logger: {
logQueryError(sql, err) {
result.push([ sql, err ]);
logQueryError(err, sql, duration, opts) {
result.push([ err, sql, duration, opts ]);
},
},
});
await assert.rejects(async () => await driver2.query('SELECT x'));
const [ err, sql ] = result[0];
assert.equal(sql, 'SELECT x');
await assert.rejects(async () => await driver2.query('SELECT x, ? FROM users WHERE email = ? AND status = ?', ['nickname', '[email protected]', 1]));
const [ err, sql, duration, opts ] = result[0];
assert.equal(sql, "SELECT x, 'nickname' FROM users WHERE email = '[email protected]' AND status = 1");
assert.ok(duration >= 0);
assert.ok(err);
assert.ok(/ER_BAD_FIELD_ERROR/.test(err.message));
assert.ok(opts);
assert.equal(opts.query, 'SELECT x, ? FROM users WHERE email = ? AND status = ?');
});

it('driver.querySchemaInfo()', async () => {
Expand Down
20 changes: 12 additions & 8 deletions test/unit/drivers/postgres/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,33 @@ describe('=> PostgreSQL driver', () => {
result.push([ sql, duration, opts, res ]);
},
});
await driver2.query('SELECT 1');
await driver2.query('SELECT ?, ? FROM users WHERE email = ? AND status = ?', ['id', 'nickname', '[email protected]', 1]);
const [ sql, duration, opts, res ] = result[0];
assert.equal(sql, 'SELECT 1');
assert.ok(duration > 0);
assert.equal(sql, "SELECT 'id', 'nickname' FROM users WHERE email = '[email protected]' AND status = 1");
assert.ok(duration >= 0);
assert.ok(res);
assert.ok(opts);
assert.equal(opts.query, 'SELECT ?, ? FROM users WHERE email = ? AND status = ?');
});

it('driver.logger.logQueryError', async () => {
const result = [];
const driver2 = new PostgresDriver({
...options,
logger: {
logQueryError(sql, err) {
result.push([ sql, err ]);
logQueryError(err, sql, duration, opts) {
result.push([ err, sql, duration, opts ]);
},
},
});
await assert.rejects(async () => await driver2.query('SELECT x'));
const [ err, sql ] = result[0];
assert.equal(sql, 'SELECT x');
await assert.rejects(async () => await driver2.query('SELECT x, ? FROM users WHERE email = ? AND status = ?', ['nickname', '[email protected]', 1]));
const [ err, sql, duration, opts ] = result[0];
assert.equal(sql, "SELECT x, 'nickname' FROM users WHERE email = '[email protected]' AND status = 1");
assert.ok(duration >= 0);
assert.ok(err);
assert.ok(/column "x" does not exist/.test(err.message));
assert.ok(opts);
assert.equal(opts.query, 'SELECT x, ? FROM users WHERE email = ? AND status = ?');
});

it('driver.querySchemaInfo()', async () => {
Expand Down
18 changes: 11 additions & 7 deletions test/unit/drivers/sqlite/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,33 @@ describe('=> SQLite driver', () => {
result.push([ sql, duration, opts, res ]);
},
});
await driver2.query('SELECT 1');
await driver2.query('SELECT ?, ? FROM users WHERE email = ? AND status = ?', ['id', 'nickname', '[email protected]', 1]);
const [ sql, duration, opts, res ] = result[0];
assert.equal(sql, 'SELECT 1');
assert.equal(sql, "SELECT 'id', 'nickname' FROM users WHERE email = '[email protected]' AND status = 1");
assert.ok(duration >= 0);
assert.ok(res);
assert.ok(opts);
assert.equal(opts.query, 'SELECT ?, ? FROM users WHERE email = ? AND status = ?');
});

it('driver.logger.logQueryError', async () => {
const result = [];
const driver2 = new SqliteDriver({
...options,
logger: {
logQueryError(sql, err) {
result.push([ sql, err ]);
logQueryError(err, sql, duration, opts) {
result.push([ err, sql, duration, opts ]);
},
},
});
await assert.rejects(async () => await driver2.query('SELECT x'));
const [ err, sql ] = result[0];
assert.equal(sql, 'SELECT x');
await assert.rejects(async () => await driver2.query('SELECT x, ? FROM users WHERE email = ? AND status = ?', ['nickname', '[email protected]', 1]));
const [ err, sql, duration, opts ] = result[0];
assert.equal(sql, "SELECT x, 'nickname' FROM users WHERE email = '[email protected]' AND status = 1");
assert.ok(duration >= 0);
assert.ok(err);
assert.ok(/no such column/.test(err.message));
assert.ok(opts);
assert.equal(opts.query, 'SELECT x, ? FROM users WHERE email = ? AND status = ?');
});

it('driver.querySchemaInfo()', async () => {
Expand Down
6 changes: 6 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,12 @@ export default class Realm {

connect(): Promise<Bone>;

/**
* disconnect manually
* @param callback
*/
disconnect(callback?: Function): Promise<boolean | void>;

define(
name: string,
attributes: Record<string, DataTypes<DataType> | AttributeMeta>,
Expand Down

0 comments on commit ac39adb

Please sign in to comment.