Skip to content

Instantly share code, notes, and snippets.

@beeman
Created March 11, 2015 15:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beeman/98c6beeafe24b86ac768 to your computer and use it in GitHub Desktop.
Save beeman/98c6beeafe24b86ac768 to your computer and use it in GitHub Desktop.
Working version of a cascading delete in a Loopback model definition
module.exports = function (Category) {
Category.observe('before delete', function (ctx, next) {
var Product = ctx.Model.app.models.Product;
Product.find({
where: {
categoryId: ctx.where.id
}
}, function (err, res) {
res.forEach(function (product) {
Product.destroyById(product.id, function () {
console.log("Deleted product", product.id);
});
});
});
next();
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment