diff --git a/lib/dao.js b/lib/dao.js index 65cbcf97..c8911985 100644 --- a/lib/dao.js +++ b/lib/dao.js @@ -43,11 +43,12 @@ function DataAccessObject() { } function idName(m) { - return m.dataSource.idName ? m.dataSource.idName(m.modelName) : 'id'; + return m.getDataSource().idName + ? m.getDataSource().idName(m.modelName) : 'id'; } function getIdValue(m, data) { - return data && data[m.dataSource.idName(m.modelName)]; + return data && data[m.getDataSource().idName(m.modelName)]; } function setIdValue(m, data, value) { @@ -57,7 +58,7 @@ function setIdValue(m, data, value) { } DataAccessObject._forDB = function (data) { - if(!(this.dataSource.isRelational && this.dataSource.isRelational())) { + if(!(this.getDataSource().isRelational && this.getDataSource().isRelational())) { return data; } var res = {}; @@ -83,7 +84,7 @@ DataAccessObject._forDB = function (data) { * - instance (null or Model) */ DataAccessObject.create = function (data, callback) { - if (stillConnecting(this.dataSource, this, arguments)) return; + if (stillConnecting(this.getDataSource(), this, arguments)) return; var Model = this; var modelName = Model.modelName; @@ -213,13 +214,13 @@ function stillConnecting(dataSource, obj, args) { * @param {Function} [callback] The callback function */ DataAccessObject.upsert = DataAccessObject.updateOrCreate = function upsert(data, callback) { - if (stillConnecting(this.dataSource, this, arguments)) return; + if (stillConnecting(this.getDataSource(), this, arguments)) return; var Model = this; if (!getIdValue(this, data)) return this.create(data, callback); - if (this.dataSource.connector.updateOrCreate) { + if (this.getDataSource().connector.updateOrCreate) { var inst = new Model(data); - this.dataSource.connector.updateOrCreate(Model.modelName, inst.toObject(true), function (err, data) { + this.getDataSource().connector.updateOrCreate(Model.modelName, inst.toObject(true), function (err, data) { var obj; if (data) { inst._initProperties(data, false); @@ -286,7 +287,7 @@ DataAccessObject.findOrCreate = function findOrCreate(query, data, callback) { * @param {Function} cb - callbacl called with (err, exists: Bool) */ DataAccessObject.exists = function exists(id, cb) { - if (stillConnecting(this.dataSource, this, arguments)) return; + if (stillConnecting(this.getDataSource(), this, arguments)) return; if (id !== undefined && id !== null && id !== '') { this.dataSource.connector.exists(this.modelName, id, cb); @@ -310,9 +311,9 @@ setRemoting(DataAccessObject.exists, { * @param {Function} cb - callback called with (err, instance) */ DataAccessObject.findById = function find(id, cb) { - if (stillConnecting(this.dataSource, this, arguments)) return; + if (stillConnecting(this.getDataSource(), this, arguments)) return; - this.dataSource.connector.find(this.modelName, id, function (err, data) { + this.getDataSource().connector.find(this.modelName, id, function (err, data) { var obj = null; if (data) { if (!getIdValue(this, data)) { @@ -358,7 +359,7 @@ DataAccessObject.all = function () { */ DataAccessObject.find = function find(params, cb) { - if (stillConnecting(this.dataSource, this, arguments)) return; + if (stillConnecting(this.getDataSource(), this, arguments)) return; if (arguments.length === 1) { cb = params; @@ -369,7 +370,7 @@ DataAccessObject.find = function find(params, cb) { params = params || {}; var fields = params.fields; var near = params && geo.nearFilter(params.where); - var supportsGeo = !!this.dataSource.connector.buildNearFilter; + var supportsGeo = !!this.getDataSource().connector.buildNearFilter; // normalize fields as array of included property names if(fields) { @@ -380,11 +381,11 @@ DataAccessObject.find = function find(params, cb) { if(near) { if(supportsGeo) { // convert it - this.dataSource.connector.buildNearFilter(params, near); + this.getDataSource().connector.buildNearFilter(params, near); } else if(params.where) { // do in memory query // using all documents - this.dataSource.connector.all(this.modelName, {}, function (err, data) { + this.getDataSource().connector.all(this.modelName, {}, function (err, data) { var memory = new Memory(); var modelName = constr.modelName; @@ -414,7 +415,7 @@ DataAccessObject.find = function find(params, cb) { } } - this.dataSource.connector.all(this.modelName, params, function (err, data) { + this.getDataSource().connector.all(this.modelName, params, function (err, data) { if (data && data.forEach) { data.forEach(function (d, i) { var obj = new constr(); @@ -456,7 +457,7 @@ setRemoting(DataAccessObject.find, { * @param {Function} cb - callback called with (err, instance) */ DataAccessObject.findOne = function findOne(params, cb) { - if (stillConnecting(this.dataSource, this, arguments)) return; + if (stillConnecting(this.getDataSource(), this, arguments)) return; if (typeof params === 'function') { cb = params; @@ -486,20 +487,20 @@ setRemoting(DataAccessObject.findOne, { DataAccessObject.remove = DataAccessObject.deleteAll = DataAccessObject.destroyAll = function destroyAll(where, cb) { - if (stillConnecting(this.dataSource, this, arguments)) return; + if (stillConnecting(this.getDataSource(), this, arguments)) return; if(!cb && 'function' === typeof where) { cb = where; where = undefined; } if(!where) { - this.dataSource.connector.destroyAll(this.modelName, function (err, data) { + this.getDataSource().connector.destroyAll(this.modelName, function (err, data) { cb && cb(err, data); }.bind(this)); } else { // Support an optional where object where = removeUndefined(where); - this.dataSource.connector.destroyAll(this.modelName, where, function (err, data) { + this.getDataSource().connector.destroyAll(this.modelName, where, function (err, data) { cb && cb(err, data); }.bind(this)); } @@ -513,9 +514,9 @@ DataAccessObject.destroyAll = function destroyAll(where, cb) { DataAccessObject.removeById = DataAccessObject.deleteById = DataAccessObject.destroyById = function deleteById(id, cb) { - if (stillConnecting(this.dataSource, this, arguments)) return; + if (stillConnecting(this.getDataSource(), this, arguments)) return; - this.dataSource.connector.destroy(this.modelName, id, function (err) { + this.getDataSource().connector.destroy(this.modelName, id, function (err) { if ('function' === typeof cb) { cb(err); } @@ -537,14 +538,14 @@ setRemoting(DataAccessObject.deleteById, { * @param {Function} cb - callback, called with (err, count) */ DataAccessObject.count = function (where, cb) { - if (stillConnecting(this.dataSource, this, arguments)) return; + if (stillConnecting(this.getDataSource(), this, arguments)) return; if (typeof where === 'function') { cb = where; where = null; } where = removeUndefined(where); - this.dataSource.connector.count(this.modelName, cb, where); + this.getDataSource().connector.count(this.modelName, cb, where); }; @@ -564,7 +565,7 @@ setRemoting(DataAccessObject.count, { * @param callback(err, obj) */ DataAccessObject.prototype.save = function (options, callback) { - if (stillConnecting(this.constructor.dataSource, this, arguments)) return; + if (stillConnecting(this.getDataSource(), this, arguments)) return; if (typeof options == 'function') { callback = options; @@ -638,7 +639,7 @@ DataAccessObject.prototype.isNewRecord = function () { * @private */ DataAccessObject.prototype._adapter = function () { - return this.dataSource.connector; + return this.getDataSource().connector; }; /** @@ -649,7 +650,7 @@ DataAccessObject.prototype._adapter = function () { DataAccessObject.prototype.remove = DataAccessObject.prototype.delete = DataAccessObject.prototype.destroy = function (cb) { - if (stillConnecting(this.constructor.dataSource, this, arguments)) return; + if (stillConnecting(this.getDataSource(), this, arguments)) return; this.trigger('destroy', function (destroyed) { this._adapter().destroy(this.constructor.modelName, getIdValue(this.constructor, this), function (err) { @@ -690,7 +691,7 @@ DataAccessObject.prototype.updateAttribute = function updateAttribute(name, valu * @param {Function} callback - callback called with (err, instance) */ DataAccessObject.prototype.updateAttributes = function updateAttributes(data, cb) { - if (stillConnecting(this.constructor.dataSource, this, arguments)) return; + if (stillConnecting(this.getDataSource(), this, arguments)) return; var inst = this; var model = this.constructor.modelName; @@ -757,7 +758,7 @@ setRemoting(DataAccessObject.prototype.updateAttributes, { * @param {Function} callback - called with (err, instance) arguments */ DataAccessObject.prototype.reload = function reload(callback) { - if (stillConnecting(this.constructor.dataSource, this, arguments)) return; + if (stillConnecting(this.getDataSource(), this, arguments)) return; this.constructor.findById(getIdValue(this.constructor, this), callback); }; diff --git a/lib/datasource.js b/lib/datasource.js index 113f7b37..9a4c5d08 100644 --- a/lib/datasource.js +++ b/lib/datasource.js @@ -1316,7 +1316,6 @@ DataSource.prototype.copyModel = function copyModel(Master) { var md = Master.dataSource.definitions[className]; var Slave = function SlaveModel() { Master.apply(this, [].slice.call(arguments)); - this.dataSource = dataSource; }; util.inherits(Slave, Master); diff --git a/lib/model-builder.js b/lib/model-builder.js index 3519e943..7fa9b91c 100644 --- a/lib/model-builder.js +++ b/lib/model-builder.js @@ -110,8 +110,8 @@ ModelBuilder.prototype.define = function defineClass(className, properties, sett return new ModelConstructor(data, dataSource); } ModelBaseClass.apply(this, arguments); - if(!this.dataSource) { - hiddenProperty(this, 'dataSource', dataSource || this.constructor.dataSource); + if(dataSource) { + hiddenProperty(this, '__dataSource', dataSource); } }; @@ -397,7 +397,6 @@ ModelBuilder.prototype.copyModel = function copyModel(Master) { var md = Master.dataSource.definitions[className]; var Slave = function SlaveModel() { Master.apply(this, [].slice.call(arguments)); - this.dataSource = dataSource; }; util.inherits(Slave, Master); diff --git a/lib/model.js b/lib/model.js index 88a8ceb3..ec8ec90e 100644 --- a/lib/model.js +++ b/lib/model.js @@ -289,5 +289,12 @@ ModelBaseClass.mixin = function(anotherClass, options) { return jutil.mixin(this, anotherClass, options); }; +ModelBaseClass.prototype.getDataSource = function () { + return this.__dataSource || this.constructor.dataSource; +} +ModelBaseClass.getDataSource = function () { + return this.dataSource; +} + jutil.mixin(ModelBaseClass, Hookable); jutil.mixin(ModelBaseClass, validations.Validatable); diff --git a/test/schema.test.js b/test/schema.test.js index 1a0d8f8c..2f9be1a4 100644 --- a/test/schema.test.js +++ b/test/schema.test.js @@ -9,7 +9,7 @@ describe('dataSource', function() { Model = db.define('Model'); Model.dataSource.should.eql(db); var m = new Model; - m.dataSource.should.eql(db); + m.getDataSource().should.eql(db); }); it('should clone existing model', function() { @@ -18,8 +18,8 @@ describe('dataSource', function() { slave.should.not.eql(db); var sm = new SlaveModel; sm.should.be.instanceOf(Model); - sm.dataSource.should.not.eql(db); - sm.dataSource.should.eql(slave); + sm.getDataSource().should.not.eql(db); + sm.getDataSource().should.eql(slave); }); it('should automigrate', function(done) {