From 12199ad77906f9f8ce9c0194ef0175c4aa03917e Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Thu, 11 Jul 2013 09:55:26 -0700 Subject: [PATCH] Add support to use adapter constructor for initialization --- lib/datasource.js | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/lib/datasource.js b/lib/datasource.js index bd5e094f..6cc08365 100644 --- a/lib/datasource.js +++ b/lib/datasource.js @@ -170,7 +170,7 @@ DataSource.prototype.setup = function(name, settings) { } if (adapter) { - adapter.initialize(this, function () { + var postInit = function postInit() { // we have an adaper now? if (!this.adapter) { @@ -192,7 +192,16 @@ DataSource.prototype.setup = function(name, settings) { this.connected = true; this.emit('connected'); - }.bind(this)); + }.bind(this); + + if ('function' === typeof adapter.initialize) { + // Call the async initialize method + adapter.initialize(this, postInit); + } else if('function' === typeof adapter) { + // Use the adapter constructor directly + this.adapter = new adapter(this.settings); + postInit(); + } } schema.connect = function(cb) { @@ -277,7 +286,7 @@ DataSource.prototype.define = function defineClass(className, properties, settin // add data access objects this.mixin(NewClass); - if(this.adapter) { + if(this.adapter && this.adapter.define) { // pass control to adapter this.adapter.define({ model: NewClass, @@ -344,7 +353,7 @@ DataSource.prototype.attach = function (ModelCtor) { this.mixin(ModelCtor); - if(this.adapter) { + if(this.adapter && this.adapter.define) { // pass control to adapter this.adapter.define({ model: ModelCtor, @@ -1160,7 +1169,7 @@ DataSource.prototype.copyModel = function copyModel(Master) { settings: md.settings }; - if (!schema.isTransaction) { + if ((!schema.isTransaction) && schema.adapter && schema.adapter.define) { schema.adapter.define({ model: Slave, properties: md.properties,