removeSubdocs.js 643 B

12345678910111213141516171819202122232425262728293031
  1. 'use strict';
  2. const each = require('../helpers/each');
  3. /*!
  4. * ignore
  5. */
  6. module.exports = function(schema) {
  7. const unshift = true;
  8. schema.s.hooks.pre('remove', false, function(next) {
  9. if (this.$isSubdocument) {
  10. next();
  11. return;
  12. }
  13. const _this = this;
  14. const subdocs = this.$getAllSubdocs();
  15. each(subdocs, function(subdoc, cb) {
  16. subdoc.$__remove(cb);
  17. }, function(error) {
  18. if (error) {
  19. return _this.$__schema.s.hooks.execPost('remove:error', _this, [_this], { error: error }, function(error) {
  20. next(error);
  21. });
  22. }
  23. next();
  24. });
  25. }, null, unshift);
  26. };