Skip to content

Instantly share code, notes, and snippets.

@Xinecraft
Forked from jordanell/models.js
Created November 8, 2019 07:48
Show Gist options
  • Save Xinecraft/59fb0cf7337a97a64d3dc34f171c3cd9 to your computer and use it in GitHub Desktop.
Save Xinecraft/59fb0cf7337a97a64d3dc34f171c3cd9 to your computer and use it in GitHub Desktop.
Sequelize paranoid delete cascade
import paranoidDeleteCascade from './helpers/paranoidDeleteCascade';
// Patch the paranoid delete functionality of Sequelize
sequelize.addHook('afterDestroy', paranoidDeleteCascade(db));
import isArray from 'lodash/isArray';
import map from 'lodash/map';
const paranoidDeleteCascade = (models) =>
async (instance, options, next) => {
// Only operate on paranoid models
if (!instance.$modelOptions.paranoid) {
return next();
}
const modelName = instance.$modelOptions.name.singular;
await Promise.all(
// Go over all associations of the instance model, and delete if needed
map(models[modelName].associations, async (association) => {
try {
// Only delete if cascade is set up correctly
if (association.options.onDelete !== 'CASCADE') {
return true;
}
let relationModel = association.target;
const getOptions = {};
// Handle "through" cases
if (association.through) {
relationModel = association.through.model;
// Include the id of the through model instance
getOptions.include = [{
model: relationModel,
}];
}
// Load id(s) of association
const instances = await instance[`get${association.as}`](getOptions);
if (isArray(instances)) {
// Association has no results so nothing to delete
if (instances.length === 0) {
return true;
}
// Delete all individually as bulk delete doesn't cascase in sequelize
return await Promise.all(instances.map(i => i.destroy()));
}
// Association is not set, so nothing to delete
if (!instances) {
return true;
}
return await instances.destroy();
} catch (error) {
// If we had issues deleting, we have bigger problems
Promise.resolve(true);
}
return true;
})
);
return next();
};
export default paranoidDeleteCascade;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment