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