orm-model
Advanced tools
Comparing version 0.0.6 to 0.0.7
@@ -11,2 +11,15 @@ 'use strict'; | ||
/** | ||
* Returns logger. | ||
* | ||
* @param {boolean|function} | ||
* @return {boolean|function} | ||
*/ | ||
function buildLogger(logger, orm, connector) { | ||
return typeof logger != 'function' ? logger : function() { | ||
logger.apply(null, [orm, connector].concat(Array.prototype.slice.call(arguments))); | ||
}; | ||
} | ||
/** | ||
* This module defines `mongoose` and `sequelize` database connections. | ||
@@ -63,3 +76,5 @@ * | ||
var connector = connectors[name]; | ||
this._connections[name] = require('./connections/'+connector.orm)(connector, options); | ||
this._connections[name] = require('./connections/'+connector.orm)(connector, { | ||
logger: buildLogger(options.logger, connector.orm, name) | ||
}); | ||
}.bind(this)); | ||
@@ -66,0 +81,0 @@ }, |
@@ -9,4 +9,89 @@ 'use strict'; | ||
var mongoose = require('mongoose'); | ||
var util = require('util'); | ||
var utils = require('mongoose/lib/utils'); | ||
/* | ||
* Helper for `buildLogger`. | ||
* (copy & paste from mongoose module) | ||
*/ | ||
function print (arg) { | ||
var type = typeof arg; | ||
if ('function' === type || 'undefined' === type) return ''; | ||
var data = format(arg); | ||
return typeof data == 'object' ? JSON.stringify(data) : data; | ||
} | ||
/* | ||
* Helper for `buildLogger`. | ||
* (copy & paste from mongoose module + removing return statement) | ||
*/ | ||
function format (obj, sub) { | ||
var x = utils.clone(obj); | ||
if (x) { | ||
if ('Binary' === x.constructor.name) { | ||
x = '[object Buffer]'; | ||
} else if ('ObjectID' === x.constructor.name) { | ||
var representation = 'ObjectId("' + x.toHexString() + '")'; | ||
x = { inspect: function() { return representation; } }; | ||
} else if ('Date' === x.constructor.name) { | ||
var representation = 'new Date("' + x.toUTCString() + '")'; | ||
x = { inspect: function() { return representation; } }; | ||
} else if ('Object' === x.constructor.name) { | ||
var keys = Object.keys(x) | ||
, i = keys.length | ||
, key | ||
while (i--) { | ||
key = keys[i]; | ||
if (x[key]) { | ||
if ('Binary' === x[key].constructor.name) { | ||
x[key] = '[object Buffer]'; | ||
} else if ('Object' === x[key].constructor.name) { | ||
x[key] = format(x[key], true); | ||
} else if ('ObjectID' === x[key].constructor.name) { | ||
;(function(x){ | ||
var representation = 'ObjectId("' + x[key].toHexString() + '")'; | ||
x[key] = { inspect: function() { return representation; } }; | ||
})(x) | ||
} else if ('Date' === x[key].constructor.name) { | ||
;(function(x){ | ||
var representation = 'new Date("' + x[key].toUTCString() + '")'; | ||
x[key] = { inspect: function() { return representation; } }; | ||
})(x) | ||
} else if (Array.isArray(x[key])) { | ||
x[key] = x[key].map(function (o) { | ||
return format(o, true) | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
if (sub) return x; | ||
} | ||
return x; // modified | ||
} | ||
/** | ||
* Returns logger. | ||
* | ||
* @param {boolean|function} | ||
* @return {boolean|function} | ||
*/ | ||
function buildLogger(logger) { | ||
return typeof logger != 'function' ? logger : function() { | ||
logger(util.format( | ||
'%s.%s(%s) %s %s %s', | ||
print(arguments[0]), | ||
print(arguments[1]), | ||
print(arguments[2]), | ||
print(arguments[3]), | ||
print(arguments[4]), | ||
print(arguments[5]) | ||
)); | ||
}; | ||
} | ||
/** | ||
* Mongoose connection. | ||
@@ -20,3 +105,3 @@ * | ||
module.exports = function(connector, options) { | ||
mongoose.set('debug', options.logger || false); | ||
mongoose.set('debug', buildLogger(options.logger)); | ||
@@ -23,0 +108,0 @@ var conn = mongoose.createConnection(connector.uris, connector.options); |
@@ -20,8 +20,7 @@ 'use strict'; | ||
module.exports = function(connector, options) { | ||
var logger = options.logger==true ? console.log : options.logger; | ||
var conn = new Sequelize(connector.database, connector.username, connector.password, _.merge({ | ||
logging: logger | ||
}, connector.options)); | ||
options.logging = options.logger==true ? console.log : options.logger; | ||
var conn = new Sequelize(connector.database, connector.username, connector.password, options); | ||
conn.close = conn.close || function() {}; // parent module expects close() method that closes the connection | ||
return conn; | ||
}; |
{ | ||
"name": "orm-model", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"description": "Unified MVC-style structure for ORM models.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
24094
701