2013-04-11 23:23:34 +00:00
|
|
|
/**
|
|
|
|
* Dependencies
|
|
|
|
*/
|
|
|
|
var i8n = require('inflection');
|
|
|
|
var defineScope = require('./scope.js').defineScope;
|
2013-06-05 21:33:52 +00:00
|
|
|
var ModelBaseClass = require('./model.js');
|
2013-04-11 23:23:34 +00:00
|
|
|
|
2013-06-05 21:33:52 +00:00
|
|
|
module.exports = Relation;
|
|
|
|
|
|
|
|
function Relation() {
|
|
|
|
}
|
2013-04-11 23:23:34 +00:00
|
|
|
|
2013-06-05 21:33:52 +00:00
|
|
|
Relation.relationNameFor = function relationNameFor(foreignKey) {
|
2013-04-18 19:00:15 +00:00
|
|
|
for (var rel in this.relations) {
|
|
|
|
if (this.relations[rel].type === 'belongsTo' && this.relations[rel].keyFrom === foreignKey) {
|
|
|
|
return rel;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-04-11 23:23:34 +00:00
|
|
|
/**
|
|
|
|
* Declare hasMany relation
|
|
|
|
*
|
2013-06-05 21:33:52 +00:00
|
|
|
* @param {Relation} anotherClass - class to has many
|
2013-04-11 23:23:34 +00:00
|
|
|
* @param {Object} params - configuration {as:, foreignKey:}
|
|
|
|
* @example `User.hasMany(Post, {as: 'posts', foreignKey: 'authorId'});`
|
|
|
|
*/
|
2013-06-05 21:33:52 +00:00
|
|
|
Relation.hasMany = function hasMany(anotherClass, params) {
|
2013-04-11 23:23:34 +00:00
|
|
|
var thisClass = this, thisClassName = this.modelName;
|
|
|
|
params = params || {};
|
|
|
|
if (typeof anotherClass === 'string') {
|
|
|
|
params.as = anotherClass;
|
|
|
|
if (params.model) {
|
|
|
|
anotherClass = params.model;
|
|
|
|
} else {
|
|
|
|
var anotherClassName = i8n.singularize(anotherClass).toLowerCase();
|
2013-07-23 18:16:43 +00:00
|
|
|
for(var name in this.dataSource.models) {
|
2013-04-11 23:23:34 +00:00
|
|
|
if (name.toLowerCase() === anotherClassName) {
|
2013-07-23 18:16:43 +00:00
|
|
|
anotherClass = this.dataSource.models[name];
|
2013-04-11 23:23:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-10-25 23:25:05 +00:00
|
|
|
var methodName = params.as || i8n.camelize(anotherClass.pluralModelName, true);
|
2013-04-11 23:23:34 +00:00
|
|
|
var fk = params.foreignKey || i8n.camelize(thisClassName + '_id', true);
|
|
|
|
|
2013-11-05 06:53:02 +00:00
|
|
|
var idName = this.dataSource.idName(this.modelName) || 'id';
|
|
|
|
|
2013-04-18 19:00:15 +00:00
|
|
|
this.relations[methodName] = {
|
2013-04-11 23:23:34 +00:00
|
|
|
type: 'hasMany',
|
2013-11-05 06:53:02 +00:00
|
|
|
keyFrom: idName,
|
2013-04-11 23:23:34 +00:00
|
|
|
keyTo: fk,
|
|
|
|
modelTo: anotherClass,
|
|
|
|
multiple: true
|
|
|
|
};
|
|
|
|
// each instance of this class should have method named
|
|
|
|
// pluralize(anotherClass.modelName)
|
2013-11-05 06:53:02 +00:00
|
|
|
// which is actually just anotherClass.all({where: {thisModelNameId: this[idName]}}, cb);
|
2013-04-12 21:35:06 +00:00
|
|
|
var scopeMethods = {
|
2013-06-24 19:42:58 +00:00
|
|
|
findById: find,
|
2013-04-11 23:23:34 +00:00
|
|
|
destroy: destroy
|
2013-04-12 21:35:06 +00:00
|
|
|
};
|
|
|
|
if (params.through) {
|
|
|
|
var fk2 = i8n.camelize(anotherClass.modelName + '_id', true);
|
|
|
|
scopeMethods.create = function create(data, done) {
|
|
|
|
if (typeof data !== 'object') {
|
|
|
|
done = data;
|
|
|
|
data = {};
|
|
|
|
}
|
2013-04-18 19:00:15 +00:00
|
|
|
if ('function' !== typeof done) {
|
|
|
|
done = function() {};
|
|
|
|
}
|
|
|
|
var self = this;
|
2013-11-05 06:53:02 +00:00
|
|
|
var id = this[idName];
|
2013-04-12 21:35:06 +00:00
|
|
|
anotherClass.create(data, function(err, ac) {
|
|
|
|
if (err) return done(err, ac);
|
|
|
|
var d = {};
|
2013-04-18 19:00:15 +00:00
|
|
|
d[params.through.relationNameFor(fk)] = self;
|
|
|
|
d[params.through.relationNameFor(fk2)] = ac;
|
2013-04-12 21:35:06 +00:00
|
|
|
params.through.create(d, function(e) {
|
|
|
|
if (e) {
|
|
|
|
ac.destroy(function() {
|
|
|
|
done(e);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
done(err, ac);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
scopeMethods.add = function(acInst, done) {
|
|
|
|
var data = {};
|
2013-04-18 19:00:15 +00:00
|
|
|
var query = {};
|
2013-11-05 06:53:02 +00:00
|
|
|
query[fk] = this[idName];
|
2013-04-18 19:00:15 +00:00
|
|
|
data[params.through.relationNameFor(fk)] = this;
|
2013-11-05 06:53:02 +00:00
|
|
|
query[fk2] = acInst[idName] || acInst;
|
2013-04-18 19:00:15 +00:00
|
|
|
data[params.through.relationNameFor(fk2)] = acInst;
|
|
|
|
params.through.findOrCreate({where: query}, data, done);
|
2013-04-12 21:35:06 +00:00
|
|
|
};
|
|
|
|
scopeMethods.remove = function(acInst, done) {
|
|
|
|
var self = this;
|
|
|
|
var q = {};
|
2013-11-05 06:53:02 +00:00
|
|
|
q[fk2] = acInst[idName] || acInst;
|
2013-04-12 21:35:06 +00:00
|
|
|
params.through.findOne({where: q}, function(err, d) {
|
|
|
|
if (err) {
|
|
|
|
return done(err);
|
|
|
|
}
|
|
|
|
if (!d) {
|
|
|
|
return done();
|
|
|
|
}
|
|
|
|
d.destroy(done);
|
|
|
|
});
|
|
|
|
};
|
2013-04-13 01:35:53 +00:00
|
|
|
delete scopeMethods.destroy;
|
2013-04-12 21:35:06 +00:00
|
|
|
}
|
|
|
|
defineScope(this.prototype, params.through || anotherClass, methodName, function () {
|
|
|
|
var filter = {};
|
|
|
|
filter.where = {};
|
2013-11-05 06:53:02 +00:00
|
|
|
filter.where[fk] = this[idName];
|
2013-04-12 21:35:06 +00:00
|
|
|
if (params.through) {
|
|
|
|
filter.collect = i8n.camelize(anotherClass.modelName, true);
|
|
|
|
filter.include = filter.collect;
|
|
|
|
}
|
|
|
|
return filter;
|
|
|
|
}, scopeMethods);
|
2013-04-11 23:23:34 +00:00
|
|
|
|
2013-04-13 01:35:53 +00:00
|
|
|
if (!params.through) {
|
|
|
|
// obviously, anotherClass should have attribute called `fk`
|
2013-07-23 18:16:43 +00:00
|
|
|
anotherClass.dataSource.defineForeignKey(anotherClass.modelName, fk, this.modelName);
|
2013-04-13 01:35:53 +00:00
|
|
|
}
|
2013-04-11 23:23:34 +00:00
|
|
|
|
|
|
|
function find(id, cb) {
|
2013-06-24 19:42:58 +00:00
|
|
|
anotherClass.findById(id, function (err, inst) {
|
2013-04-11 23:23:34 +00:00
|
|
|
if (err) return cb(err);
|
|
|
|
if (!inst) return cb(new Error('Not found'));
|
2013-11-05 06:53:02 +00:00
|
|
|
if (inst[fk] && inst[fk].toString() == this[idName].toString()) {
|
2013-04-11 23:23:34 +00:00
|
|
|
cb(null, inst);
|
|
|
|
} else {
|
|
|
|
cb(new Error('Permission denied'));
|
|
|
|
}
|
|
|
|
}.bind(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
function destroy(id, cb) {
|
2013-10-27 19:55:01 +00:00
|
|
|
var self = this;
|
|
|
|
anotherClass.findById(id, function (err, inst) {
|
2013-04-11 23:23:34 +00:00
|
|
|
if (err) return cb(err);
|
2013-10-27 19:55:01 +00:00
|
|
|
if (!inst) return cb(new Error('Not found'));
|
2013-11-05 06:53:02 +00:00
|
|
|
if (inst[fk] && inst[fk].toString() == self[idName].toString()) {
|
2013-04-11 23:23:34 +00:00
|
|
|
inst.destroy(cb);
|
|
|
|
} else {
|
2013-10-27 19:55:01 +00:00
|
|
|
cb(new Error('Permission denied'));
|
2013-04-11 23:23:34 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Declare belongsTo relation
|
|
|
|
*
|
|
|
|
* @param {Class} anotherClass - class to belong
|
|
|
|
* @param {Object} params - configuration {as: 'propertyName', foreignKey: 'keyName'}
|
|
|
|
*
|
|
|
|
* **Usage examples**
|
|
|
|
* Suppose model Post have a *belongsTo* relationship with User (the author of the post). You could declare it this way:
|
|
|
|
* Post.belongsTo(User, {as: 'author', foreignKey: 'userId'});
|
|
|
|
*
|
|
|
|
* When a post is loaded, you can load the related author with:
|
|
|
|
* post.author(function(err, user) {
|
|
|
|
* // the user variable is your user object
|
|
|
|
* });
|
|
|
|
*
|
|
|
|
* The related object is cached, so if later you try to get again the author, no additional request will be made.
|
|
|
|
* But there is an optional boolean parameter in first position that set whether or not you want to reload the cache:
|
|
|
|
* post.author(true, function(err, user) {
|
|
|
|
* // The user is reloaded, even if it was already cached.
|
|
|
|
* });
|
|
|
|
*
|
|
|
|
* This optional parameter default value is false, so the related object will be loaded from cache if available.
|
|
|
|
*/
|
2013-06-05 21:33:52 +00:00
|
|
|
Relation.belongsTo = function (anotherClass, params) {
|
2013-04-11 23:23:34 +00:00
|
|
|
params = params || {};
|
|
|
|
if ('string' === typeof anotherClass) {
|
|
|
|
params.as = anotherClass;
|
|
|
|
if (params.model) {
|
|
|
|
anotherClass = params.model;
|
|
|
|
} else {
|
|
|
|
var anotherClassName = anotherClass.toLowerCase();
|
2013-07-23 18:16:43 +00:00
|
|
|
for(var name in this.dataSource.models) {
|
2013-04-11 23:23:34 +00:00
|
|
|
if (name.toLowerCase() === anotherClassName) {
|
2013-07-23 18:16:43 +00:00
|
|
|
anotherClass = this.dataSource.models[name];
|
2013-04-11 23:23:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-11-05 06:53:02 +00:00
|
|
|
|
|
|
|
var idName = this.dataSource.idName(this.modelName) || 'id';
|
2013-04-11 23:23:34 +00:00
|
|
|
var methodName = params.as || i8n.camelize(anotherClass.modelName, true);
|
|
|
|
var fk = params.foreignKey || methodName + 'Id';
|
|
|
|
|
2013-04-12 21:35:06 +00:00
|
|
|
this.relations[methodName] = {
|
2013-04-11 23:23:34 +00:00
|
|
|
type: 'belongsTo',
|
|
|
|
keyFrom: fk,
|
2013-11-05 06:53:02 +00:00
|
|
|
keyTo: idName,
|
2013-04-11 23:23:34 +00:00
|
|
|
modelTo: anotherClass,
|
|
|
|
multiple: false
|
|
|
|
};
|
|
|
|
|
2013-07-23 18:16:43 +00:00
|
|
|
this.dataSource.defineForeignKey(this.modelName, fk, anotherClass.modelName);
|
2013-04-11 23:23:34 +00:00
|
|
|
this.prototype['__finders__'] = this.prototype['__finders__'] || {};
|
|
|
|
|
|
|
|
this.prototype['__finders__'][methodName] = function (id, cb) {
|
|
|
|
if (id === null) {
|
|
|
|
cb(null, null);
|
|
|
|
return;
|
|
|
|
}
|
2013-06-24 19:42:58 +00:00
|
|
|
anotherClass.findById(id, function (err,inst) {
|
2013-04-11 23:23:34 +00:00
|
|
|
if (err) return cb(err);
|
|
|
|
if (!inst) return cb(null, null);
|
2013-11-05 06:53:02 +00:00
|
|
|
if (inst[idName] === this[fk]) {
|
2013-04-11 23:23:34 +00:00
|
|
|
cb(null, inst);
|
|
|
|
} else {
|
|
|
|
cb(new Error('Permission denied'));
|
|
|
|
}
|
|
|
|
}.bind(this));
|
|
|
|
};
|
|
|
|
|
|
|
|
this.prototype[methodName] = function (refresh, p) {
|
|
|
|
if (arguments.length === 1) {
|
|
|
|
p = refresh;
|
|
|
|
refresh = false;
|
|
|
|
} else if (arguments.length > 2) {
|
|
|
|
throw new Error('Method can\'t be called with more than two arguments');
|
|
|
|
}
|
|
|
|
var self = this;
|
|
|
|
var cachedValue;
|
|
|
|
if (!refresh && this.__cachedRelations && (typeof this.__cachedRelations[methodName] !== 'undefined')) {
|
|
|
|
cachedValue = this.__cachedRelations[methodName];
|
|
|
|
}
|
2013-06-05 21:33:52 +00:00
|
|
|
if (p instanceof ModelBaseClass) { // acts as setter
|
2013-11-05 06:53:02 +00:00
|
|
|
this[fk] = p[idName];
|
2013-04-11 23:23:34 +00:00
|
|
|
this.__cachedRelations[methodName] = p;
|
|
|
|
} else if (typeof p === 'function') { // acts as async getter
|
|
|
|
if (typeof cachedValue === 'undefined') {
|
|
|
|
this.__finders__[methodName].apply(self, [this[fk], function(err, inst) {
|
|
|
|
if (!err) {
|
|
|
|
self.__cachedRelations[methodName] = inst;
|
|
|
|
}
|
|
|
|
p(err, inst);
|
|
|
|
}]);
|
|
|
|
return this[fk];
|
|
|
|
} else {
|
|
|
|
p(null, cachedValue);
|
|
|
|
return cachedValue;
|
|
|
|
}
|
|
|
|
} else if (typeof p === 'undefined') { // acts as sync getter
|
|
|
|
return this[fk];
|
|
|
|
} else { // setter
|
|
|
|
this[fk] = p;
|
|
|
|
delete this.__cachedRelations[methodName];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2013-04-12 21:35:06 +00:00
|
|
|
/**
|
|
|
|
* Many-to-many relation
|
|
|
|
*
|
|
|
|
* Post.hasAndBelongsToMany('tags'); creates connection model 'PostTag'
|
|
|
|
*/
|
2013-06-05 21:33:52 +00:00
|
|
|
Relation.hasAndBelongsToMany = function hasAndBelongsToMany(anotherClass, params) {
|
2013-04-12 21:35:06 +00:00
|
|
|
params = params || {};
|
2013-07-23 18:16:43 +00:00
|
|
|
var models = this.dataSource.models;
|
2013-04-12 21:35:06 +00:00
|
|
|
|
|
|
|
if ('string' === typeof anotherClass) {
|
|
|
|
params.as = anotherClass;
|
|
|
|
if (params.model) {
|
|
|
|
anotherClass = params.model;
|
|
|
|
} else {
|
|
|
|
anotherClass = lookupModel(i8n.singularize(anotherClass)) ||
|
|
|
|
anotherClass;
|
|
|
|
}
|
|
|
|
if (typeof anotherClass === 'string') {
|
|
|
|
throw new Error('Could not find "' + anotherClass + '" relation for ' + this.modelName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!params.through) {
|
|
|
|
var name1 = this.modelName + anotherClass.modelName;
|
|
|
|
var name2 = anotherClass.modelName + this.modelName;
|
|
|
|
params.through = lookupModel(name1) || lookupModel(name2) ||
|
2013-07-23 18:16:43 +00:00
|
|
|
this.dataSource.define(name1);
|
2013-04-12 21:35:06 +00:00
|
|
|
}
|
|
|
|
params.through.belongsTo(this);
|
|
|
|
params.through.belongsTo(anotherClass);
|
|
|
|
|
|
|
|
this.hasMany(anotherClass, {as: params.as, through: params.through});
|
|
|
|
|
|
|
|
function lookupModel(modelName) {
|
|
|
|
var lookupClassName = modelName.toLowerCase();
|
|
|
|
for (var name in models) {
|
|
|
|
if (name.toLowerCase() === lookupClassName) {
|
|
|
|
return models[name];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|