Created
March 11, 2015 15:03
-
-
Save beeman/98c6beeafe24b86ac768 to your computer and use it in GitHub Desktop.
Working version of a cascading delete in a Loopback model definition
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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