Merge pull request #33 from strongloop/get-datasource
Remove model.dataSource property / Add model.getDataSource() method
This commit is contained in:
commit
65bb5c8ead
57
lib/dao.js
57
lib/dao.js
|
@ -43,11 +43,12 @@ function DataAccessObject() {
|
||||||
}
|
}
|
||||||
|
|
||||||
function idName(m) {
|
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) {
|
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) {
|
function setIdValue(m, data, value) {
|
||||||
|
@ -57,7 +58,7 @@ function setIdValue(m, data, value) {
|
||||||
}
|
}
|
||||||
|
|
||||||
DataAccessObject._forDB = function (data) {
|
DataAccessObject._forDB = function (data) {
|
||||||
if(!(this.dataSource.isRelational && this.dataSource.isRelational())) {
|
if(!(this.getDataSource().isRelational && this.getDataSource().isRelational())) {
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
var res = {};
|
var res = {};
|
||||||
|
@ -83,7 +84,7 @@ DataAccessObject._forDB = function (data) {
|
||||||
* - instance (null or Model)
|
* - instance (null or Model)
|
||||||
*/
|
*/
|
||||||
DataAccessObject.create = function (data, callback) {
|
DataAccessObject.create = function (data, callback) {
|
||||||
if (stillConnecting(this.dataSource, this, arguments)) return;
|
if (stillConnecting(this.getDataSource(), this, arguments)) return;
|
||||||
|
|
||||||
var Model = this;
|
var Model = this;
|
||||||
var modelName = Model.modelName;
|
var modelName = Model.modelName;
|
||||||
|
@ -213,13 +214,13 @@ function stillConnecting(dataSource, obj, args) {
|
||||||
* @param {Function} [callback] The callback function
|
* @param {Function} [callback] The callback function
|
||||||
*/
|
*/
|
||||||
DataAccessObject.upsert = DataAccessObject.updateOrCreate = function upsert(data, callback) {
|
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;
|
var Model = this;
|
||||||
if (!getIdValue(this, data)) return this.create(data, callback);
|
if (!getIdValue(this, data)) return this.create(data, callback);
|
||||||
if (this.dataSource.connector.updateOrCreate) {
|
if (this.getDataSource().connector.updateOrCreate) {
|
||||||
var inst = new Model(data);
|
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;
|
var obj;
|
||||||
if (data) {
|
if (data) {
|
||||||
inst._initProperties(data, false);
|
inst._initProperties(data, false);
|
||||||
|
@ -286,7 +287,7 @@ DataAccessObject.findOrCreate = function findOrCreate(query, data, callback) {
|
||||||
* @param {Function} cb - callbacl called with (err, exists: Bool)
|
* @param {Function} cb - callbacl called with (err, exists: Bool)
|
||||||
*/
|
*/
|
||||||
DataAccessObject.exists = function exists(id, cb) {
|
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 !== '') {
|
if (id !== undefined && id !== null && id !== '') {
|
||||||
this.dataSource.connector.exists(this.modelName, id, cb);
|
this.dataSource.connector.exists(this.modelName, id, cb);
|
||||||
|
@ -310,9 +311,9 @@ setRemoting(DataAccessObject.exists, {
|
||||||
* @param {Function} cb - callback called with (err, instance)
|
* @param {Function} cb - callback called with (err, instance)
|
||||||
*/
|
*/
|
||||||
DataAccessObject.findById = function find(id, cb) {
|
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;
|
var obj = null;
|
||||||
if (data) {
|
if (data) {
|
||||||
if (!getIdValue(this, data)) {
|
if (!getIdValue(this, data)) {
|
||||||
|
@ -358,7 +359,7 @@ DataAccessObject.all = function () {
|
||||||
*/
|
*/
|
||||||
|
|
||||||
DataAccessObject.find = function find(params, cb) {
|
DataAccessObject.find = function find(params, cb) {
|
||||||
if (stillConnecting(this.dataSource, this, arguments)) return;
|
if (stillConnecting(this.getDataSource(), this, arguments)) return;
|
||||||
|
|
||||||
if (arguments.length === 1) {
|
if (arguments.length === 1) {
|
||||||
cb = params;
|
cb = params;
|
||||||
|
@ -369,7 +370,7 @@ DataAccessObject.find = function find(params, cb) {
|
||||||
params = params || {};
|
params = params || {};
|
||||||
var fields = params.fields;
|
var fields = params.fields;
|
||||||
var near = params && geo.nearFilter(params.where);
|
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
|
// normalize fields as array of included property names
|
||||||
if(fields) {
|
if(fields) {
|
||||||
|
@ -380,11 +381,11 @@ DataAccessObject.find = function find(params, cb) {
|
||||||
if(near) {
|
if(near) {
|
||||||
if(supportsGeo) {
|
if(supportsGeo) {
|
||||||
// convert it
|
// convert it
|
||||||
this.dataSource.connector.buildNearFilter(params, near);
|
this.getDataSource().connector.buildNearFilter(params, near);
|
||||||
} else if(params.where) {
|
} else if(params.where) {
|
||||||
// do in memory query
|
// do in memory query
|
||||||
// using all documents
|
// 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 memory = new Memory();
|
||||||
var modelName = constr.modelName;
|
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) {
|
if (data && data.forEach) {
|
||||||
data.forEach(function (d, i) {
|
data.forEach(function (d, i) {
|
||||||
var obj = new constr();
|
var obj = new constr();
|
||||||
|
@ -456,7 +457,7 @@ setRemoting(DataAccessObject.find, {
|
||||||
* @param {Function} cb - callback called with (err, instance)
|
* @param {Function} cb - callback called with (err, instance)
|
||||||
*/
|
*/
|
||||||
DataAccessObject.findOne = function findOne(params, cb) {
|
DataAccessObject.findOne = function findOne(params, cb) {
|
||||||
if (stillConnecting(this.dataSource, this, arguments)) return;
|
if (stillConnecting(this.getDataSource(), this, arguments)) return;
|
||||||
|
|
||||||
if (typeof params === 'function') {
|
if (typeof params === 'function') {
|
||||||
cb = params;
|
cb = params;
|
||||||
|
@ -486,20 +487,20 @@ setRemoting(DataAccessObject.findOne, {
|
||||||
DataAccessObject.remove =
|
DataAccessObject.remove =
|
||||||
DataAccessObject.deleteAll =
|
DataAccessObject.deleteAll =
|
||||||
DataAccessObject.destroyAll = function destroyAll(where, cb) {
|
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) {
|
if(!cb && 'function' === typeof where) {
|
||||||
cb = where;
|
cb = where;
|
||||||
where = undefined;
|
where = undefined;
|
||||||
}
|
}
|
||||||
if(!where) {
|
if(!where) {
|
||||||
this.dataSource.connector.destroyAll(this.modelName, function (err, data) {
|
this.getDataSource().connector.destroyAll(this.modelName, function (err, data) {
|
||||||
cb && cb(err, data);
|
cb && cb(err, data);
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
} else {
|
} else {
|
||||||
// Support an optional where object
|
// Support an optional where object
|
||||||
where = removeUndefined(where);
|
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);
|
cb && cb(err, data);
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
}
|
}
|
||||||
|
@ -513,9 +514,9 @@ DataAccessObject.destroyAll = function destroyAll(where, cb) {
|
||||||
DataAccessObject.removeById =
|
DataAccessObject.removeById =
|
||||||
DataAccessObject.deleteById =
|
DataAccessObject.deleteById =
|
||||||
DataAccessObject.destroyById = function deleteById(id, cb) {
|
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) {
|
if ('function' === typeof cb) {
|
||||||
cb(err);
|
cb(err);
|
||||||
}
|
}
|
||||||
|
@ -537,14 +538,14 @@ setRemoting(DataAccessObject.deleteById, {
|
||||||
* @param {Function} cb - callback, called with (err, count)
|
* @param {Function} cb - callback, called with (err, count)
|
||||||
*/
|
*/
|
||||||
DataAccessObject.count = function (where, cb) {
|
DataAccessObject.count = function (where, cb) {
|
||||||
if (stillConnecting(this.dataSource, this, arguments)) return;
|
if (stillConnecting(this.getDataSource(), this, arguments)) return;
|
||||||
|
|
||||||
if (typeof where === 'function') {
|
if (typeof where === 'function') {
|
||||||
cb = where;
|
cb = where;
|
||||||
where = null;
|
where = null;
|
||||||
}
|
}
|
||||||
where = removeUndefined(where);
|
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)
|
* @param callback(err, obj)
|
||||||
*/
|
*/
|
||||||
DataAccessObject.prototype.save = function (options, callback) {
|
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') {
|
if (typeof options == 'function') {
|
||||||
callback = options;
|
callback = options;
|
||||||
|
@ -638,7 +639,7 @@ DataAccessObject.prototype.isNewRecord = function () {
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
DataAccessObject.prototype._adapter = function () {
|
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.remove =
|
||||||
DataAccessObject.prototype.delete =
|
DataAccessObject.prototype.delete =
|
||||||
DataAccessObject.prototype.destroy = function (cb) {
|
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.trigger('destroy', function (destroyed) {
|
||||||
this._adapter().destroy(this.constructor.modelName, getIdValue(this.constructor, this), function (err) {
|
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)
|
* @param {Function} callback - callback called with (err, instance)
|
||||||
*/
|
*/
|
||||||
DataAccessObject.prototype.updateAttributes = function updateAttributes(data, cb) {
|
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 inst = this;
|
||||||
var model = this.constructor.modelName;
|
var model = this.constructor.modelName;
|
||||||
|
@ -757,7 +758,7 @@ setRemoting(DataAccessObject.prototype.updateAttributes, {
|
||||||
* @param {Function} callback - called with (err, instance) arguments
|
* @param {Function} callback - called with (err, instance) arguments
|
||||||
*/
|
*/
|
||||||
DataAccessObject.prototype.reload = function reload(callback) {
|
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);
|
this.constructor.findById(getIdValue(this.constructor, this), callback);
|
||||||
};
|
};
|
||||||
|
|
|
@ -1335,7 +1335,6 @@ DataSource.prototype.copyModel = function copyModel(Master) {
|
||||||
var md = Master.dataSource.definitions[className];
|
var md = Master.dataSource.definitions[className];
|
||||||
var Slave = function SlaveModel() {
|
var Slave = function SlaveModel() {
|
||||||
Master.apply(this, [].slice.call(arguments));
|
Master.apply(this, [].slice.call(arguments));
|
||||||
this.dataSource = dataSource;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
util.inherits(Slave, Master);
|
util.inherits(Slave, Master);
|
||||||
|
|
|
@ -110,8 +110,8 @@ ModelBuilder.prototype.define = function defineClass(className, properties, sett
|
||||||
return new ModelConstructor(data, dataSource);
|
return new ModelConstructor(data, dataSource);
|
||||||
}
|
}
|
||||||
ModelBaseClass.apply(this, arguments);
|
ModelBaseClass.apply(this, arguments);
|
||||||
if(!this.dataSource) {
|
if(dataSource) {
|
||||||
hiddenProperty(this, 'dataSource', dataSource || this.constructor.dataSource);
|
hiddenProperty(this, '__dataSource', dataSource);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -397,7 +397,6 @@ ModelBuilder.prototype.copyModel = function copyModel(Master) {
|
||||||
var md = Master.dataSource.definitions[className];
|
var md = Master.dataSource.definitions[className];
|
||||||
var Slave = function SlaveModel() {
|
var Slave = function SlaveModel() {
|
||||||
Master.apply(this, [].slice.call(arguments));
|
Master.apply(this, [].slice.call(arguments));
|
||||||
this.dataSource = dataSource;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
util.inherits(Slave, Master);
|
util.inherits(Slave, Master);
|
||||||
|
|
|
@ -289,5 +289,12 @@ ModelBaseClass.mixin = function(anotherClass, options) {
|
||||||
return jutil.mixin(this, 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, Hookable);
|
||||||
jutil.mixin(ModelBaseClass, validations.Validatable);
|
jutil.mixin(ModelBaseClass, validations.Validatable);
|
||||||
|
|
|
@ -9,7 +9,7 @@ describe('dataSource', function() {
|
||||||
Model = db.define('Model');
|
Model = db.define('Model');
|
||||||
Model.dataSource.should.eql(db);
|
Model.dataSource.should.eql(db);
|
||||||
var m = new Model;
|
var m = new Model;
|
||||||
m.dataSource.should.eql(db);
|
m.getDataSource().should.eql(db);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should clone existing model', function() {
|
it('should clone existing model', function() {
|
||||||
|
@ -18,8 +18,8 @@ describe('dataSource', function() {
|
||||||
slave.should.not.eql(db);
|
slave.should.not.eql(db);
|
||||||
var sm = new SlaveModel;
|
var sm = new SlaveModel;
|
||||||
sm.should.be.instanceOf(Model);
|
sm.should.be.instanceOf(Model);
|
||||||
sm.dataSource.should.not.eql(db);
|
sm.getDataSource().should.not.eql(db);
|
||||||
sm.dataSource.should.eql(slave);
|
sm.getDataSource().should.eql(slave);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should automigrate', function(done) {
|
it('should automigrate', function(done) {
|
||||||
|
|
Loading…
Reference in New Issue