merge.js 966 B

123456789101112131415161718192021222324252627282930313233343536
  1. 'use strict';
  2. module.exports = function merge(s1, s2, skipConflictingPaths) {
  3. const paths = Object.keys(s2.tree);
  4. const pathsToAdd = {};
  5. for (const key of paths) {
  6. if (skipConflictingPaths && (s1.paths[key] || s1.nested[key] || s1.singleNestedPaths[key])) {
  7. continue;
  8. }
  9. pathsToAdd[key] = s2.tree[key];
  10. }
  11. s1.options._isMerging = true;
  12. s1.add(pathsToAdd, null);
  13. delete s1.options._isMerging;
  14. s1.callQueue = s1.callQueue.concat(s2.callQueue);
  15. s1.method(s2.methods);
  16. s1.static(s2.statics);
  17. for (const [option, value] of Object.entries(s2._userProvidedOptions)) {
  18. if (!(option in s1._userProvidedOptions)) {
  19. s1.set(option, value);
  20. }
  21. }
  22. for (const query in s2.query) {
  23. s1.query[query] = s2.query[query];
  24. }
  25. for (const virtual in s2.virtuals) {
  26. s1.virtuals[virtual] = s2.virtuals[virtual].clone();
  27. }
  28. s1._indexes = s1._indexes.concat(s2._indexes || []);
  29. s1.s.hooks.merge(s2.s.hooks, false);
  30. };