Permalink
Browse files

feat: use common.extend to instead of config.util

  • Loading branch information...
XadillaX committed Oct 14, 2016
1 parent 3803fc0 commit 353e26087c21f0a6e15e6121b55bdbf7ec0e391a
Showing with 38 additions and 9 deletions.
  1. +3 −2 lib/adapters/base.js
  2. +13 −7 lib/adapters/mysql.js
  3. +22 −0 util/common.js
@@ -6,10 +6,11 @@
*/
"use strict";

const cu = require("config.util");
const debug = require("debug")("toshihiko:adapter:base");
const EventEmitter = require("eventemitter2").EventEmitter2;

const common = require("../../util/common");

class Adapter extends EventEmitter {
constructor(parent, options) {
super();
@@ -22,7 +23,7 @@ class Adapter extends EventEmitter {
enumerable: false
},
options: {
value: cu.extendDeep({}, options || {}),
value: common.extend({}, options || {}),
writable: true,
configurable: false,
enumerable: true
@@ -8,13 +8,13 @@

const _ = require("lodash");
const async = require("async");
const cu = require("config.util");
const debug = require("debug")("toshihiko:adapter:mysql");
const Scarlet = require("scarlet-task");
const SqlString = require("sqlstring");
const SqlParser = require("toshihiko-mysqlparser");

const Adapter = require("./base");
const common = require("../../util/common");
const escaper = require("../../util/escaper");

const FIELD_LOGICS = {
@@ -60,7 +60,7 @@ class MySQLAdapter extends Adapter {
constructor(parent, options) {
super(parent, options);

this.options = cu.extendDeep({}, {
this.options = common.extend({
username: "",
password: "",
database: "toshihiko",
@@ -80,11 +80,17 @@ class MySQLAdapter extends Adapter {
}
});

delete this.options.username;
delete this.options.database;
delete this.options.password;
try {
delete this.options.username;
delete this.options.database;
delete this.options.password;
} catch(e) {
// delete may occur error:
// not existing
// frozen
}

const opt = cu.extendDeep({}, this.options, {
const opt = common.extend(this.options, {
user: this.username,
database: this.database,
password: PASSWORD
@@ -704,7 +710,7 @@ class MySQLAdapter extends Adapter {
}

queryToOptions(query, options) {
const _options = cu.extendDeep({}, {
const _options = common.extend({
fields: query._fields,
where: query._where,
order: query._order,
@@ -6,6 +6,7 @@
*/
"use strict";

const _ = require("lodash");
const debug = require("debug")("toshihiko:common");

const STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg;
@@ -24,3 +25,24 @@ common.getParamNames = function(func) {
const result = fnStr.slice(fnStr.indexOf("(") + 1, fnStr.indexOf(")")).match(ARGUMENT_NAMES);
return (null === result) ? [] : result;
};

common.extend = function(_default, options) {
_default = _default || {};
const obj = _.cloneDeep(options);

for(let key in _default) {
if(!_default.hasOwnProperty(key)) continue;

if(undefined === obj[key]) {
obj[key] = _.cloneDeep(_default[key]);
continue;
}

if(typeof _default[key] === "object" && typeof obj[key] === "object") {
obj[key] = common.extend(obj[key], _default[key]);
continue;
}
}

return obj;
};

0 comments on commit 353e260

Please sign in to comment.