Skip to content

Commit

Permalink
Deletion errors should break transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
theninj4 committed Jul 1, 2016
1 parent 390441f commit 0722f1e
Showing 1 changed file with 39 additions and 24 deletions.
63 changes: 39 additions & 24 deletions lib/sqlHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,42 @@ SqlStore.prototype._dealWithTransaction = function(done, callback) {
});
};

SqlStore.prototype._clearAndSetMany = function(relationModel, prop, theResource, keyName, ucKeyName, t, taskCallback) {
async.map(theResource[keyName] || [], function(deadRow, acallback) {
deadRow.destroy(t).asCallback(acallback);
}, function(deleteError) {
if (deleteError) return taskCallback(deleteError);

async.map(prop, function(item, acallback) {
relationModel.create(item, t).asCallback(function(err4, newRelationModel) {
if (err4) return acallback(err4);

theResource["add" + ucKeyName](newRelationModel, t).asCallback(acallback);
});
}, taskCallback);
});
};

SqlStore.prototype._clearAndSetOne = function(relationModel, prop, theResource, keyName, ucKeyName, t, taskCallback) {
var next = function(deleteError) {
if (deleteError) return taskCallback(deleteError);
if (!prop) {
theResource["set" + ucKeyName](null, t).asCallback(taskCallback);
} else {
relationModel.create(prop, t).asCallback(function(err3, newRelationModel) {
if (err3) return taskCallback(err3);

theResource["set" + ucKeyName](newRelationModel, t).asCallback(taskCallback);
});
}
}
if (theResource[keyName]) {
theResource[keyName].destroy(t).asCallback(next);
} else {
next();
}
};

SqlStore.prototype._clearAndSetRelationTables = function(theResource, partialResource, t, callback) {
var self = this;

Expand All @@ -347,34 +383,13 @@ SqlStore.prototype._clearAndSetRelationTables = function(theResource, partialRes
var relationModel = self.relations[relationName];

var keyName = self.resourceConfig.resource + "-" + relationName;
var uc = keyName[0].toUpperCase() + keyName.slice(1, keyName.length);
var ucKeyName = keyName[0].toUpperCase() + keyName.slice(1, keyName.length);

tasks[relationName] = function(taskCallback) {
if (prop instanceof Array) {
(theResource[keyName] || []).map(function(deadRow) {
deadRow.destroy(t);
});

async.map(prop, function(item, acallback) {
relationModel.create(item, t).asCallback(function(err4, newRelationModel) {
if (err4) return acallback(err4);

theResource["add" + uc](newRelationModel, t).asCallback(acallback);
});
}, taskCallback);
self._clearAndSetMany(relationModel, prop, theResource, keyName, ucKeyName, t, taskCallback);
} else {
if (theResource[keyName]) {
theResource[keyName].destroy(t);
}
if (!prop) {
theResource["set" + uc](null, t).asCallback(taskCallback);
} else {
relationModel.create(prop, t).asCallback(function(err3, newRelationModel) {
if (err3) return taskCallback(err3);

theResource["set" + uc](newRelationModel, t).asCallback(taskCallback);
});
}
self._clearAndSetOne(relationModel, prop, theResource, keyName, ucKeyName, t, taskCallback);
}
};
});
Expand Down

0 comments on commit 0722f1e

Please sign in to comment.