Fixed embedsMany after LB integration

This commit is contained in:
Fabien Franzen 2014-07-30 13:22:20 +02:00
parent 7a9b64f1bf
commit 5cee6a4b79
1 changed files with 17 additions and 16 deletions

View File

@ -128,6 +128,7 @@ RelationDefinition.prototype.toJSON = function () {
* @param {Object} filter (where, order, limit, fields, ...) * @param {Object} filter (where, order, limit, fields, ...)
*/ */
RelationDefinition.prototype.applyScope = function(modelInstance, filter) { RelationDefinition.prototype.applyScope = function(modelInstance, filter) {
filter = filter || {};
filter.where = filter.where || {}; filter.where = filter.where || {};
if ((this.type !== 'belongsTo' || this.type === 'hasOne') if ((this.type !== 'belongsTo' || this.type === 'hasOne')
&& typeof this.discriminator === 'string') { // polymorphic && typeof this.discriminator === 'string') { // polymorphic
@ -1537,19 +1538,19 @@ RelationDefinition.embedsMany = function embedsMany(modelFrom, modelTo, params)
}; };
var findByIdFunc = scopeMethods.findById; var findByIdFunc = scopeMethods.findById;
modelFrom.prototype['__findById__' + relationName] = findByIdFunc; modelFrom.prototype['__findById__' + accessorName] = findByIdFunc;
var destroyByIdFunc = scopeMethods.destroy; var destroyByIdFunc = scopeMethods.destroy;
modelFrom.prototype['__destroyById__' + relationName] = destroyByIdFunc; modelFrom.prototype['__destroyById__' + accessorName] = destroyByIdFunc;
var updateByIdFunc = scopeMethods.updateById; var updateByIdFunc = scopeMethods.updateById;
modelFrom.prototype['__updateById__' + relationName] = updateByIdFunc; modelFrom.prototype['__updateById__' + accessorName] = updateByIdFunc;
var addFunc = scopeMethods.add; var addFunc = scopeMethods.add;
modelFrom.prototype['__link__' + relationName] = addFunc; modelFrom.prototype['__link__' + accessorName] = addFunc;
var removeFunc = scopeMethods.remove; var removeFunc = scopeMethods.remove;
modelFrom.prototype['__unlink__' + relationName] = removeFunc; modelFrom.prototype['__unlink__' + accessorName] = removeFunc;
scopeMethods.create = scopeMethod(definition, 'create'); scopeMethods.create = scopeMethod(definition, 'create');
scopeMethods.build = scopeMethod(definition, 'build'); scopeMethods.build = scopeMethod(definition, 'build');
@ -1614,17 +1615,15 @@ EmbedsMany.prototype.findById = function (fkId, cb) {
var embeddedList = modelInstance[relationName] || []; var embeddedList = modelInstance[relationName] || [];
fkId = fkId.toString(); // in case of explicit id
var find = function(id) { var find = function(id) {
for (var i = 0; i < embeddedList.length; i++) { for (var i = 0; i < embeddedList.length; i++) {
var item = embeddedList[i]; var item = embeddedList[i];
if (item[pk].toString() === fkId) return item; if (item[pk].toString() === id) return item;
} }
return null; return null;
}; };
var item = find(fkId); var item = find(fkId.toString()); // in case of explicit id
item = (item instanceof modelTo) ? item : null; item = (item instanceof modelTo) ? item : null;
if (typeof cb === 'function') { if (typeof cb === 'function') {
@ -1700,13 +1699,11 @@ EmbedsMany.prototype.destroyById = function (fkId, cb) {
if (typeof cb === 'function') { if (typeof cb === 'function') {
modelInstance.updateAttribute(relationName, modelInstance.updateAttribute(relationName,
embeddedList, function(err) { embeddedList, function(err) {
cb(err, inst); cb(err);
}); });
} }
} else if (typeof cb === 'function') { } else if (typeof cb === 'function') {
process.nextTick(function() { process.nextTick(cb); // not found
cb(null, null); // not found
});
} }
return inst; // sync return inst; // sync
}; };
@ -1782,7 +1779,11 @@ EmbedsMany.prototype.build = function(targetModelData) {
var ids = embeddedList.map(function(m) { var ids = embeddedList.map(function(m) {
return (typeof m[pk] === 'number' ? m[pk] : 0); return (typeof m[pk] === 'number' ? m[pk] : 0);
}); });
targetModelData[pk] = (Math.max(ids) || 0) + 1; if (ids.length > 0) {
targetModelData[pk] = Math.max.apply(null, ids) + 1;
} else {
targetModelData[pk] = 1;
}
} }
this.definition.applyProperties(this.modelInstance, targetModelData); this.definition.applyProperties(this.modelInstance, targetModelData);
@ -1880,7 +1881,7 @@ EmbedsMany.prototype.remove = function (acInst, cb) {
}); });
modelInstance.save(function(err) { modelInstance.save(function(err) {
cb(err, err ? [] : items); cb(err);
}); });
}); });
}; };