Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix cyclic update #2

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 56 additions & 22 deletions lib/relationHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,51 @@ var setupRelationHooks = function(relatedCollection, relation) {
});
};

var getTopLevelFields = function(fields) {
return fields.map(function(field) {
if (field.indexOf('.') >= 0) {
return field.split('.')[0];
}

return field;
});
};

var getModifiedFields = function(modifier) {
if (utils.isModifier(modifier)) {
return Object.keys(modifier).reduce(function(fields, key) {
return fields.concat(
getTopLevelFields(Object.keys(modifier[key]))
);
}, []);
}

return getTopLevelFields(Object.keys(modifier));
};

var shouldUpdate = function(modifier) {
var projectionKeys = Object.keys(relation.projection),
modifiedFields = getModifiedFields(modifier);

return projectionKeys.filter(function(key) {
return key !== relation.key;
}).some(function(key) {
return modifiedFields.indexOf(key) >= 0;
});
};

var beforeUpdate = function(params, callback) {
if (relation.onUpdate === 'cascade') {
params.meta.modifiedIdentifiers = params.meta.modifiedIdentifiers || {};

if (
relation.onUpdate === 'cascade' &&
!params.meta.modifiedIdentifiers[relation.key] &&
shouldUpdate(params.modifier)
) {
getModifiedIdentifiers(params.condition, function(err, identifiers) {
if (err) return callback(err);

params.meta.modifiedIdentifiers = identifiers;
params.meta.modifiedIdentifiers[relation.key] = identifiers;

callback();
});
Expand All @@ -39,32 +78,27 @@ var setupRelationHooks = function(relatedCollection, relation) {
relation.collection.on('beforeUpdateMany', beforeUpdate);

var afterUpdate = function(params, callback) {
var identifiers = params.meta.modifiedIdentifiers || [];
var identifiers = params.meta.modifiedIdentifiers[relation.key] || [];

if (!identifiers.length) return callback();
if (!identifiers.length || !shouldUpdate(params.modifier)) return callback();

if (relation.onUpdate === 'cascade') {
// in cascade mode we need to update each updated identifier
var funcs = identifiers.map(function(identifier) {
return function(callback) {
var projectionKeys = Object.keys(relation.projection);
if (projectionKeys.length === 1 && relation.projection[relation.key]) {
callback();
} else {
var condition = utils.createObject(
relation.paths.identifier,
identifier
);

var modifier = {
$set: utils.createObject(
relation.paths.modifier,
relation.embedder(identifier)
)
};

relatedCollection.updateMany(condition, modifier, callback);
}
var condition = utils.createObject(
relation.paths.identifier,
identifier
);

var modifier = {
$set: utils.createObject(
relation.paths.modifier,
relation.embedder(identifier)
)
};

relatedCollection.updateMany(condition, modifier, callback);
};
});

Expand Down