removeSubdocs.js 736 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. 'use strict';
  2. const each = require('async/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.ownerDocument) {
  10. next();
  11. return;
  12. }
  13. const _this = this;
  14. const subdocs = this.$__getAllSubdocs();
  15. if (!subdocs.length) {
  16. next();
  17. return;
  18. }
  19. each(subdocs, function(subdoc, cb) {
  20. subdoc.$__remove(function(err) {
  21. cb(err);
  22. });
  23. }, function(error) {
  24. if (error) {
  25. return _this.schema.s.hooks.execPost('remove:error', _this, [_this], { error: error }, function(error) {
  26. next(error);
  27. });
  28. }
  29. next();
  30. });
  31. }, null, unshift);
  32. };