Permalink
Browse files

feat: add customized SQL logger function support (#162)

  • Loading branch information...
XadillaX committed Dec 18, 2017
1 parent a70f782 commit 99b76e5dd16ffb2f1902195f191ca9585f284685
Showing with 52 additions and 4 deletions.
  1. +1 −0 docs/docs/getting-started.md
  2. +5 −1 lib/adapters/mysql.js
  3. +1 −0 package.json
  4. +44 −2 test/adapters/mysql.js
  5. +1 −1 test/toshihiko.js
@@ -33,6 +33,7 @@ const options = { // options for MySQL
port: 3306,
charset: "utf8mb4_bin",
connectionLimit: 10,
showSql: true,
cache: {
// cache options, if have
@@ -77,6 +77,10 @@ class MySQLAdapter extends Adapter {
port: 3306
}, this.options);

if(this.options.showSql === true) {
this.options.showSql = console.log.bind(console);
}

const PASSWORD = this.options.password;
Object.defineProperties(this, {
username: {
@@ -1221,7 +1225,7 @@ class MySQLAdapter extends Adapter {

debug(`executing sql 【${sql}`);
if(this.options.showSql) {
console.log(`❤️ Toshihiko is executing SQL: 【${sql}】...`);
this.options.showSql(sql);
}

if(conn) {
@@ -20,6 +20,7 @@
"toshihiko-mysqlparser": "^1.0.0"
},
"devDependencies": {
"chalk": "^2.3.0",
"conventional-changelog-cli": "1.3.5",
"coveralls": "3.0.0",
"cz-conventional-changelog": "2.1.0",
@@ -9,6 +9,7 @@
const path = require("path");

const async = require("async");
const chalk = require("chalk");
const decache = require("decache");
const otrans = require("otrans");
const runSync = require("sync-runner");
@@ -19,16 +20,40 @@ const common = require("../util/common");
const MySQLAdapter = require("../../lib/adapters/mysql");

describe("🐣 adapters/mysql", function() {
const _write = process.stdout.write;
let _last;
process.stdout.write = function() {
_last = arguments[0];
_write.apply(process.stdout, arguments);
};

const correctOptions = {
username: "root",
password: "",
database: "__toshihiko__",
charset: "utf8mb4_general_ci",
showSql: true
showSql: function(sql) {
const temp = _last
.replace("\u001b[32m", "")
.replace("\u001b[0m", "")
.replace("\u001b[90m", "");
let i = 0;
sql = chalk.gray(`>> sql: ${sql}`);
while(temp[i] === " ") {
sql = ` ${sql}`;
i++;
}

if(temp[i] !== ">") {
sql = ` ${sql}`;
}

console.log(sql);
}
};

before(function(done) {
const adapter = new MySQLAdapter({}, require("../../util/common").extend(correctOptions, {
const adapter = new MySQLAdapter({}, Object.assign({}, correctOptions, {
database: "mysql"
}));
adapter.execute("CREATE DATABASE IF NOT EXISTS `__toshihiko__`;", function(err) {
@@ -302,6 +327,23 @@ describe("🐣 adapters/mysql", function() {
});
});

describe("show sql", function() {
it("should use a certain logger", function(done) {
let logged = false;
const adapter = new MySQLAdapter({}, Object.assign({}, correctOptions, {
database: "mysql",
showSql: function(sql) {
sql.should.equal("HELLO WORLD");
logged = true;
}
}));
adapter.execute("HELLO WORLD", function(err) {
err.message.indexOf("HELLO WORLD").should.not.equal(-1);
if(logged) done();
});
});
});

require("./mysql_insert")(name, correctOptions);
require("./mysql_make")(name, correctOptions);
require("./mysql_find")(name, correctOptions);
@@ -21,7 +21,7 @@ describe("🐣 toshihiko", function() {
});

it("should create with base adapter using module", function() {
const options = { foo: 1 };
const options = { foo: 1, showSql: true };
const toshihiko = new Toshihiko(require("../lib/adapters/base"), options);
toshihiko.options.should.equal(options);
toshihiko.adapter.should.be.instanceof(require("../lib/adapters/base"));

0 comments on commit 99b76e5

Please sign in to comment.