diff --git a/src/drivers/mysql/index.js b/src/drivers/mysql/index.js index 62437015..2a6c1545 100644 --- a/src/drivers/mysql/index.js +++ b/src/drivers/mysql/index.js @@ -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; } diff --git a/src/drivers/postgres/index.js b/src/drivers/postgres/index.js index 236e9f46..a362ba48 100644 --- a/src/drivers/postgres/index.js +++ b/src/drivers/postgres/index.js @@ -51,6 +51,7 @@ 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; @@ -58,13 +59,13 @@ class PostgresDriver extends AbstractDriver { 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; } diff --git a/src/drivers/sqlite/index.js b/src/drivers/sqlite/index.js index dee60070..0ac5793c 100644 --- a/src/drivers/sqlite/index.js +++ b/src/drivers/sqlite/index.js @@ -53,6 +53,7 @@ class SqliteDriver extends AbstractDriver { } const { logger } = this; + const logOpts = { ...opts, query }; const sql = logger.format(query, values, opts); const start = performance.now(); let result; @@ -60,13 +61,13 @@ class SqliteDriver extends AbstractDriver { 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; } diff --git a/test/unit/drivers/mysql/index.test.js b/test/unit/drivers/mysql/index.test.js index 7dd0b591..5df2ef4f 100644 --- a/test/unit/drivers/mysql/index.test.js +++ b/test/unit/drivers/mysql/index.test.js @@ -26,12 +26,13 @@ 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', 'yhorm@giant.com', 1]); const [ sql, duration, opts, res ] = result[0]; - assert.equal(sql, 'SELECT 1'); + assert.equal(sql, "SELECT 'id', 'nickname' FROM users WHERE email = 'yhorm@giant.com' 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 () => { @@ -39,16 +40,19 @@ describe('=> MySQL driver', () => { 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', 'yhorm@giant.com', 1])); + const [ err, sql, duration, opts ] = result[0]; + assert.equal(sql, "SELECT x, 'nickname' FROM users WHERE email = 'yhorm@giant.com' 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 () => { diff --git a/test/unit/drivers/postgres/index.test.js b/test/unit/drivers/postgres/index.test.js index 4c2f6656..58ca933d 100644 --- a/test/unit/drivers/postgres/index.test.js +++ b/test/unit/drivers/postgres/index.test.js @@ -26,12 +26,13 @@ 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', 'yhorm@giant.com', 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 = 'yhorm@giant.com' 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 () => { @@ -39,16 +40,19 @@ describe('=> PostgreSQL driver', () => { 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', 'yhorm@giant.com', 1])); + const [ err, sql, duration, opts ] = result[0]; + assert.equal(sql, "SELECT x, 'nickname' FROM users WHERE email = 'yhorm@giant.com' 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 () => { diff --git a/test/unit/drivers/sqlite/index.test.js b/test/unit/drivers/sqlite/index.test.js index f4a8c4ed..66effc84 100644 --- a/test/unit/drivers/sqlite/index.test.js +++ b/test/unit/drivers/sqlite/index.test.js @@ -28,12 +28,13 @@ 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', 'yhorm@giant.com', 1]); const [ sql, duration, opts, res ] = result[0]; - assert.equal(sql, 'SELECT 1'); + assert.equal(sql, "SELECT 'id', 'nickname' FROM users WHERE email = 'yhorm@giant.com' 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 () => { @@ -41,16 +42,19 @@ describe('=> SQLite driver', () => { 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', 'yhorm@giant.com', 1])); + const [ err, sql, duration, opts ] = result[0]; + assert.equal(sql, "SELECT x, 'nickname' FROM users WHERE email = 'yhorm@giant.com' 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 () => { diff --git a/types/index.d.ts b/types/index.d.ts index d908a8db..3ef90b0e 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -881,6 +881,12 @@ export default class Realm { connect(): Promise; + /** + * disconnect manually + * @param callback + */ + disconnect(callback?: Function): Promise; + define( name: string, attributes: Record | AttributeMeta>,