Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sharding.js 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. 'use strict';
  2. const objectIdSymbol = require('../helpers/symbols').objectIdSymbol;
  3. const utils = require('../utils');
  4. /*!
  5. * ignore
  6. */
  7. module.exports = function shardingPlugin(schema) {
  8. schema.post('init', function() {
  9. storeShard.call(this);
  10. return this;
  11. });
  12. schema.pre('save', function(next) {
  13. applyWhere.call(this);
  14. next();
  15. });
  16. schema.post('save', function() {
  17. storeShard.call(this);
  18. });
  19. };
  20. /*!
  21. * ignore
  22. */
  23. function applyWhere() {
  24. let paths;
  25. let len;
  26. if (this.$__.shardval) {
  27. paths = Object.keys(this.$__.shardval);
  28. len = paths.length;
  29. this.$where = this.$where || {};
  30. for (let i = 0; i < len; ++i) {
  31. this.$where[paths[i]] = this.$__.shardval[paths[i]];
  32. }
  33. }
  34. }
  35. /*!
  36. * ignore
  37. */
  38. module.exports.storeShard = storeShard;
  39. /*!
  40. * ignore
  41. */
  42. function storeShard() {
  43. // backwards compat
  44. const key = this.schema.options.shardKey || this.schema.options.shardkey;
  45. if (!(key && utils.getFunctionName(key.constructor) === 'Object')) {
  46. return;
  47. }
  48. const orig = this.$__.shardval = {};
  49. const paths = Object.keys(key);
  50. const len = paths.length;
  51. let val;
  52. for (let i = 0; i < len; ++i) {
  53. val = this.getValue(paths[i]);
  54. if (val == null) {
  55. orig[paths[i]] = val;
  56. } else if (utils.isMongooseObject(val)) {
  57. orig[paths[i]] = val.toObject({depopulate: true, _isNested: true});
  58. } else if (val instanceof Date || val[objectIdSymbol]) {
  59. orig[paths[i]] = val;
  60. } else if (typeof val.valueOf === 'function') {
  61. orig[paths[i]] = val.valueOf();
  62. } else {
  63. orig[paths[i]] = val;
  64. }
  65. }
  66. }