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.

compile.js 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. 'use strict';
  2. const get = require('../../helpers/get');
  3. const getSymbol = require('../../helpers/symbols').getSymbol;
  4. const utils = require('../../utils');
  5. let Document;
  6. /*!
  7. * exports
  8. */
  9. exports.compile = compile;
  10. exports.defineKey = defineKey;
  11. /*!
  12. * Compiles schemas.
  13. */
  14. function compile(tree, proto, prefix, options) {
  15. Document = Document || require('../../document');
  16. const keys = Object.keys(tree);
  17. const len = keys.length;
  18. let limb;
  19. let key;
  20. for (let i = 0; i < len; ++i) {
  21. key = keys[i];
  22. limb = tree[key];
  23. const hasSubprops = utils.getFunctionName(limb.constructor) === 'Object' &&
  24. Object.keys(limb).length &&
  25. (!limb[options.typeKey] || (options.typeKey === 'type' && limb.type.type));
  26. const subprops = hasSubprops ? limb : null;
  27. defineKey(key, subprops, proto, prefix, keys, options);
  28. }
  29. }
  30. /*!
  31. * Defines the accessor named prop on the incoming prototype.
  32. */
  33. function defineKey(prop, subprops, prototype, prefix, keys, options) {
  34. Document = Document || require('../../document');
  35. const path = (prefix ? prefix + '.' : '') + prop;
  36. prefix = prefix || '';
  37. if (subprops) {
  38. Object.defineProperty(prototype, prop, {
  39. enumerable: true,
  40. configurable: true,
  41. get: function() {
  42. const _this = this;
  43. if (!this.$__.getters) {
  44. this.$__.getters = {};
  45. }
  46. if (!this.$__.getters[path]) {
  47. const nested = Object.create(Document.prototype, getOwnPropertyDescriptors(this));
  48. // save scope for nested getters/setters
  49. if (!prefix) {
  50. nested.$__.scope = this;
  51. }
  52. nested.$__.nestedPath = path;
  53. Object.defineProperty(nested, 'schema', {
  54. enumerable: false,
  55. configurable: true,
  56. writable: false,
  57. value: prototype.schema
  58. });
  59. Object.defineProperty(nested, 'toObject', {
  60. enumerable: false,
  61. configurable: true,
  62. writable: false,
  63. value: function() {
  64. return utils.clone(_this.get(path, null, {
  65. virtuals: get(this, 'schema.options.toObject.virtuals', null)
  66. }));
  67. }
  68. });
  69. Object.defineProperty(nested, 'toJSON', {
  70. enumerable: false,
  71. configurable: true,
  72. writable: false,
  73. value: function() {
  74. return _this.get(path, null, {
  75. virtuals: get(_this, 'schema.options.toJSON.virtuals', null)
  76. });
  77. }
  78. });
  79. Object.defineProperty(nested, '$__isNested', {
  80. enumerable: false,
  81. configurable: true,
  82. writable: false,
  83. value: true
  84. });
  85. compile(subprops, nested, path, options);
  86. this.$__.getters[path] = nested;
  87. }
  88. return this.$__.getters[path];
  89. },
  90. set: function(v) {
  91. if (v instanceof Document) {
  92. v = v.toObject({ transform: false });
  93. }
  94. const doc = this.$__.scope || this;
  95. return doc.$set(path, v);
  96. }
  97. });
  98. } else {
  99. Object.defineProperty(prototype, prop, {
  100. enumerable: true,
  101. configurable: true,
  102. get: function() {
  103. return this[getSymbol].call(this.$__.scope || this, path);
  104. },
  105. set: function(v) {
  106. return this.$set.call(this.$__.scope || this, path, v);
  107. }
  108. });
  109. }
  110. }
  111. // gets descriptors for all properties of `object`
  112. // makes all properties non-enumerable to match previous behavior to #2211
  113. function getOwnPropertyDescriptors(object) {
  114. const result = {};
  115. Object.getOwnPropertyNames(object).forEach(function(key) {
  116. result[key] = Object.getOwnPropertyDescriptor(object, key);
  117. // Assume these are schema paths, ignore them re: #5470
  118. if (result[key].get) {
  119. delete result[key];
  120. return;
  121. }
  122. result[key].enumerable = ['isNew', '$__', 'errors', '_doc'].indexOf(key) === -1;
  123. });
  124. return result;
  125. }