Refactored embedsMany (relationName vs. propertyName)
This commit is contained in:
parent
f2c349b5b3
commit
dd9ea68f47
|
@ -9,6 +9,7 @@ var mergeQuery = require('./scope.js').mergeQuery;
|
|||
var ModelBaseClass = require('./model.js');
|
||||
var applyFilter = require('./connectors/memory').applyFilter;
|
||||
var ValidationError = require('./validations.js').ValidationError;
|
||||
var debug = require('debug')('loopback:relations');
|
||||
|
||||
exports.Relation = Relation;
|
||||
exports.RelationDefinition = RelationDefinition;
|
||||
|
@ -89,7 +90,6 @@ function RelationDefinition(definition) {
|
|||
}
|
||||
definition = definition || {};
|
||||
this.name = definition.name;
|
||||
this.accessor = definition.accessor || this.name;
|
||||
assert(this.name, 'Relation name is missing');
|
||||
this.type = normalizeType(definition.type);
|
||||
assert(this.type, 'Invalid relation type: ' + definition.type);
|
||||
|
@ -1467,18 +1467,22 @@ RelationDefinition.embedsMany = function embedsMany(modelFrom, modelTo, params)
|
|||
modelTo = lookupModelTo(modelFrom, modelTo, params, true);
|
||||
|
||||
var thisClassName = modelFrom.modelName;
|
||||
var accessorName = params.as || (i8n.camelize(modelTo.modelName, true) + 'List');
|
||||
var relationName = params.property || i8n.camelize(modelTo.pluralModelName, true);
|
||||
var fk = modelTo.dataSource.idName(modelTo.modelName) || 'id';
|
||||
var idName = modelFrom.dataSource.idName(modelFrom.modelName) || 'id';
|
||||
var relationName = params.as || (i8n.camelize(modelTo.modelName, true) + 'List');
|
||||
var propertyName = params.property || i8n.camelize(modelTo.pluralModelName, true);
|
||||
var idName = modelTo.dataSource.idName(modelTo.modelName) || 'id';
|
||||
|
||||
var definition = modelFrom.relations[accessorName] = new RelationDefinition({
|
||||
accessor: accessorName,
|
||||
if (relationName === propertyName) {
|
||||
propertyName = '_' + propertyName;
|
||||
debug('EmbedsMany property cannot be equal to relation name: ' +
|
||||
'forcing property %s for relation %s', propertyName, relationName);
|
||||
}
|
||||
|
||||
var definition = modelFrom.relations[relationName] = new RelationDefinition({
|
||||
name: relationName,
|
||||
type: RelationTypes.embedsMany,
|
||||
modelFrom: modelFrom,
|
||||
keyFrom: idName,
|
||||
keyTo: fk,
|
||||
keyFrom: propertyName,
|
||||
keyTo: idName,
|
||||
modelTo: modelTo,
|
||||
multiple: true,
|
||||
properties: params.properties,
|
||||
|
@ -1487,7 +1491,7 @@ RelationDefinition.embedsMany = function embedsMany(modelFrom, modelTo, params)
|
|||
embed: true
|
||||
});
|
||||
|
||||
modelFrom.dataSource.defineProperty(modelFrom.modelName, relationName, {
|
||||
modelFrom.dataSource.defineProperty(modelFrom.modelName, propertyName, {
|
||||
type: [modelTo], default: function() { return []; }
|
||||
});
|
||||
|
||||
|
@ -1495,14 +1499,14 @@ RelationDefinition.embedsMany = function embedsMany(modelFrom, modelTo, params)
|
|||
modelTo.validatesPresenceOf(idName);
|
||||
|
||||
if (!params.polymorphic) {
|
||||
modelFrom.validate(relationName, function(err) {
|
||||
var embeddedList = this[relationName] || [];
|
||||
modelFrom.validate(propertyName, function(err) {
|
||||
var embeddedList = this[propertyName] || [];
|
||||
var ids = embeddedList.map(function(m) { return m[idName]; });
|
||||
var uniqueIds = ids.filter(function(id, pos) {
|
||||
return ids.indexOf(id) === pos;
|
||||
});
|
||||
if (ids.length !== uniqueIds.length) {
|
||||
this.errors.add(relationName, 'Contains duplicate `' + idName + '`', 'uniqueness');
|
||||
this.errors.add(propertyName, 'Contains duplicate `' + idName + '`', 'uniqueness');
|
||||
err(false);
|
||||
}
|
||||
}, { code: 'uniqueness' })
|
||||
|
@ -1510,9 +1514,9 @@ RelationDefinition.embedsMany = function embedsMany(modelFrom, modelTo, params)
|
|||
|
||||
// validate all embedded items
|
||||
if (definition.options.validate) {
|
||||
modelFrom.validate(relationName, function(err) {
|
||||
modelFrom.validate(propertyName, function(err) {
|
||||
var self = this;
|
||||
var embeddedList = this[relationName] || [];
|
||||
var embeddedList = this[propertyName] || [];
|
||||
var hasErrors = false;
|
||||
embeddedList.forEach(function(item) {
|
||||
if (item instanceof modelTo) {
|
||||
|
@ -1522,11 +1526,11 @@ RelationDefinition.embedsMany = function embedsMany(modelFrom, modelTo, params)
|
|||
var first = Object.keys(item.errors)[0];
|
||||
var msg = 'contains invalid item: `' + id + '`';
|
||||
msg += ' (' + first + ' ' + item.errors[first] + ')';
|
||||
self.errors.add(relationName, msg, 'invalid');
|
||||
self.errors.add(propertyName, msg, 'invalid');
|
||||
}
|
||||
} else {
|
||||
hasErrors = true;
|
||||
self.errors.add(relationName, 'Contains invalid item', 'invalid');
|
||||
self.errors.add(propertyName, 'Contains invalid item', 'invalid');
|
||||
}
|
||||
});
|
||||
if (hasErrors) err(false);
|
||||
|
@ -1547,19 +1551,19 @@ RelationDefinition.embedsMany = function embedsMany(modelFrom, modelTo, params)
|
|||
};
|
||||
|
||||
var findByIdFunc = scopeMethods.findById;
|
||||
modelFrom.prototype['__findById__' + accessorName] = findByIdFunc;
|
||||
modelFrom.prototype['__findById__' + relationName] = findByIdFunc;
|
||||
|
||||
var destroyByIdFunc = scopeMethods.destroy;
|
||||
modelFrom.prototype['__destroyById__' + accessorName] = destroyByIdFunc;
|
||||
modelFrom.prototype['__destroyById__' + relationName] = destroyByIdFunc;
|
||||
|
||||
var updateByIdFunc = scopeMethods.updateById;
|
||||
modelFrom.prototype['__updateById__' + accessorName] = updateByIdFunc;
|
||||
modelFrom.prototype['__updateById__' + relationName] = updateByIdFunc;
|
||||
|
||||
var addFunc = scopeMethods.add;
|
||||
modelFrom.prototype['__link__' + accessorName] = addFunc;
|
||||
modelFrom.prototype['__link__' + relationName] = addFunc;
|
||||
|
||||
var removeFunc = scopeMethods.remove;
|
||||
modelFrom.prototype['__unlink__' + accessorName] = removeFunc;
|
||||
modelFrom.prototype['__unlink__' + relationName] = removeFunc;
|
||||
|
||||
scopeMethods.create = scopeMethod(definition, 'create');
|
||||
scopeMethods.build = scopeMethod(definition, 'build');
|
||||
|
@ -1572,12 +1576,12 @@ RelationDefinition.embedsMany = function embedsMany(modelFrom, modelTo, params)
|
|||
var methodName = customMethods[i];
|
||||
var method = scopeMethods[methodName];
|
||||
if (typeof method === 'function' && method.shared === true) {
|
||||
modelFrom.prototype['__' + methodName + '__' + accessorName] = method;
|
||||
modelFrom.prototype['__' + methodName + '__' + relationName] = method;
|
||||
}
|
||||
};
|
||||
|
||||
// Mix the property and scoped methods into the prototype class
|
||||
var scopeDefinition = defineScope(modelFrom.prototype, modelTo, accessorName, function () {
|
||||
var scopeDefinition = defineScope(modelFrom.prototype, modelTo, relationName, function () {
|
||||
return {};
|
||||
}, scopeMethods, definition.options);
|
||||
|
||||
|
@ -1586,7 +1590,7 @@ RelationDefinition.embedsMany = function embedsMany(modelFrom, modelTo, params)
|
|||
|
||||
EmbedsMany.prototype.related = function(receiver, scopeParams, condOrRefresh, cb) {
|
||||
var modelTo = this.definition.modelTo;
|
||||
var relationName = this.definition.name;
|
||||
var propertyName = this.definition.keyFrom;
|
||||
var modelInstance = this.modelInstance;
|
||||
var self = receiver;
|
||||
|
||||
|
@ -1605,7 +1609,7 @@ EmbedsMany.prototype.related = function(receiver, scopeParams, condOrRefresh, cb
|
|||
throw new Error('Method can be only called with one or two arguments');
|
||||
}
|
||||
|
||||
var embeddedList = self[relationName] || [];
|
||||
var embeddedList = self[propertyName] || [];
|
||||
|
||||
this.definition.applyScope(modelInstance, actualCond);
|
||||
|
||||
|
@ -1627,12 +1631,12 @@ EmbedsMany.prototype.related = function(receiver, scopeParams, condOrRefresh, cb
|
|||
};
|
||||
|
||||
EmbedsMany.prototype.findById = function (fkId, cb) {
|
||||
var pk = this.definition.keyFrom;
|
||||
var pk = this.definition.keyTo;
|
||||
var modelTo = this.definition.modelTo;
|
||||
var relationName = this.definition.name;
|
||||
var propertyName = this.definition.keyFrom;
|
||||
var modelInstance = this.modelInstance;
|
||||
|
||||
var embeddedList = modelInstance[relationName] || [];
|
||||
var embeddedList = modelInstance[propertyName] || [];
|
||||
|
||||
var find = function(id) {
|
||||
for (var i = 0; i < embeddedList.length; i++) {
|
||||
|
@ -1669,10 +1673,10 @@ EmbedsMany.prototype.updateById = function (fkId, data, cb) {
|
|||
}
|
||||
|
||||
var modelTo = this.definition.modelTo;
|
||||
var relationName = this.definition.name;
|
||||
var propertyName = this.definition.keyFrom;
|
||||
var modelInstance = this.modelInstance;
|
||||
|
||||
var embeddedList = modelInstance[relationName] || [];
|
||||
var embeddedList = modelInstance[propertyName] || [];
|
||||
|
||||
var inst = this.findById(fkId);
|
||||
|
||||
|
@ -1690,7 +1694,7 @@ EmbedsMany.prototype.updateById = function (fkId, data, cb) {
|
|||
}
|
||||
|
||||
if (typeof cb === 'function') {
|
||||
modelInstance.updateAttribute(relationName,
|
||||
modelInstance.updateAttribute(propertyName,
|
||||
embeddedList, function(err) {
|
||||
cb(err, inst);
|
||||
});
|
||||
|
@ -1705,10 +1709,10 @@ EmbedsMany.prototype.updateById = function (fkId, data, cb) {
|
|||
|
||||
EmbedsMany.prototype.destroyById = function (fkId, cb) {
|
||||
var modelTo = this.definition.modelTo;
|
||||
var relationName = this.definition.name;
|
||||
var propertyName = this.definition.keyFrom;
|
||||
var modelInstance = this.modelInstance;
|
||||
|
||||
var embeddedList = modelInstance[relationName] || [];
|
||||
var embeddedList = modelInstance[propertyName] || [];
|
||||
|
||||
var inst = (fkId instanceof modelTo) ? fkId : this.findById(fkId);
|
||||
|
||||
|
@ -1716,7 +1720,7 @@ EmbedsMany.prototype.destroyById = function (fkId, cb) {
|
|||
var index = embeddedList.indexOf(inst);
|
||||
if (index > -1) embeddedList.splice(index, 1);
|
||||
if (typeof cb === 'function') {
|
||||
modelInstance.updateAttribute(relationName,
|
||||
modelInstance.updateAttribute(propertyName,
|
||||
embeddedList, function(err) {
|
||||
cb(err);
|
||||
});
|
||||
|
@ -1733,10 +1737,10 @@ EmbedsMany.prototype.unset = EmbedsMany.prototype.destroyById;
|
|||
|
||||
EmbedsMany.prototype.at = function (index, cb) {
|
||||
var modelTo = this.definition.modelTo;
|
||||
var relationName = this.definition.name;
|
||||
var propertyName = this.definition.keyFrom;
|
||||
var modelInstance = this.modelInstance;
|
||||
|
||||
var embeddedList = modelInstance[relationName] || [];
|
||||
var embeddedList = modelInstance[propertyName] || [];
|
||||
|
||||
var item = embeddedList[parseInt(index)];
|
||||
item = (item instanceof modelTo) ? item : null;
|
||||
|
@ -1751,9 +1755,9 @@ EmbedsMany.prototype.at = function (index, cb) {
|
|||
};
|
||||
|
||||
EmbedsMany.prototype.create = function (targetModelData, cb) {
|
||||
var pk = this.definition.keyFrom;
|
||||
var pk = this.definition.keyTo;
|
||||
var modelTo = this.definition.modelTo;
|
||||
var relationName = this.definition.name;
|
||||
var propertyName = this.definition.keyFrom;
|
||||
var modelInstance = this.modelInstance;
|
||||
var autoId = this.definition.options.autoId !== false;
|
||||
|
||||
|
@ -1763,7 +1767,7 @@ EmbedsMany.prototype.create = function (targetModelData, cb) {
|
|||
}
|
||||
targetModelData = targetModelData || {};
|
||||
|
||||
var embeddedList = modelInstance[relationName] || [];
|
||||
var embeddedList = modelInstance[propertyName] || [];
|
||||
|
||||
var inst = this.build(targetModelData);
|
||||
|
||||
|
@ -1775,20 +1779,20 @@ EmbedsMany.prototype.create = function (targetModelData, cb) {
|
|||
});
|
||||
}
|
||||
|
||||
modelInstance.updateAttribute(relationName,
|
||||
modelInstance.updateAttribute(propertyName,
|
||||
embeddedList, function(err, modelInst) {
|
||||
cb(err, err ? null : inst);
|
||||
});
|
||||
};
|
||||
|
||||
EmbedsMany.prototype.build = function(targetModelData) {
|
||||
var pk = this.definition.keyFrom;
|
||||
var pk = this.definition.keyTo;
|
||||
var modelTo = this.definition.modelTo;
|
||||
var relationName = this.definition.name;
|
||||
var propertyName = this.definition.keyFrom;
|
||||
var modelInstance = this.modelInstance;
|
||||
var autoId = this.definition.options.autoId !== false;
|
||||
|
||||
var embeddedList = modelInstance[relationName] || [];
|
||||
var embeddedList = modelInstance[propertyName] || [];
|
||||
|
||||
targetModelData = targetModelData || {};
|
||||
|
||||
|
@ -1890,7 +1894,7 @@ EmbedsMany.prototype.remove = function (acInst, cb) {
|
|||
|
||||
belongsTo.applyScope(modelInstance, filter);
|
||||
|
||||
modelInstance[definition.accessor](filter, function(err, items) {
|
||||
modelInstance[definition.name](filter, function(err, items) {
|
||||
if (err) return cb(err);
|
||||
|
||||
items.forEach(function(item) {
|
||||
|
|
|
@ -918,7 +918,7 @@ describe('Load models with relations', function () {
|
|||
var ds = new DataSource('memory');
|
||||
|
||||
var Post = ds.define('Post', {userId: Number, content: String});
|
||||
var User = ds.define('User', {name: String}, {relations: {posts: {type: 'embedsMany', model: 'Post'}}});
|
||||
var User = ds.define('User', {name: String}, {relations: {posts: {type: 'embedsMany', model: 'Post' }}});
|
||||
|
||||
assert(User.relations['posts']);
|
||||
done();
|
||||
|
|
Loading…
Reference in New Issue